| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -365,21 +365,106 @@ public void div(String key, float amount) { | |||
| 365 | 365 | } | |
| 366 | 366 | ||
| 367 | 367 | ||
| 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 | + | ||
| 368 | 462 | public int index(String what) { | |
| 369 | 463 | Integer found = indices.get(what); | |
| 370 | 464 | return (found == null) ? -1 : found.intValue(); | |
| 371 | 465 | } | |
| 372 | 466 | ||
| 373 | 467 | ||
| 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 | - | ||
| 383 | 468 | protected void create(String what, float much) { | |
| 384 | 469 | if (count == keys.length) { | |
| 385 | 470 | keys = PApplet.expand(keys); | |
@@ -396,12 +481,17 @@ protected void create(String what, float much) { | |||
| 396 | 481 | * @webref floatdict:method | |
| 397 | 482 | * @brief Remove a key/value pair | |
| 398 | 483 | */ | |
| 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; | ||
| 401 | 490 | } | |
| 402 | 491 | ||
| 403 | 492 | ||
| 404 | - public void removeIndex(int index) { | ||
| 493 | + public String removeIndex(int index) { | ||
| 494 | + String key = keys[index]; | ||
| 405 | 495 | //System.out.println("index is " + which + " and " + keys[which]); | |
| 406 | 496 | indices.remove(keys[index]); | |
| 407 | 497 | for (int i = index; i < count-1; i++) { | |
@@ -412,6 +502,7 @@ public void removeIndex(int index) { | |||
| 412 | 502 | count--; | |
| 413 | 503 | keys[count] = null; | |
| 414 | 504 | values[count] = 0; | |
| 505 | + return key; | ||
| 415 | 506 | } | |
| 416 | 507 | ||
| 417 | 508 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,41 +132,38 @@ public void set(int index, float what) { | |||
| 132 | 132 | * @webref floatlist:method | |
| 133 | 133 | * @brief Remove an element from the specified index | |
| 134 | 134 | */ | |
| 135 | - public void remove(int index) { | ||
| 135 | + public float remove(int index) { | ||
| 136 | + float entry = data[index]; | ||
| 136 | 137 | // int[] outgoing = new int[count - 1]; | |
| 137 | 138 | // System.arraycopy(data, 0, outgoing, 0, index); | |
| 138 | 139 | // count--; | |
| 139 | 140 | // System.arraycopy(data, index + 1, outgoing, 0, count - index); | |
| 140 | 141 | // data = outgoing; | |
| 142 | + // For most cases, this actually appears to be faster | ||
| 143 | + // than arraycopy() on an array copying into itself. | ||
| 141 | 144 | for (int i = index; i < count-1; i++) { | |
| 142 | 145 | data[i] = data[i+1]; | |
| 143 | 146 | } | |
| 144 | 147 | count--; | |
| 148 | + return entry; | ||
| 145 | 149 | } | |
| 146 | 150 | ||
| 147 | 151 | ||
| 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; | ||
| 163 | 159 | } | |
| 164 | - return false; | ||
| 160 | + return -1; | ||
| 165 | 161 | } | |
| 166 | 162 | ||
| 167 | 163 | ||
| 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) { | ||
| 170 | 167 | int ii = 0; | |
| 171 | 168 | if (Float.isNaN(value)) { | |
| 172 | 169 | for (int i = 0; i < count; i++) { | |
@@ -181,11 +178,9 @@ public boolean removeValues(float value) { | |||
| 181 | 178 | } | |
| 182 | 179 | } | |
| 183 | 180 | } | |
| 184 | - if (count == ii) { | ||
| 185 | - return false; | ||
| 186 | - } | ||
| 181 | + int removed = count - ii; | ||
| 187 | 182 | count = ii; | |
| 188 | - return true; | ||
| 183 | + return removed; | ||
| 189 | 184 | } | |
| 190 | 185 | ||
| 191 | 186 | ||
@@ -387,15 +382,6 @@ public int index(float what) { | |||
| 387 | 382 | } | |
| 388 | 383 | ||
| 389 | 384 | ||
| 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 | - | ||
| 399 | 385 | /** | |
| 400 | 386 | * @webref floatlist:method | |
| 401 | 387 | * @brief Check if a number is a part of the list | |
@@ -418,11 +404,6 @@ public boolean hasValue(float value) { | |||
| 418 | 404 | } | |
| 419 | 405 | ||
| 420 | 406 | ||
| 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 | - | ||
| 426 | 407 | /** | |
| 427 | 408 | * @webref floatlist:method | |
| 428 | 409 | * @brief Add to a value | |
@@ -431,6 +412,7 @@ public void add(int index, float amount) { | |||
| 431 | 412 | data[index] += amount; | |
| 432 | 413 | } | |
| 433 | 414 | ||
| 415 | + | ||
| 434 | 416 | /** | |
| 435 | 417 | * @webref floatlist:method | |
| 436 | 418 | * @brief Subtract from a value | |
@@ -439,6 +421,7 @@ public void sub(int index, float amount) { | |||
| 439 | 421 | data[index] -= amount; | |
| 440 | 422 | } | |
| 441 | 423 | ||
| 424 | + | ||
| 442 | 425 | /** | |
| 443 | 426 | * @webref floatlist:method | |
| 444 | 427 | * @brief Multiply a value | |
@@ -447,6 +430,7 @@ public void mult(int index, float amount) { | |||
| 447 | 430 | data[index] *= amount; | |
| 448 | 431 | } | |
| 449 | 432 | ||
| 433 | + | ||
| 450 | 434 | /** | |
| 451 | 435 | * @webref floatlist:method | |
| 452 | 436 | * @brief Divide a value | |
@@ -455,64 +439,86 @@ public void div(int index, float amount) { | |||
| 455 | 439 | data[index] /= amount; | |
| 456 | 440 | } | |
| 457 | 441 | ||
| 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 | + | ||
| 458 | 453 | /** | |
| 459 | 454 | * @webref floatlist:method | |
| 460 | 455 | * @brief Return the smallest value | |
| 461 | 456 | */ | |
| 462 | 457 | 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"); | ||
| 469 | 466 | 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++) { | ||
| 471 | 469 | // find one good value to start | |
| 472 | 470 | if (data[i] == data[i]) { | |
| 473 | 471 | m = data[i]; | |
| 472 | + mi = i; | ||
| 474 | 473 | ||
| 475 | 474 | // calculate the rest | |
| 476 | - for (int j = i+1; j < data.length; j++) { | ||
| 475 | + for (int j = i+1; j < count; j++) { | ||
| 477 | 476 | float d = data[j]; | |
| 478 | 477 | if (!Float.isNaN(d) && (d < m)) { | |
| 479 | 478 | m = data[j]; | |
| 479 | + mi = j; | ||
| 480 | 480 | } | |
| 481 | 481 | } | |
| 482 | 482 | break; | |
| 483 | 483 | } | |
| 484 | 484 | } | |
| 485 | - return m; | ||
| 485 | + return mi; | ||
| 486 | 486 | } | |
| 487 | 487 | ||
| 488 | + | ||
| 488 | 489 | /** | |
| 489 | 490 | * @webref floatlist:method | |
| 490 | 491 | * @brief Return the largest value | |
| 491 | 492 | */ | |
| 492 | 493 | 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"); | ||
| 499 | 502 | 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++) { | ||
| 501 | 505 | // find one good value to start | |
| 502 | 506 | if (data[i] == data[i]) { | |
| 503 | 507 | m = data[i]; | |
| 508 | + mi = i; | ||
| 504 | 509 | ||
| 505 | 510 | // calculate the rest | |
| 506 | - for (int j = i+1; j < data.length; j++) { | ||
| 511 | + for (int j = i+1; j < count; j++) { | ||
| 507 | 512 | float d = data[j]; | |
| 508 | 513 | if (!Float.isNaN(d) && (d > m)) { | |
| 509 | 514 | m = data[j]; | |
| 515 | + mi = j; | ||
| 510 | 516 | } | |
| 511 | 517 | } | |
| 512 | 518 | break; | |
| 513 | 519 | } | |
| 514 | 520 | } | |
| 515 | - return m; | ||
| 521 | + return mi; | ||
| 516 | 522 | } | |
| 517 | 523 | ||
| 518 | 524 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments