1
1
package processing .data ;
2
2
3
- import java .io .*;
3
+ //import java.io.*;
4
+ import java .io .BufferedReader ;
5
+ import java .io .File ;
6
+ import java .io .PrintWriter ;
4
7
import java .util .HashMap ;
5
8
import java .util .Iterator ;
6
9
import java .util .Map ;
@@ -36,9 +39,10 @@ public DoubleDict() {
36
39
37
40
38
41
/**
39
- * Create a new lookup with a specific size. This is more efficient than not
40
- * specifying a size. Use it when you know the rough size of the thing you're creating.
42
+ * Create a new lookup with a specific size.This is more efficient than not
43
+ specifying a size. Use it when you know the rough size of the thing you're creating.
41
44
*
45
+ * @param length
42
46
* @nowebref
43
47
*/
44
48
public DoubleDict (int length ) {
@@ -52,26 +56,29 @@ public DoubleDict(int length) {
52
56
* Read a set of entries from a Reader that has each key/value pair on
53
57
* a single line, separated by a tab.
54
58
*
59
+ * @param reader
55
60
* @nowebref
56
61
*/
57
62
public DoubleDict (BufferedReader reader ) {
58
63
String [] lines = PApplet .loadStrings (reader );
59
64
keys = new String [lines .length ];
60
65
values = new double [lines .length ];
61
66
62
- for (int i = 0 ; i < lines .length ; i ++) {
63
- String [] pieces = PApplet .split (lines [i ], '\t' );
64
- if (pieces .length == 2 ) {
65
- keys [count ] = pieces [0 ];
66
- values [count ] = PApplet .parseFloat (pieces [1 ]);
67
- indices .put (pieces [0 ], count );
68
- count ++;
67
+ for (String line : lines ) {
68
+ String [] pieces = PApplet .split (line , '\t' );
69
+ if (pieces .length == 2 ) {
70
+ keys [count ] = pieces [0 ];
71
+ values [count ] = PApplet .parseFloat (pieces [1 ]);
72
+ indices .put (pieces [0 ], count );
73
+ count ++;
74
+ }
69
75
}
70
- }
71
76
}
72
77
73
78
74
79
/**
80
+ * @param keys
81
+ * @param values
75
82
* @nowebref
76
83
*/
77
84
public DoubleDict (String [] keys , double [] values ) {
@@ -95,6 +102,7 @@ public DoubleDict(String[] keys, double[] values) {
95
102
* { "key2", 2 }
96
103
* });
97
104
* </pre>
105
+ * @param pairs
98
106
*/
99
107
public DoubleDict (Object [][] pairs ) {
100
108
count = pairs .length ;
@@ -123,6 +131,7 @@ public DoubleDict(Map<String, Double> incoming) {
123
131
124
132
125
133
/**
134
+ * @return
126
135
* @webref doubledict:method
127
136
* @brief Returns the number of key/value pairs
128
137
*/
@@ -132,8 +141,8 @@ public int size() {
132
141
133
142
134
143
/**
135
- * Resize the internal data, this can only be used to shrink the list.
136
- * Helpful for situations like sorting and then grabbing the top 50 entries.
144
+ * Resize the internal data, this can only be used to shrink the list.Helpful for situations like sorting and then grabbing the top 50 entries.
145
+ * @param length
137
146
*/
138
147
public void resize (int length ) {
139
148
if (length == count ) return ;
@@ -191,30 +200,28 @@ public class Entry {
191
200
192
201
193
202
public Iterable <Entry > entries () {
194
- return new Iterable <Entry >() {
195
-
196
- public Iterator <Entry > iterator () {
197
- return entryIterator ();
198
- }
199
- };
203
+ return this ::entryIterator ;
200
204
}
201
205
202
206
203
207
public Iterator <Entry > entryIterator () {
204
208
return new Iterator <Entry >() {
205
209
int index = -1 ;
206
210
211
+ @ Override
207
212
public void remove () {
208
213
removeIndex (index );
209
214
index --;
210
215
}
211
216
217
+ @ Override
212
218
public Entry next () {
213
219
++index ;
214
220
Entry e = new Entry (keys [index ], values [index ]);
215
221
return e ;
216
222
}
217
223
224
+ @ Override
218
225
public boolean hasNext () {
219
226
return index +1 < size ();
220
227
}
@@ -239,13 +246,7 @@ protected void crop() {
239
246
240
247
241
248
public Iterable <String > keys () {
242
- return new Iterable <String >() {
243
-
244
- @ Override
245
- public Iterator <String > iterator () {
246
- return keyIterator ();
247
- }
248
- };
249
+ return this ::keyIterator ;
249
250
}
250
251
251
252
@@ -254,15 +255,18 @@ public Iterator<String> keyIterator() {
254
255
return new Iterator <String >() {
255
256
int index = -1 ;
256
257
258
+ @ Override
257
259
public void remove () {
258
260
removeIndex (index );
259
261
index --;
260
262
}
261
263
264
+ @ Override
262
265
public String next () {
263
266
return key (++index );
264
267
}
265
268
269
+ @ Override
266
270
public boolean hasNext () {
267
271
return index +1 < size ();
268
272
}
@@ -271,8 +275,9 @@ public boolean hasNext() {
271
275
272
276
273
277
/**
274
- * Return a copy of the internal keys array. This array can be modified.
278
+ * Return a copy of the internal keys array.This array can be modified.
275
279
*
280
+ * @return
276
281
* @webref doubledict:method
277
282
* @brief Return a copy of the internal keys array
278
283
*/
@@ -297,33 +302,31 @@ public double value(int index) {
297
302
298
303
299
304
/**
305
+ * @return
300
306
* @webref doubledict:method
301
307
* @brief Return the internal array being used to store the values
302
308
*/
303
309
public Iterable <Double > values () {
304
- return new Iterable <Double >() {
305
-
306
- @ Override
307
- public Iterator <Double > iterator () {
308
- return valueIterator ();
309
- }
310
- };
310
+ return () -> valueIterator ();
311
311
}
312
312
313
313
314
314
public Iterator <Double > valueIterator () {
315
315
return new Iterator <Double >() {
316
316
int index = -1 ;
317
317
318
+ @ Override
318
319
public void remove () {
319
320
removeIndex (index );
320
321
index --;
321
322
}
322
323
324
+ @ Override
323
325
public Double next () {
324
326
return value (++index );
325
327
}
326
328
329
+ @ Override
327
330
public boolean hasNext () {
328
331
return index +1 < size ();
329
332
}
@@ -334,6 +337,7 @@ public boolean hasNext() {
334
337
/**
335
338
* Create a new array and copy each of the values into it.
336
339
*
340
+ * @return
337
341
* @webref doubledict:method
338
342
* @brief Create a new array and copy each of the values into it
339
343
*/
@@ -345,8 +349,10 @@ public double[] valueArray() {
345
349
346
350
/**
347
351
* Fill an already-allocated array with the values (more efficient than
348
- * creating a new array each time). If 'array' is null, or not the same
352
+ * creating a new array each time).If 'array' is null, or not the same
349
353
* size as the number of values, a new array will be allocated and returned.
354
+ * @param array
355
+ * @return
350
356
*/
351
357
public double [] valueArray (double [] array ) {
352
358
if (array == null || array .length != size ()) {
@@ -360,6 +366,8 @@ public double[] valueArray(double[] array) {
360
366
/**
361
367
* Return a value for the specified key.
362
368
*
369
+ * @param key
370
+ * @return
363
371
* @webref doubledict:method
364
372
* @brief Return a value for the specified key
365
373
*/
@@ -382,6 +390,8 @@ public double get(String key, double alternate) {
382
390
383
391
384
392
/**
393
+ * @param key
394
+ * @param amount
385
395
* @webref doubledict:method
386
396
* @brief Create a new key/value pair or change the value of one
387
397
*/
@@ -405,6 +415,8 @@ public void setIndex(int index, String key, double value) {
405
415
406
416
407
417
/**
418
+ * @param key
419
+ * @return
408
420
* @webref doubledict:method
409
421
* @brief Check if a key is a part of the data structure
410
422
*/
@@ -414,6 +426,8 @@ public boolean hasKey(String key) {
414
426
415
427
416
428
/**
429
+ * @param key
430
+ * @param amount
417
431
* @webref doubledict:method
418
432
* @brief Add to a value
419
433
*/
@@ -428,6 +442,8 @@ public void add(String key, double amount) {
428
442
429
443
430
444
/**
445
+ * @param key
446
+ * @param amount
431
447
* @webref doubledict:method
432
448
* @brief Subtract from a value
433
449
*/
@@ -437,6 +453,8 @@ public void sub(String key, double amount) {
437
453
438
454
439
455
/**
456
+ * @param key
457
+ * @param amount
440
458
* @webref doubledict:method
441
459
* @brief Multiply a value
442
460
*/
@@ -449,6 +467,8 @@ public void mult(String key, double amount) {
449
467
450
468
451
469
/**
470
+ * @param key
471
+ * @param amount
452
472
* @webref doubledict:method
453
473
* @brief Divide a value
454
474
*/
@@ -471,6 +491,7 @@ private void checkMinMax(String functionName) {
471
491
472
492
473
493
/**
494
+ * @return
474
495
* @webref doublelist:method
475
496
* @brief Return the smallest value
476
497
*/
@@ -525,6 +546,7 @@ public double minValue() {
525
546
526
547
527
548
/**
549
+ * @return
528
550
* @webref doublelist:method
529
551
* @brief Return the largest value
530
552
*/
@@ -558,7 +580,8 @@ public int maxIndex() {
558
580
}
559
581
560
582
561
- /** The key for a max value; null if empty or everything is NaN (no max). */
583
+ /** The key for a max value; null if empty or everything is NaN (no max).
584
+ * @return */
562
585
public String maxKey () {
563
586
//checkMinMax("maxKey");
564
587
int index = maxIndex ();
@@ -569,7 +592,8 @@ public String maxKey() {
569
592
}
570
593
571
594
572
- /** The max value. (Or NaN if no entries or they're all NaN.) */
595
+ /** * The max value.(Or NaN if no entries or they're all NaN.)
596
+ * @return */
573
597
public double maxValue () {
574
598
//checkMinMax("maxValue");
575
599
int index = maxIndex ();
@@ -591,7 +615,7 @@ public double sum() {
591
615
592
616
public int index (String what ) {
593
617
Integer found = indices .get (what );
594
- return (found == null ) ? -1 : found . intValue () ;
618
+ return (found == null ) ? -1 : found ;
595
619
}
596
620
597
621
@@ -600,14 +624,16 @@ protected void create(String what, double much) {
600
624
keys = PApplet .expand (keys );
601
625
values = PApplet .expand (values );
602
626
}
603
- indices .put (what , Integer . valueOf ( count ) );
627
+ indices .put (what , count );
604
628
keys [count ] = what ;
605
629
values [count ] = much ;
606
630
count ++;
607
631
}
608
632
609
633
610
634
/**
635
+ * @param key
636
+ * @return
611
637
* @webref doubledict:method
612
638
* @brief Remove a key/value pair
613
639
*/
@@ -789,7 +815,8 @@ public DoubleDict getPercent() {
789
815
}
790
816
791
817
792
- /** Returns a duplicate copy of this object. */
818
+ /** Returns a duplicate copy of this object.
819
+ * @return */
793
820
public DoubleDict copy () {
794
821
DoubleDict outgoing = new DoubleDict (count );
795
822
System .arraycopy (keys , 0 , outgoing .keys , 0 , count );
@@ -811,11 +838,12 @@ public void print() {
811
838
812
839
/**
813
840
* Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
841
+ * @param file
814
842
*/
815
843
public void save (File file ) {
816
- PrintWriter writer = PApplet .createWriter (file );
817
- write (writer );
818
- writer . close ();
844
+ try ( PrintWriter writer = PApplet .createWriter (file )) {
845
+ write (writer );
846
+ }
819
847
}
820
848
821
849
@@ -833,6 +861,7 @@ public void write(PrintWriter writer) {
833
861
834
862
/**
835
863
* Return this dictionary as a String in JSON format.
864
+ * @return
836
865
*/
837
866
public String toJSON () {
838
867
StringList items = new StringList ();
0 commit comments