Skip to content

Commit 07168f1

Browse files
committed
Update processing Lists and Dict
1 parent 479f1a9 commit 07168f1

File tree

5 files changed

+281
-142
lines changed

5 files changed

+281
-142
lines changed

src/main/java/processing/data/DoubleDict.java

+72-43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package processing.data;
22

3-
import java.io.*;
3+
//import java.io.*;
4+
import java.io.BufferedReader;
5+
import java.io.File;
6+
import java.io.PrintWriter;
47
import java.util.HashMap;
58
import java.util.Iterator;
69
import java.util.Map;
@@ -36,9 +39,10 @@ public DoubleDict() {
3639

3740

3841
/**
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.
4144
*
45+
* @param length
4246
* @nowebref
4347
*/
4448
public DoubleDict(int length) {
@@ -52,26 +56,29 @@ public DoubleDict(int length) {
5256
* Read a set of entries from a Reader that has each key/value pair on
5357
* a single line, separated by a tab.
5458
*
59+
* @param reader
5560
* @nowebref
5661
*/
5762
public DoubleDict(BufferedReader reader) {
5863
String[] lines = PApplet.loadStrings(reader);
5964
keys = new String[lines.length];
6065
values = new double[lines.length];
6166

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+
}
6975
}
70-
}
7176
}
7277

7378

7479
/**
80+
* @param keys
81+
* @param values
7582
* @nowebref
7683
*/
7784
public DoubleDict(String[] keys, double[] values) {
@@ -95,6 +102,7 @@ public DoubleDict(String[] keys, double[] values) {
95102
* { "key2", 2 }
96103
* });
97104
* </pre>
105+
* @param pairs
98106
*/
99107
public DoubleDict(Object[][] pairs) {
100108
count = pairs.length;
@@ -123,6 +131,7 @@ public DoubleDict(Map<String, Double> incoming) {
123131

124132

125133
/**
134+
* @return
126135
* @webref doubledict:method
127136
* @brief Returns the number of key/value pairs
128137
*/
@@ -132,8 +141,8 @@ public int size() {
132141

133142

134143
/**
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
137146
*/
138147
public void resize(int length) {
139148
if (length == count) return;
@@ -191,30 +200,28 @@ public class Entry {
191200

192201

193202
public Iterable<Entry> entries() {
194-
return new Iterable<Entry>() {
195-
196-
public Iterator<Entry> iterator() {
197-
return entryIterator();
198-
}
199-
};
203+
return this::entryIterator;
200204
}
201205

202206

203207
public Iterator<Entry> entryIterator() {
204208
return new Iterator<Entry>() {
205209
int index = -1;
206210

211+
@Override
207212
public void remove() {
208213
removeIndex(index);
209214
index--;
210215
}
211216

217+
@Override
212218
public Entry next() {
213219
++index;
214220
Entry e = new Entry(keys[index], values[index]);
215221
return e;
216222
}
217223

224+
@Override
218225
public boolean hasNext() {
219226
return index+1 < size();
220227
}
@@ -239,13 +246,7 @@ protected void crop() {
239246

240247

241248
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;
249250
}
250251

251252

@@ -254,15 +255,18 @@ public Iterator<String> keyIterator() {
254255
return new Iterator<String>() {
255256
int index = -1;
256257

258+
@Override
257259
public void remove() {
258260
removeIndex(index);
259261
index--;
260262
}
261263

264+
@Override
262265
public String next() {
263266
return key(++index);
264267
}
265268

269+
@Override
266270
public boolean hasNext() {
267271
return index+1 < size();
268272
}
@@ -271,8 +275,9 @@ public boolean hasNext() {
271275

272276

273277
/**
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.
275279
*
280+
* @return
276281
* @webref doubledict:method
277282
* @brief Return a copy of the internal keys array
278283
*/
@@ -297,33 +302,31 @@ public double value(int index) {
297302

298303

299304
/**
305+
* @return
300306
* @webref doubledict:method
301307
* @brief Return the internal array being used to store the values
302308
*/
303309
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();
311311
}
312312

313313

314314
public Iterator<Double> valueIterator() {
315315
return new Iterator<Double>() {
316316
int index = -1;
317317

318+
@Override
318319
public void remove() {
319320
removeIndex(index);
320321
index--;
321322
}
322323

324+
@Override
323325
public Double next() {
324326
return value(++index);
325327
}
326328

329+
@Override
327330
public boolean hasNext() {
328331
return index+1 < size();
329332
}
@@ -334,6 +337,7 @@ public boolean hasNext() {
334337
/**
335338
* Create a new array and copy each of the values into it.
336339
*
340+
* @return
337341
* @webref doubledict:method
338342
* @brief Create a new array and copy each of the values into it
339343
*/
@@ -345,8 +349,10 @@ public double[] valueArray() {
345349

346350
/**
347351
* 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
349353
* size as the number of values, a new array will be allocated and returned.
354+
* @param array
355+
* @return
350356
*/
351357
public double[] valueArray(double[] array) {
352358
if (array == null || array.length != size()) {
@@ -360,6 +366,8 @@ public double[] valueArray(double[] array) {
360366
/**
361367
* Return a value for the specified key.
362368
*
369+
* @param key
370+
* @return
363371
* @webref doubledict:method
364372
* @brief Return a value for the specified key
365373
*/
@@ -382,6 +390,8 @@ public double get(String key, double alternate) {
382390

383391

384392
/**
393+
* @param key
394+
* @param amount
385395
* @webref doubledict:method
386396
* @brief Create a new key/value pair or change the value of one
387397
*/
@@ -405,6 +415,8 @@ public void setIndex(int index, String key, double value) {
405415

406416

407417
/**
418+
* @param key
419+
* @return
408420
* @webref doubledict:method
409421
* @brief Check if a key is a part of the data structure
410422
*/
@@ -414,6 +426,8 @@ public boolean hasKey(String key) {
414426

415427

416428
/**
429+
* @param key
430+
* @param amount
417431
* @webref doubledict:method
418432
* @brief Add to a value
419433
*/
@@ -428,6 +442,8 @@ public void add(String key, double amount) {
428442

429443

430444
/**
445+
* @param key
446+
* @param amount
431447
* @webref doubledict:method
432448
* @brief Subtract from a value
433449
*/
@@ -437,6 +453,8 @@ public void sub(String key, double amount) {
437453

438454

439455
/**
456+
* @param key
457+
* @param amount
440458
* @webref doubledict:method
441459
* @brief Multiply a value
442460
*/
@@ -449,6 +467,8 @@ public void mult(String key, double amount) {
449467

450468

451469
/**
470+
* @param key
471+
* @param amount
452472
* @webref doubledict:method
453473
* @brief Divide a value
454474
*/
@@ -471,6 +491,7 @@ private void checkMinMax(String functionName) {
471491

472492

473493
/**
494+
* @return
474495
* @webref doublelist:method
475496
* @brief Return the smallest value
476497
*/
@@ -525,6 +546,7 @@ public double minValue() {
525546

526547

527548
/**
549+
* @return
528550
* @webref doublelist:method
529551
* @brief Return the largest value
530552
*/
@@ -558,7 +580,8 @@ public int maxIndex() {
558580
}
559581

560582

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 */
562585
public String maxKey() {
563586
//checkMinMax("maxKey");
564587
int index = maxIndex();
@@ -569,7 +592,8 @@ public String maxKey() {
569592
}
570593

571594

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 */
573597
public double maxValue() {
574598
//checkMinMax("maxValue");
575599
int index = maxIndex();
@@ -591,7 +615,7 @@ public double sum() {
591615

592616
public int index(String what) {
593617
Integer found = indices.get(what);
594-
return (found == null) ? -1 : found.intValue();
618+
return (found == null) ? -1 : found;
595619
}
596620

597621

@@ -600,14 +624,16 @@ protected void create(String what, double much) {
600624
keys = PApplet.expand(keys);
601625
values = PApplet.expand(values);
602626
}
603-
indices.put(what, Integer.valueOf(count));
627+
indices.put(what, count);
604628
keys[count] = what;
605629
values[count] = much;
606630
count++;
607631
}
608632

609633

610634
/**
635+
* @param key
636+
* @return
611637
* @webref doubledict:method
612638
* @brief Remove a key/value pair
613639
*/
@@ -789,7 +815,8 @@ public DoubleDict getPercent() {
789815
}
790816

791817

792-
/** Returns a duplicate copy of this object. */
818+
/** Returns a duplicate copy of this object.
819+
* @return */
793820
public DoubleDict copy() {
794821
DoubleDict outgoing = new DoubleDict(count);
795822
System.arraycopy(keys, 0, outgoing.keys, 0, count);
@@ -811,11 +838,12 @@ public void print() {
811838

812839
/**
813840
* Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
841+
* @param file
814842
*/
815843
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+
}
819847
}
820848

821849

@@ -833,6 +861,7 @@ public void write(PrintWriter writer) {
833861

834862
/**
835863
* Return this dictionary as a String in JSON format.
864+
* @return
836865
*/
837866
public String toJSON() {
838867
StringList items = new StringList();

0 commit comments

Comments
 (0)