99
1010/**
1111 * A simple table class to use a String as a lookup for an float value.
12+ *
13+ * @webref data:composite
14+ * @see IntDict
15+ * @see StringDict
1216 */
1317public class FloatDict {
1418
@@ -75,13 +79,21 @@ public FloatDict(String[] keys, float[] values) {
7579 }
7680 }
7781
78-
82+ /**
83+ * @webref floatdict:method
84+ * @brief Returns the number of key/value pairs
85+ */
7986 public int size () {
8087 return count ;
8188 }
8289
8390
84- /** Remove all entries. */
91+ /**
92+ * Remove all entries.
93+ *
94+ * @webref floatdict:method
95+ * @brief Remove all entries
96+ */
8597 public void clear () {
8698 count = 0 ;
8799 indices = new HashMap <String , Integer >();
@@ -110,7 +122,10 @@ protected void crop() {
110122// return keys;
111123// }
112124
113-
125+ /**
126+ * @webref floatdict:method
127+ * @brief Return the internal array being used to store the keys
128+ */
114129 public Iterable <String > keys () {
115130 return new Iterable <String >() {
116131
@@ -166,6 +181,9 @@ public void reset() {
166181
167182 /**
168183 * Return a copy of the internal keys array. This array can be modified.
184+ *
185+ * @webref floatdict:method
186+ * @brief Return a copy of the internal keys array
169187 */
170188 public String [] keyArray () {
171189 return keyArray (null );
@@ -191,7 +209,10 @@ public float value(int index) {
191209// return values;
192210// }
193211
194-
212+ /**
213+ * @webref floatdict:method
214+ * @brief Return the internal array being used to store the values
215+ */
195216 public Iterable <Float > values () {
196217 return new Iterable <Float >() {
197218
@@ -218,6 +239,9 @@ public boolean hasNext() {
218239
219240 /**
220241 * Create a new array and copy each of the values into it.
242+ *
243+ * @webref floatdict:method
244+ * @brief Create a new array and copy each of the values into it
221245 */
222246 public float [] valueArray () {
223247 return valueArray (null );
@@ -240,6 +264,9 @@ public float[] valueArray(float[] array) {
240264
241265 /**
242266 * Return a value for the specified key.
267+ *
268+ * @webref floatdict:method
269+ * @brief Return a value for the specified key
243270 */
244271 public float get (String key ) {
245272 int index = index (key );
@@ -248,7 +275,11 @@ public float get(String key) {
248275 }
249276
250277
251- public void set (String key , int amount ) {
278+ /**
279+ * @webref floatdict:method
280+ * @brief Create a new key/value pair or change the value of one
281+ */
282+ public void set (String key , float amount ) {
252283 int index = index (key );
253284 if (index == -1 ) {
254285 create (key , amount );
@@ -258,6 +289,10 @@ public void set(String key, int amount) {
258289 }
259290
260291
292+ /**
293+ * @webref floatdict:method
294+ * @brief Check if a key is a part of the data structure
295+ */
261296 public boolean hasKey (String key ) {
262297 return index (key ) != -1 ;
263298 }
@@ -275,6 +310,10 @@ public boolean hasKey(String key) {
275310// }
276311
277312
313+ /**
314+ * @webref floatdict:method
315+ * @brief Add to a value
316+ */
278317 public void add (String key , float amount ) {
279318 int index = index (key );
280319 if (index == -1 ) {
@@ -291,11 +330,19 @@ public void add(String key, float amount) {
291330// }
292331
293332
333+ /**
334+ * @webref floatdict:method
335+ * @brief Subtract from a value
336+ */
294337 public void sub (String key , float amount ) {
295338 add (key , -amount );
296339 }
297340
298341
342+ /**
343+ * @webref floatdict:method
344+ * @brief Multiply a value
345+ */
299346 public void mult (String key , float amount ) {
300347 int index = index (key );
301348 if (index != -1 ) {
@@ -304,6 +351,10 @@ public void mult(String key, float amount) {
304351 }
305352
306353
354+ /**
355+ * @webref floatdict:method
356+ * @brief Divide a value
357+ */
307358 public void div (String key , float amount ) {
308359 int index = index (key );
309360 if (index != -1 ) {
@@ -312,21 +363,106 @@ public void div(String key, float amount) {
312363 }
313364
314365
366+ private void checkMinMax (String functionName ) {
367+ if (count == 0 ) {
368+ String msg =
369+ String .format ("Cannot use %s() on an empty %s." ,
370+ functionName , getClass ().getSimpleName ());
371+ throw new RuntimeException (msg );
372+ }
373+ }
374+
375+
376+ /**
377+ * @webref floatlist:method
378+ * @brief Return the smallest value
379+ */
380+ public int minIndex () {
381+ checkMinMax ("minIndex" );
382+ // Will still return NaN if there is 1 or more entries, and they're all NaN
383+ float m = Float .NaN ;
384+ int mi = -1 ;
385+ for (int i = 0 ; i < count ; i ++) {
386+ // find one good value to start
387+ if (values [i ] == values [i ]) {
388+ m = values [i ];
389+ mi = i ;
390+
391+ // calculate the rest
392+ for (int j = i +1 ; j < count ; j ++) {
393+ float d = values [j ];
394+ if (!Float .isNaN (d ) && (d < m )) {
395+ m = values [j ];
396+ mi = j ;
397+ }
398+ }
399+ break ;
400+ }
401+ }
402+ return mi ;
403+ }
404+
405+
406+ public String minKey () {
407+ checkMinMax ("minKey" );
408+ return keys [minIndex ()];
409+ }
410+
411+
412+ public float minValue () {
413+ checkMinMax ("minValue" );
414+ return values [minIndex ()];
415+ }
416+
417+
418+ /**
419+ * @webref floatlist:method
420+ * @brief Return the largest value
421+ */
422+ public int maxIndex () {
423+ checkMinMax ("maxIndex" );
424+ // Will still return NaN if there is 1 or more entries, and they're all NaN
425+ float m = Float .NaN ;
426+ int mi = -1 ;
427+ for (int i = 0 ; i < count ; i ++) {
428+ // find one good value to start
429+ if (values [i ] == values [i ]) {
430+ m = values [i ];
431+ mi = i ;
432+
433+ // calculate the rest
434+ for (int j = i +1 ; j < count ; j ++) {
435+ float d = values [j ];
436+ if (!Float .isNaN (d ) && (d > m )) {
437+ m = values [j ];
438+ mi = j ;
439+ }
440+ }
441+ break ;
442+ }
443+ }
444+ return mi ;
445+ }
446+
447+
448+ public String maxKey () {
449+ checkMinMax ("maxKey" );
450+ return keys [maxIndex ()];
451+ }
452+
453+
454+ public float maxValue () {
455+ checkMinMax ("maxValue" );
456+ return values [maxIndex ()];
457+ }
458+
459+
315460 public int index (String what ) {
316461 Integer found = indices .get (what );
317462 return (found == null ) ? -1 : found .intValue ();
318463 }
319464
320465
321- // public void add(String key) {
322- // if (index(key) != -1) {
323- // throw new IllegalArgumentException("Use inc() to increment an entry, " +
324- // "add() is for adding a new key");
325- // }
326- // add(key, 0);
327- // }
328-
329-
330466 protected void create (String what , float much ) {
331467 if (count == keys .length ) {
332468 keys = PApplet .expand (keys );
@@ -339,12 +475,21 @@ protected void create(String what, float much) {
339475 }
340476
341477
342- public void remove (String key ) {
343- removeIndex (index (key ));
478+ /**
479+ * @webref floatdict:method
480+ * @brief Remove a key/value pair
481+ */
482+ public int remove (String key ) {
483+ int index = index (key );
484+ if (index != -1 ) {
485+ removeIndex (index );
486+ }
487+ return index ;
344488 }
345489
346490
347- public void removeIndex (int index ) {
491+ public String removeIndex (int index ) {
492+ String key = keys [index ];
348493 //System.out.println("index is " + which + " and " + keys[which]);
349494 indices .remove (keys [index ]);
350495 for (int i = index ; i < count -1 ; i ++) {
@@ -355,6 +500,7 @@ public void removeIndex(int index) {
355500 count --;
356501 keys [count ] = null ;
357502 values [count ] = 0 ;
503+ return key ;
358504 }
359505
360506
@@ -387,6 +533,9 @@ protected void swap(int a, int b) {
387533 /**
388534 * Sort the keys alphabetically (ignoring case). Uses the value as a
389535 * tie-breaker (only really possible with a key that has a case change).
536+ *
537+ * @webref floatdict:method
538+ * @brief Sort the keys alphabetically
390539 */
391540 public void sortKeys () {
392541 sortImpl (true , false );
@@ -403,6 +552,10 @@ public void sortKeys() {
403552 }
404553
405554
555+ /**
556+ * @webref floatdict:method
557+ * @brief Sort the keys alphabetially in reverse
558+ */
406559 public void sortKeysReverse () {
407560 sortImpl (true , true );
408561// new InternalSort() {
@@ -420,6 +573,9 @@ public void sortKeysReverse() {
420573
421574 /**
422575 * Sort by values in descending order (largest value will be at [0]).
576+ *
577+ * @webref floatdict:method
578+ * @brief Sort by values in ascending order
423579 */
424580 public void sortValues () {
425581 sortImpl (false , false );
@@ -432,6 +588,10 @@ public void sortValues() {
432588 }
433589
434590
591+ /**
592+ * @webref floatdict:method
593+ * @brief Sort by values in descending order
594+ */
435595 public void sortValuesReverse () {
436596 sortImpl (false , true );
437597// new InternalSort() {
0 commit comments