@@ -50,13 +50,15 @@ public FloatList() {
50
50
data = new float [10 ];
51
51
}
52
52
53
+
53
54
/**
54
55
* @nowebref
55
56
*/
56
57
public FloatList (int length ) {
57
58
data = new float [length ];
58
59
}
59
60
61
+
60
62
/**
61
63
* @nowebref
62
64
*/
@@ -66,13 +68,49 @@ public FloatList(float[] list) {
66
68
System .arraycopy (list , 0 , data , 0 , count );
67
69
}
68
70
71
+
69
72
/**
73
+ * Construct an FloatList from an iterable pile of objects.
74
+ * For instance, a float array, an array of strings, who knows).
75
+ * Un-parseable or null values will be set to NaN.
70
76
* @nowebref
71
77
*/
72
- public FloatList (Iterable <Float > iter ) {
78
+ public FloatList (Iterable <Object > iter ) {
73
79
this (10 );
74
- for (float v : iter ) {
75
- append (v );
80
+ for (Object o : iter ) {
81
+ if (o == null ) {
82
+ append (Float .NaN );
83
+ } else if (o instanceof Number ) {
84
+ append (((Number ) o ).floatValue ());
85
+ } else {
86
+ append (PApplet .parseFloat (o .toString ().trim ()));
87
+ }
88
+ }
89
+ crop ();
90
+ }
91
+
92
+
93
+ /**
94
+ * Construct an FloatList from a random pile of objects.
95
+ * Un-parseable or null values will be set to NaN.
96
+ */
97
+ public FloatList (Object ... items ) {
98
+ // nuts, no good way to pass missingValue to this fn (varargs must be last)
99
+ final float missingValue = Float .NaN ;
100
+
101
+ count = items .length ;
102
+ data = new float [count ];
103
+ int index = 0 ;
104
+ for (Object o : items ) {
105
+ float value = missingValue ;
106
+ if (o != null ) {
107
+ if (o instanceof Number ) {
108
+ value = ((Number ) o ).floatValue ();
109
+ } else {
110
+ value = PApplet .parseFloat (o .toString ().trim (), missingValue );
111
+ }
112
+ }
113
+ data [index ++] = value ;
76
114
}
77
115
}
78
116
@@ -132,6 +170,9 @@ public void clear() {
132
170
* @brief Get an entry at a particular index
133
171
*/
134
172
public float get (int index ) {
173
+ if (index >= count ) {
174
+ throw new ArrayIndexOutOfBoundsException (index );
175
+ }
135
176
return data [index ];
136
177
}
137
178
@@ -156,6 +197,22 @@ public void set(int index, float what) {
156
197
}
157
198
158
199
200
+ /** Just an alias for append(), but matches pop() */
201
+ public void push (float value ) {
202
+ append (value );
203
+ }
204
+
205
+
206
+ public float pop () {
207
+ if (count == 0 ) {
208
+ throw new RuntimeException ("Can't call pop() on an empty list" );
209
+ }
210
+ float value = get (count -1 );
211
+ count --;
212
+ return value ;
213
+ }
214
+
215
+
159
216
/**
160
217
* Remove an element from the specified index.
161
218
*
@@ -288,6 +345,14 @@ public void append(FloatList list) {
288
345
}
289
346
290
347
348
+ /** Add this value, but only if it's not already in the list. */
349
+ public void appendUnique (float value ) {
350
+ if (!hasValue (value )) {
351
+ append (value );
352
+ }
353
+ }
354
+
355
+
291
356
// public void insert(int index, int value) {
292
357
// if (index+1 > count) {
293
358
// if (index+1 < data.length) {
@@ -318,8 +383,13 @@ public void append(FloatList list) {
318
383
// }
319
384
320
385
386
+ public void insert (int index , float value ) {
387
+ insert (index , new float [] { value });
388
+ }
389
+
390
+
321
391
// same as splice
322
- public void insert (int index , int [] values ) {
392
+ public void insert (int index , float [] values ) {
323
393
if (index < 0 ) {
324
394
throw new IllegalArgumentException ("insert() index cannot be negative: it was " + index );
325
395
}
@@ -347,7 +417,7 @@ public void insert(int index, int[] values) {
347
417
}
348
418
349
419
350
- public void insert (int index , IntList list ) {
420
+ public void insert (int index , FloatList list ) {
351
421
insert (index , list .values ());
352
422
}
353
423
@@ -437,12 +507,23 @@ public boolean hasValue(float value) {
437
507
}
438
508
439
509
510
+ private void boundsProblem (int index , String method ) {
511
+ final String msg = String .format ("The list size is %d. " +
512
+ "You cannot %s() to element %d." , count , method , index );
513
+ throw new ArrayIndexOutOfBoundsException (msg );
514
+ }
515
+
516
+
440
517
/**
441
518
* @webref floatlist:method
442
519
* @brief Add to a value
443
520
*/
444
521
public void add (int index , float amount ) {
445
- data [index ] += amount ;
522
+ if (index < count ) {
523
+ data [index ] += amount ;
524
+ } else {
525
+ boundsProblem (index , "add" );
526
+ }
446
527
}
447
528
448
529
@@ -451,7 +532,11 @@ public void add(int index, float amount) {
451
532
* @brief Subtract from a value
452
533
*/
453
534
public void sub (int index , float amount ) {
454
- data [index ] -= amount ;
535
+ if (index < count ) {
536
+ data [index ] -= amount ;
537
+ } else {
538
+ boundsProblem (index , "sub" );
539
+ }
455
540
}
456
541
457
542
@@ -460,7 +545,11 @@ public void sub(int index, float amount) {
460
545
* @brief Multiply a value
461
546
*/
462
547
public void mult (int index , float amount ) {
463
- data [index ] *= amount ;
548
+ if (index < count ) {
549
+ data [index ] *= amount ;
550
+ } else {
551
+ boundsProblem (index , "mult" );
552
+ }
464
553
}
465
554
466
555
@@ -469,7 +558,11 @@ public void mult(int index, float amount) {
469
558
* @brief Divide a value
470
559
*/
471
560
public void div (int index , float amount ) {
472
- data [index ] /= amount ;
561
+ if (index < count ) {
562
+ data [index ] /= amount ;
563
+ } else {
564
+ boundsProblem (index , "div" );
565
+ }
473
566
}
474
567
475
568
@@ -585,7 +678,27 @@ public void sortReverse() {
585
678
new Sort () {
586
679
@ Override
587
680
public int size () {
588
- return count ;
681
+ // if empty, don't even mess with the NaN check, it'll AIOOBE
682
+ if (count == 0 ) {
683
+ return 0 ;
684
+ }
685
+ // move NaN values to the end of the list and don't sort them
686
+ int right = count - 1 ;
687
+ while (data [right ] != data [right ]) {
688
+ right --;
689
+ if (right == -1 ) { // all values are NaN
690
+ return 0 ;
691
+ }
692
+ }
693
+ for (int i = right ; i >= 0 ; --i ) {
694
+ float v = data [i ];
695
+ if (v != v ) {
696
+ data [i ] = data [right ];
697
+ data [right ] = v ;
698
+ --right ;
699
+ }
700
+ }
701
+ return right + 1 ;
589
702
}
590
703
591
704
@ Override
@@ -623,7 +736,7 @@ public void swap(int a, int b) {
623
736
624
737
/**
625
738
* @webref floatlist:method
626
- * @brief Reverse sort, orders values by first digit
739
+ * @brief Reverse the order of the list elements
627
740
*/
628
741
public void reverse () {
629
742
int ii = count - 1 ;
@@ -691,6 +804,7 @@ public float[] values() {
691
804
692
805
693
806
/** Implemented this way so that we can use a FloatList in a for loop. */
807
+ @ Override
694
808
public Iterator <Float > iterator () {
695
809
// }
696
810
//
@@ -701,6 +815,7 @@ public Iterator<Float> iterator() {
701
815
702
816
public void remove () {
703
817
FloatList .this .remove (index );
818
+ index --;
704
819
}
705
820
706
821
public Float next () {
@@ -726,7 +841,8 @@ public float[] array() {
726
841
727
842
728
843
/**
729
- * Copy as many values as possible into the specified array.
844
+ * Copy values into the specified array. If the specified array is null or
845
+ * not the same size, a new array will be allocated.
730
846
* @param array
731
847
*/
732
848
public float [] array (float [] array ) {
@@ -784,17 +900,23 @@ public String join(String separator) {
784
900
}
785
901
786
902
903
+ public void print () {
904
+ for (int i = 0 ; i < count ; i ++) {
905
+ System .out .format ("[%d] %f%n" , i , data [i ]);
906
+ }
907
+ }
908
+
909
+
910
+ /**
911
+ * Return this dictionary as a String in JSON format.
912
+ */
913
+ public String toJSON () {
914
+ return "[ " + join (", " ) + " ]" ;
915
+ }
916
+
917
+
787
918
@ Override
788
919
public String toString () {
789
- StringBuilder sb = new StringBuilder ();
790
- sb .append (getClass ().getSimpleName () + " size=" + size () + " [ " );
791
- for (int i = 0 ; i < size (); i ++) {
792
- if (i != 0 ) {
793
- sb .append (", " );
794
- }
795
- sb .append (i + ": " + data [i ]);
796
- }
797
- sb .append (" ]" );
798
- return sb .toString ();
920
+ return getClass ().getSimpleName () + " size=" + size () + " " + toJSON ();
799
921
}
800
922
}
0 commit comments