Skip to content

Commit 50c77f7

Browse files
committed
more fixes for the data classes
1 parent c7dc0ed commit 50c77f7

File tree

6 files changed

+340
-109
lines changed

6 files changed

+340
-109
lines changed

core/src/processing/data/FloatDict.java

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,106 @@ public void div(String key, float amount) {
365365
}
366366

367367

368+
private void checkMinMax(String functionName) {
369+
if (count == 0) {
370+
String msg =
371+
String.format("Cannot use %s() on an empty %s.",
372+
functionName, getClass().getSimpleName());
373+
throw new RuntimeException(msg);
374+
}
375+
}
376+
377+
378+
/**
379+
* @webref floatlist:method
380+
* @brief Return the smallest value
381+
*/
382+
public int minIndex() {
383+
checkMinMax("minIndex");
384+
// Will still return NaN if there is 1 or more entries, and they're all NaN
385+
float m = Float.NaN;
386+
int mi = -1;
387+
for (int i = 0; i < count; i++) {
388+
// find one good value to start
389+
if (values[i] == values[i]) {
390+
m = values[i];
391+
mi = i;
392+
393+
// calculate the rest
394+
for (int j = i+1; j < count; j++) {
395+
float d = values[j];
396+
if (!Float.isNaN(d) && (d < m)) {
397+
m = values[j];
398+
mi = j;
399+
}
400+
}
401+
break;
402+
}
403+
}
404+
return mi;
405+
}
406+
407+
408+
public String minKey() {
409+
checkMinMax("minKey");
410+
return keys[minIndex()];
411+
}
412+
413+
414+
public float minValue() {
415+
checkMinMax("minValue");
416+
return values[minIndex()];
417+
}
418+
419+
420+
/**
421+
* @webref floatlist:method
422+
* @brief Return the largest value
423+
*/
424+
public int maxIndex() {
425+
checkMinMax("maxIndex");
426+
// Will still return NaN if there is 1 or more entries, and they're all NaN
427+
float m = Float.NaN;
428+
int mi = -1;
429+
for (int i = 0; i < count; i++) {
430+
// find one good value to start
431+
if (values[i] == values[i]) {
432+
m = values[i];
433+
mi = i;
434+
435+
// calculate the rest
436+
for (int j = i+1; j < count; j++) {
437+
float d = values[j];
438+
if (!Float.isNaN(d) && (d > m)) {
439+
m = values[j];
440+
mi = j;
441+
}
442+
}
443+
break;
444+
}
445+
}
446+
return mi;
447+
}
448+
449+
450+
public String maxKey() {
451+
checkMinMax("maxKey");
452+
return keys[maxIndex()];
453+
}
454+
455+
456+
public float maxValue() {
457+
checkMinMax("maxValue");
458+
return values[maxIndex()];
459+
}
460+
461+
368462
public int index(String what) {
369463
Integer found = indices.get(what);
370464
return (found == null) ? -1 : found.intValue();
371465
}
372466

373467

374-
// public void add(String key) {
375-
// if (index(key) != -1) {
376-
// throw new IllegalArgumentException("Use inc() to increment an entry, " +
377-
// "add() is for adding a new key");
378-
// }
379-
// add(key, 0);
380-
// }
381-
382-
383468
protected void create(String what, float much) {
384469
if (count == keys.length) {
385470
keys = PApplet.expand(keys);
@@ -396,12 +481,17 @@ protected void create(String what, float much) {
396481
* @webref floatdict:method
397482
* @brief Remove a key/value pair
398483
*/
399-
public void remove(String key) {
400-
removeIndex(index(key));
484+
public int remove(String key) {
485+
int index = index(key);
486+
if (index != -1) {
487+
removeIndex(index);
488+
}
489+
return index;
401490
}
402491

403492

404-
public void removeIndex(int index) {
493+
public String removeIndex(int index) {
494+
String key = keys[index];
405495
//System.out.println("index is " + which + " and " + keys[which]);
406496
indices.remove(keys[index]);
407497
for (int i = index; i < count-1; i++) {
@@ -412,6 +502,7 @@ public void removeIndex(int index) {
412502
count--;
413503
keys[count] = null;
414504
values[count] = 0;
505+
return key;
415506
}
416507

417508

core/src/processing/data/FloatList.java

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -132,41 +132,38 @@ public void set(int index, float what) {
132132
* @webref floatlist:method
133133
* @brief Remove an element from the specified index
134134
*/
135-
public void remove(int index) {
135+
public float remove(int index) {
136+
float entry = data[index];
136137
// int[] outgoing = new int[count - 1];
137138
// System.arraycopy(data, 0, outgoing, 0, index);
138139
// count--;
139140
// System.arraycopy(data, index + 1, outgoing, 0, count - index);
140141
// data = outgoing;
142+
// For most cases, this actually appears to be faster
143+
// than arraycopy() on an array copying into itself.
141144
for (int i = index; i < count-1; i++) {
142145
data[i] = data[i+1];
143146
}
144147
count--;
148+
return entry;
145149
}
146150

147151

148-
/** Remove the first instance of a particular value */
149-
public boolean removeValue(float value) {
150-
if (Float.isNaN(value)) {
151-
for (int i = 0; i < count; i++) {
152-
if (Float.isNaN(data[i])) {
153-
remove(i);
154-
return true;
155-
}
156-
}
157-
} else {
158-
int index = index(value);
159-
if (index != -1) {
160-
remove(index);
161-
return true;
162-
}
152+
// Remove the first instance of a particular value,
153+
// and return the index at which it was found.
154+
public int removeValue(int value) {
155+
int index = index(value);
156+
if (index != -1) {
157+
remove(index);
158+
return index;
163159
}
164-
return false;
160+
return -1;
165161
}
166162

167163

168-
/** Remove all instances of a particular value */
169-
public boolean removeValues(float value) {
164+
// Remove all instances of a particular value,
165+
// and return the number of values found and removed
166+
public int removeValues(int value) {
170167
int ii = 0;
171168
if (Float.isNaN(value)) {
172169
for (int i = 0; i < count; i++) {
@@ -181,11 +178,9 @@ public boolean removeValues(float value) {
181178
}
182179
}
183180
}
184-
if (count == ii) {
185-
return false;
186-
}
181+
int removed = count - ii;
187182
count = ii;
188-
return true;
183+
return removed;
189184
}
190185

191186

@@ -387,15 +382,6 @@ public int index(float what) {
387382
}
388383

389384

390-
// !!! TODO this is not yet correct, because it's not being reset when
391-
// the rest of the entries are changed
392-
// protected void cacheIndices() {
393-
// indexCache = new HashMap<Integer, Integer>();
394-
// for (int i = 0; i < count; i++) {
395-
// indexCache.put(data[i], i);
396-
// }
397-
// }
398-
399385
/**
400386
* @webref floatlist:method
401387
* @brief Check if a number is a part of the list
@@ -418,11 +404,6 @@ public boolean hasValue(float value) {
418404
}
419405

420406

421-
// doesn't really make sense with float.. use add() if you need it
422-
// public void increment(int index) {
423-
// data[index]++;
424-
// }
425-
426407
/**
427408
* @webref floatlist:method
428409
* @brief Add to a value
@@ -431,6 +412,7 @@ public void add(int index, float amount) {
431412
data[index] += amount;
432413
}
433414

415+
434416
/**
435417
* @webref floatlist:method
436418
* @brief Subtract from a value
@@ -439,6 +421,7 @@ public void sub(int index, float amount) {
439421
data[index] -= amount;
440422
}
441423

424+
442425
/**
443426
* @webref floatlist:method
444427
* @brief Multiply a value
@@ -447,6 +430,7 @@ public void mult(int index, float amount) {
447430
data[index] *= amount;
448431
}
449432

433+
450434
/**
451435
* @webref floatlist:method
452436
* @brief Divide a value
@@ -455,64 +439,86 @@ public void div(int index, float amount) {
455439
data[index] /= amount;
456440
}
457441

442+
443+
private void checkMinMax(String functionName) {
444+
if (count == 0) {
445+
String msg =
446+
String.format("Cannot use %s() on an empty %s.",
447+
functionName, getClass().getSimpleName());
448+
throw new RuntimeException(msg);
449+
}
450+
}
451+
452+
458453
/**
459454
* @webref floatlist:method
460455
* @brief Return the smallest value
461456
*/
462457
public float min() {
463-
if (count == 0) {
464-
throw new ArrayIndexOutOfBoundsException("Cannot use min() on IntList of length 0.");
465-
}
466-
if (data.length == 0) {
467-
return Float.NaN;
468-
}
458+
checkMinMax("min");
459+
int index = minIndex();
460+
return index == -1 ? Float.NaN : data[index];
461+
}
462+
463+
464+
public int minIndex() {
465+
checkMinMax("minIndex");
469466
float m = Float.NaN;
470-
for (int i = 0; i < data.length; i++) {
467+
int mi = -1;
468+
for (int i = 0; i < count; i++) {
471469
// find one good value to start
472470
if (data[i] == data[i]) {
473471
m = data[i];
472+
mi = i;
474473

475474
// calculate the rest
476-
for (int j = i+1; j < data.length; j++) {
475+
for (int j = i+1; j < count; j++) {
477476
float d = data[j];
478477
if (!Float.isNaN(d) && (d < m)) {
479478
m = data[j];
479+
mi = j;
480480
}
481481
}
482482
break;
483483
}
484484
}
485-
return m;
485+
return mi;
486486
}
487487

488+
488489
/**
489490
* @webref floatlist:method
490491
* @brief Return the largest value
491492
*/
492493
public float max() {
493-
if (count == 0) {
494-
throw new ArrayIndexOutOfBoundsException("Cannot use max() on IntList of length 0.");
495-
}
496-
if (data.length == 0) {
497-
return Float.NaN;
498-
}
494+
checkMinMax("max");
495+
int index = maxIndex();
496+
return index == -1 ? Float.NaN : data[index];
497+
}
498+
499+
500+
public int maxIndex() {
501+
checkMinMax("maxIndex");
499502
float m = Float.NaN;
500-
for (int i = 0; i < data.length; i++) {
503+
int mi = -1;
504+
for (int i = 0; i < count; i++) {
501505
// find one good value to start
502506
if (data[i] == data[i]) {
503507
m = data[i];
508+
mi = i;
504509

505510
// calculate the rest
506-
for (int j = i+1; j < data.length; j++) {
511+
for (int j = i+1; j < count; j++) {
507512
float d = data[j];
508513
if (!Float.isNaN(d) && (d > m)) {
509514
m = data[j];
515+
mi = j;
510516
}
511517
}
512518
break;
513519
}
514520
}
515-
return m;
521+
return mi;
516522
}
517523

518524

0 commit comments

Comments
 (0)