Skip to content

Commit 9cffde6

Browse files
committed
clean up LongList, array() change, bug fixes, remove comments and warnings
1 parent 673cb16 commit 9cffde6

2 files changed

Lines changed: 43 additions & 168 deletions

File tree

core/src/processing/data/LongList.java

Lines changed: 41 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616

1717
/**
18-
* Helper class for a list of ints. Lists are designed to have some of the
19-
* features of ArrayLists, but to maintain the simplicity and efficiency of
20-
* working with arrays.
21-
*
22-
* Functions like sort() and shuffle() always act on the list itself. To get
23-
* a sorted copy, use list.copy().sort().
18+
* Helper class for a list of ints. Lists are designed to have
19+
* some features of ArrayLists, but to maintain the simplicity and
20+
* efficiency of working with arrays.
21+
* Functions such as sort() and shuffle() always act on the list itself.
22+
* To get a sorted copy, use list.copy().sort().
2423
*
2524
* @nowebref
2625
* @see FloatList
@@ -31,6 +30,7 @@ public class LongList implements Iterable<Long> {
3130
protected long[] data;
3231

3332

33+
@SuppressWarnings("unused")
3434
public LongList() {
3535
data = new long[10];
3636
}
@@ -47,7 +47,7 @@ public LongList(int length) {
4747
/**
4848
* @nowebref
4949
*/
50-
public LongList(int[] source) {
50+
public LongList(long[] source) {
5151
count = source.length;
5252
data = new long[count];
5353
System.arraycopy(source, 0, data, 0, count);
@@ -56,10 +56,11 @@ public LongList(int[] source) {
5656

5757
/**
5858
* Construct an IntList from an iterable pile of objects.
59-
* For instance, a float array, an array of strings, who knows).
60-
* Un-parseable or null values will be set to 0.
59+
* For instance, a float array, an array of strings, who knows.
60+
* Un-parsable or null values will be set to 0.
6161
* @nowebref
6262
*/
63+
@SuppressWarnings("unused")
6364
public LongList(Iterable<Object> iter) {
6465
this(10);
6566
for (Object o : iter) {
@@ -77,28 +78,32 @@ public LongList(Iterable<Object> iter) {
7778

7879
/**
7980
* Construct an IntList from a random pile of objects.
80-
* Un-parseable or null values will be set to zero.
81+
* Un-parsable or null values will be set to zero.
8182
*/
83+
@SuppressWarnings("unused")
8284
public LongList(Object... items) {
83-
final int missingValue = 0; // nuts, can't be last/final/second arg
85+
final long missingValue = 0; // nuts, can't be last/final/second arg
8486

8587
count = items.length;
8688
data = new long[count];
8789
int index = 0;
8890
for (Object o : items) {
89-
int value = missingValue;
91+
long value = missingValue;
9092
if (o != null) {
9193
if (o instanceof Number) {
92-
value = ((Number) o).intValue();
94+
value = ((Number) o).longValue();
9395
} else {
94-
value = PApplet.parseInt(o.toString().trim(), missingValue);
96+
try {
97+
value = Long.parseLong(o.toString().trim());
98+
} catch (NumberFormatException ignored) { }
9599
}
96100
}
97101
data[index++] = value;
98102
}
99103
}
100104

101105

106+
@SuppressWarnings("unused")
102107
static public LongList fromRange(int stop) {
103108
return fromRange(0, stop);
104109
}
@@ -240,6 +245,7 @@ public long remove(int index) {
240245

241246
// Remove the first instance of a particular value,
242247
// and return the index at which it was found.
248+
@SuppressWarnings("unused")
243249
public int removeValue(int value) {
244250
int index = index(value);
245251
if (index != -1) {
@@ -252,6 +258,7 @@ public int removeValue(int value) {
252258

253259
// Remove all instances of a particular value,
254260
// and return the number of values found and removed
261+
@SuppressWarnings("unused")
255262
public int removeValues(int value) {
256263
int ii = 0;
257264
for (int i = 0; i < count; i++) {
@@ -294,6 +301,7 @@ public void append(LongList list) {
294301

295302

296303
/** Add this value, but only if it's not already in the list. */
304+
@SuppressWarnings("unused")
297305
public void appendUnique(int value) {
298306
if (!hasValue(value)) {
299307
append(value);
@@ -370,60 +378,8 @@ public void insert(int index, LongList list) {
370378
}
371379

372380

373-
// below are aborted attempts at more optimized versions of the code
374-
// that are harder to read and debug...
375-
376-
// if (index + values.length >= count) {
377-
// // We're past the current 'count', check to see if we're still allocated
378-
// // index 9, data.length = 10, values.length = 1
379-
// if (index + values.length < data.length) {
380-
// // There's still room for these entries, even though it's past 'count'.
381-
// // First clear out the entries leading up to it, however.
382-
// for (int i = count; i < index; i++) {
383-
// data[i] = 0;
384-
// }
385-
// data[index] =
386-
// }
387-
// if (index >= data.length) {
388-
// int length = index + values.length;
389-
// int[] temp = new int[length];
390-
// System.arraycopy(data, 0, temp, 0, count);
391-
// System.arraycopy(values, 0, temp, index, values.length);
392-
// data = temp;
393-
// count = data.length;
394-
// } else {
395-
//
396-
// }
397-
//
398-
// } else if (count == data.length) {
399-
// int[] temp = new int[count << 1];
400-
// System.arraycopy(data, 0, temp, 0, index);
401-
// temp[index] = value;
402-
// System.arraycopy(data, index, temp, index+1, count - index);
403-
// data = temp;
404-
//
405-
// } else {
406-
// // data[] has room to grow
407-
// // for() loop believed to be faster than System.arraycopy over itself
408-
// for (int i = count; i > index; --i) {
409-
// data[i] = data[i-1];
410-
// }
411-
// data[index] = value;
412-
// count++;
413-
// }
414-
415-
416381
/** Return the first index of a particular value. */
417382
public int index(int what) {
418-
/*
419-
if (indexCache != null) {
420-
try {
421-
return indexCache.get(what);
422-
} catch (Exception e) { // not there
423-
return -1;
424-
}
425-
}
426-
*/
427383
for (int i = 0; i < count; i++) {
428384
if (data[i] == what) {
429385
return i;
@@ -433,26 +389,13 @@ public int index(int what) {
433389
}
434390

435391

436-
// !!! TODO this is not yet correct, because it's not being reset when
437-
// the rest of the entries are changed
438-
// protected void cacheIndices() {
439-
// indexCache = new HashMap<Integer, Integer>();
440-
// for (int i = 0; i < count; i++) {
441-
// indexCache.put(data[i], i);
442-
// }
443-
// }
444-
445392
/**
446393
* Check if a number is a part of the list
447394
*
448395
* @webref intlist:method
449396
* @webBrief Check if a number is a part of the list
450397
*/
451398
public boolean hasValue(int value) {
452-
// if (indexCache == null) {
453-
// cacheIndices();
454-
// }
455-
// return index(what) != -1;
456399
for (int i = 0; i < count; i++) {
457400
if (data[i] == value) {
458401
return true;
@@ -567,6 +510,7 @@ public long min() {
567510

568511
// returns the index of the minimum value.
569512
// if there are ties, it returns the first one found.
513+
@SuppressWarnings("unused")
570514
public int minIndex() {
571515
checkMinMax("minIndex");
572516
long value = data[0];
@@ -674,23 +618,6 @@ public void swap(int a, int b) {
674618
}
675619

676620

677-
// use insert()
678-
// public void splice(int index, int value) {
679-
// }
680-
681-
682-
// public void subset(int start) {
683-
// subset(start, count - start);
684-
// }
685-
//
686-
//
687-
// public void subset(int start, int num) {
688-
// for (int i = 0; i < num; i++) {
689-
// data[i] = data[i+start];
690-
// }
691-
// count = num;
692-
// }
693-
694621
/**
695622
* Reverse the order of the list elements
696623
*
@@ -715,6 +642,7 @@ public void reverse() {
715642
* @webref intlist:method
716643
* @webBrief Randomize the order of the list elements
717644
*/
645+
@SuppressWarnings("unused")
718646
public void shuffle() {
719647
Random r = new Random();
720648
int num = count;
@@ -732,6 +660,7 @@ public void shuffle() {
732660
* Randomize the list order using the random() function from the specified
733661
* sketch, allowing shuffle() to use its current randomSeed() setting.
734662
*/
663+
@SuppressWarnings("unused")
735664
public void shuffle(PApplet sketch) {
736665
int num = count;
737666
while (num > 1) {
@@ -781,8 +710,7 @@ public long[] values() {
781710

782711
@Override
783712
public Iterator<Long> iterator() {
784-
// public Iterator<Integer> valueIterator() {
785-
return new Iterator<Long>() {
713+
return new Iterator<>() {
786714
int index = -1;
787715

788716
public void remove() {
@@ -808,78 +736,35 @@ public boolean hasNext() {
808736
* @webref intlist:method
809737
* @webBrief Create a new array with a copy of all the values
810738
*/
811-
public int[] array() {
812-
return array(null);
739+
public long[] toArray() {
740+
return toArray(null);
813741
}
814742

815743

816744
/**
817-
* Copy values into the specified array. If the specified array is null or
818-
* not the same size, a new array will be allocated.
819-
* @param array
745+
* Copy values into the specified array. If the specified array is
746+
* null or not the same size, a new array will be allocated.
820747
*/
821-
public int[] array(int[] array) {
748+
public long[] toArray(long[] array) {
822749
if (array == null || array.length != count) {
823-
array = new int[count];
750+
array = new long[count];
824751
}
825752
System.arraycopy(data, 0, array, 0, count);
826753
return array;
827754
}
828755

829756

830-
// public int[] toIntArray() {
831-
// int[] outgoing = new int[count];
832-
// for (int i = 0; i < count; i++) {
833-
// outgoing[i] = (int) data[i];
834-
// }
835-
// return outgoing;
836-
// }
837-
838-
839-
// public long[] toLongArray() {
840-
// long[] outgoing = new long[count];
841-
// for (int i = 0; i < count; i++) {
842-
// outgoing[i] = (long) data[i];
843-
// }
844-
// return outgoing;
845-
// }
846-
847-
848-
// public float[] toFloatArray() {
849-
// float[] outgoing = new float[count];
850-
// System.arraycopy(data, 0, outgoing, 0, count);
851-
// return outgoing;
852-
// }
853-
854-
855-
// public double[] toDoubleArray() {
856-
// double[] outgoing = new double[count];
857-
// for (int i = 0; i < count; i++) {
858-
// outgoing[i] = data[i];
859-
// }
860-
// return outgoing;
861-
// }
862-
863-
864-
// public String[] toStringArray() {
865-
// String[] outgoing = new String[count];
866-
// for (int i = 0; i < count; i++) {
867-
// outgoing[i] = String.valueOf(data[i]);
868-
// }
869-
// return outgoing;
870-
// }
871-
872-
873757
/**
874-
* Returns a normalized version of this array. Called getPercent() for
875-
* consistency with the Dict classes. It's a getter method because it needs
876-
* to returns a new list (because IntList/Dict can't do percentages or
877-
* normalization in place on int values).
758+
* Returns a normalized version of this array. Called getPercent()
759+
* for consistency with the Dict classes. It's a get method because
760+
* it needs to return a new list (because IntList/Dict can't do
761+
* percentages or normalization in place on int values).
878762
*/
763+
@SuppressWarnings("unused")
879764
public FloatList getPercent() {
880765
double sum = 0;
881-
for (float value : array()) {
882-
sum += value;
766+
for (int i = 0; i < count; i++) {
767+
sum += data[i];
883768
}
884769
FloatList outgoing = new FloatList(count);
885770
for (int i = 0; i < count; i++) {
@@ -890,26 +775,14 @@ public FloatList getPercent() {
890775
}
891776

892777

893-
// /**
894-
// * Count the number of times each entry is found in this list.
895-
// * Converts each entry to a String so it can be used as a key.
896-
// */
897-
// public IntDict getTally() {
898-
// IntDict outgoing = new IntDict();
899-
// for (int i = 0; i < count; i++) {
900-
// outgoing.increment(String.valueOf(data[i]));
901-
// }
902-
// return outgoing;
903-
// }
904-
905-
778+
@SuppressWarnings("unused")
906779
public LongList getSubset(int start) {
907780
return getSubset(start, count - start);
908781
}
909782

910783

911784
public LongList getSubset(int start, int num) {
912-
int[] subset = new int[num];
785+
long[] subset = new long[num];
913786
System.arraycopy(data, start, subset, 0, num);
914787
return new LongList(subset);
915788
}

core/todo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ X add toXxxxList() methods to JSONArray
88
X rename getXxxArray() to toXxxArray() in JSONArray for consistency
99
X deprecate the old versions
1010
X add random() method to XxxList classes to return a random value from the list
11+
X use toArray() instead of array() in LongList
12+
X did not deprecate old version because it was actually broken
1113

1214

1315
_ concurrent StringDict et al

0 commit comments

Comments
 (0)