@@ -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