Skip to content

Commit e1d9a0e

Browse files
committed
fixes for Iterable inconsistency
1 parent 74b0347 commit e1d9a0e

2 files changed

Lines changed: 90 additions & 33 deletions

File tree

core/src/processing/data/IntDict.java

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* A simple class to use a String as a lookup for an int value.
12-
*
12+
*
1313
* @webref data:composite
1414
*/
1515
public class IntDict {
@@ -109,9 +109,9 @@ public int size() {
109109
}
110110

111111

112-
/**
112+
/**
113113
* Remove all entries.
114-
*
114+
*
115115
* @webref intdict:method
116116
* @brief Remove all entries
117117
*/
@@ -126,32 +126,80 @@ public String key(int index) {
126126
}
127127

128128

129-
private void crop() {
130-
if (count != keys.length) {
131-
keys = PApplet.subset(keys, 0, count);
132-
values = PApplet.subset(values, 0, count);
133-
}
134-
}
129+
// private void crop() {
130+
// if (count != keys.length) {
131+
// keys = PApplet.subset(keys, 0, count);
132+
// values = PApplet.subset(values, 0, count);
133+
// }
134+
// }
135135

136136

137137
/**
138138
* Return the internal array being used to store the keys. Allocated but
139139
* unused entries will be removed. This array should not be modified.
140-
*
140+
*
141141
* @webref intdict:method
142142
* @brief Return the internal array being used to store the keys
143143
*/
144-
public String[] keys() {
145-
crop();
146-
return keys;
147-
}
144+
// public String[] keys() {
145+
// crop();
146+
// return keys;
147+
// }
148148

149149

150150
// public Iterable<String> keys() {
151151
// return new Iterable<String>() {
152152
//
153153
// @Override
154154
// public Iterator<String> iterator() {
155+
// return new Iterator<String>() {
156+
// int index = -1;
157+
//
158+
// public void remove() {
159+
// removeIndex(index);
160+
// }
161+
//
162+
// public String next() {
163+
// return key(++index);
164+
// }
165+
//
166+
// public boolean hasNext() {
167+
// return index+1 < size();
168+
// }
169+
// };
170+
// }
171+
// };
172+
// }
173+
174+
175+
// Use this with 'for' loops
176+
public Iterable<String> keys() {
177+
return new Iterable<String>() {
178+
179+
@Override
180+
public Iterator<String> iterator() {
181+
return keyIterator();
182+
// return new Iterator<String>() {
183+
// int index = -1;
184+
//
185+
// public void remove() {
186+
// removeIndex(index);
187+
// }
188+
//
189+
// public String next() {
190+
// return key(++index);
191+
// }
192+
//
193+
// public boolean hasNext() {
194+
// return index+1 < size();
195+
// }
196+
// };
197+
}
198+
};
199+
}
200+
201+
202+
// Use this to iterate when you want to be able to remove elements along the way
155203
public Iterator<String> keyIterator() {
156204
return new Iterator<String>() {
157205
int index = -1;
@@ -168,8 +216,6 @@ public boolean hasNext() {
168216
return index+1 < size();
169217
}
170218
};
171-
// }
172-
// };
173219
}
174220

175221

@@ -197,7 +243,8 @@ public int value(int index) {
197243
return values[index];
198244
}
199245

200-
/**
246+
247+
/**
201248
* @webref intdict:method
202249
* @brief To come...
203250
*/
@@ -206,21 +253,26 @@ public Iterable<Integer> values() {
206253

207254
@Override
208255
public Iterator<Integer> iterator() {
209-
return new Iterator<Integer>() {
210-
int index = -1;
256+
return valueIterator();
257+
}
258+
};
259+
}
211260

212-
public void remove() {
213-
removeIndex(index);
214-
}
215261

216-
public Integer next() {
217-
return value(++index);
218-
}
262+
public Iterator<Integer> valueIterator() {
263+
return new Iterator<Integer>() {
264+
int index = -1;
219265

220-
public boolean hasNext() {
221-
return index+1 < size();
222-
}
223-
};
266+
public void remove() {
267+
removeIndex(index);
268+
}
269+
270+
public Integer next() {
271+
return value(++index);
272+
}
273+
274+
public boolean hasNext() {
275+
return index+1 < size();
224276
}
225277
};
226278
}
@@ -285,9 +337,9 @@ public boolean hasKey(String key) {
285337
}
286338

287339

288-
/**
289-
* Increase the value of a specific key by 1.
290-
*
340+
/**
341+
* Increase the value of a specific key by 1.
342+
*
291343
* @webref intdict:method
292344
* @brief Increase the value of a specific key by 1
293345
*/
@@ -414,7 +466,7 @@ public void sortKeysReverse() {
414466

415467
/**
416468
* Sort by values in descending order (largest value will be at [0]).
417-
*
469+
*
418470
* @webref intdict:method
419471
* @brief Sort by values in descending order
420472
*/

core/todo.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
0218 core (2.0b10 or 2.0)
2+
_ data decision stuff
3+
_ key/valueIterator to get a version that can be removed in a loop
4+
_ should keys() returns cropped internal array after all?
5+
_ right now it's making a copy (which is safer, so folks don't modify)
6+
_ but this means that we don't have a fast method for access
27

38

49
before 2.0

0 commit comments

Comments
 (0)