Skip to content

Commit

Permalink
ported latest changes/fixes from java's data
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Nov 17, 2016
1 parent e934b72 commit 3b61f43
Show file tree
Hide file tree
Showing 13 changed files with 1,278 additions and 610 deletions.
453 changes: 238 additions & 215 deletions core/src/processing/data/FloatDict.java

Large diffs are not rendered by default.

166 changes: 144 additions & 22 deletions core/src/processing/data/FloatList.java
Expand Up @@ -50,13 +50,15 @@ public FloatList() {
data = new float[10];
}


/**
* @nowebref
*/
public FloatList(int length) {
data = new float[length];
}


/**
* @nowebref
*/
Expand All @@ -66,13 +68,49 @@ public FloatList(float[] list) {
System.arraycopy(list, 0, data, 0, count);
}


/**
* Construct an FloatList from an iterable pile of objects.
* For instance, a float array, an array of strings, who knows).
* Un-parseable or null values will be set to NaN.
* @nowebref
*/
public FloatList(Iterable<Float> iter) {
public FloatList(Iterable<Object> iter) {
this(10);
for (float v : iter) {
append(v);
for (Object o : iter) {
if (o == null) {
append(Float.NaN);
} else if (o instanceof Number) {
append(((Number) o).floatValue());
} else {
append(PApplet.parseFloat(o.toString().trim()));
}
}
crop();
}


/**
* Construct an FloatList from a random pile of objects.
* Un-parseable or null values will be set to NaN.
*/
public FloatList(Object... items) {
// nuts, no good way to pass missingValue to this fn (varargs must be last)
final float missingValue = Float.NaN;

count = items.length;
data = new float[count];
int index = 0;
for (Object o : items) {
float value = missingValue;
if (o != null) {
if (o instanceof Number) {
value = ((Number) o).floatValue();
} else {
value = PApplet.parseFloat(o.toString().trim(), missingValue);
}
}
data[index++] = value;
}
}

Expand Down Expand Up @@ -132,6 +170,9 @@ public void clear() {
* @brief Get an entry at a particular index
*/
public float get(int index) {
if (index >= count) {
throw new ArrayIndexOutOfBoundsException(index);
}
return data[index];
}

Expand All @@ -156,6 +197,22 @@ public void set(int index, float what) {
}


/** Just an alias for append(), but matches pop() */
public void push(float value) {
append(value);
}


public float pop() {
if (count == 0) {
throw new RuntimeException("Can't call pop() on an empty list");
}
float value = get(count-1);
count--;
return value;
}


/**
* Remove an element from the specified index.
*
Expand Down Expand Up @@ -288,6 +345,14 @@ public void append(FloatList list) {
}


/** Add this value, but only if it's not already in the list. */
public void appendUnique(float value) {
if (!hasValue(value)) {
append(value);
}
}


// public void insert(int index, int value) {
// if (index+1 > count) {
// if (index+1 < data.length) {
Expand Down Expand Up @@ -318,8 +383,13 @@ public void append(FloatList list) {
// }


public void insert(int index, float value) {
insert(index, new float[] { value });
}


// same as splice
public void insert(int index, int[] values) {
public void insert(int index, float[] values) {
if (index < 0) {
throw new IllegalArgumentException("insert() index cannot be negative: it was " + index);
}
Expand Down Expand Up @@ -347,7 +417,7 @@ public void insert(int index, int[] values) {
}


public void insert(int index, IntList list) {
public void insert(int index, FloatList list) {
insert(index, list.values());
}

Expand Down Expand Up @@ -437,12 +507,23 @@ public boolean hasValue(float value) {
}


private void boundsProblem(int index, String method) {
final String msg = String.format("The list size is %d. " +
"You cannot %s() to element %d.", count, method, index);
throw new ArrayIndexOutOfBoundsException(msg);
}


/**
* @webref floatlist:method
* @brief Add to a value
*/
public void add(int index, float amount) {
data[index] += amount;
if (index < count) {
data[index] += amount;
} else {
boundsProblem(index, "add");
}
}


Expand All @@ -451,7 +532,11 @@ public void add(int index, float amount) {
* @brief Subtract from a value
*/
public void sub(int index, float amount) {
data[index] -= amount;
if (index < count) {
data[index] -= amount;
} else {
boundsProblem(index, "sub");
}
}


Expand All @@ -460,7 +545,11 @@ public void sub(int index, float amount) {
* @brief Multiply a value
*/
public void mult(int index, float amount) {
data[index] *= amount;
if (index < count) {
data[index] *= amount;
} else {
boundsProblem(index, "mult");
}
}


Expand All @@ -469,7 +558,11 @@ public void mult(int index, float amount) {
* @brief Divide a value
*/
public void div(int index, float amount) {
data[index] /= amount;
if (index < count) {
data[index] /= amount;
} else {
boundsProblem(index, "div");
}
}


Expand Down Expand Up @@ -585,7 +678,27 @@ public void sortReverse() {
new Sort() {
@Override
public int size() {
return count;
// if empty, don't even mess with the NaN check, it'll AIOOBE
if (count == 0) {
return 0;
}
// move NaN values to the end of the list and don't sort them
int right = count - 1;
while (data[right] != data[right]) {
right--;
if (right == -1) { // all values are NaN
return 0;
}
}
for (int i = right; i >= 0; --i) {
float v = data[i];
if (v != v) {
data[i] = data[right];
data[right] = v;
--right;
}
}
return right + 1;
}

@Override
Expand Down Expand Up @@ -623,7 +736,7 @@ public void swap(int a, int b) {

/**
* @webref floatlist:method
* @brief Reverse sort, orders values by first digit
* @brief Reverse the order of the list elements
*/
public void reverse() {
int ii = count - 1;
Expand Down Expand Up @@ -691,6 +804,7 @@ public float[] values() {


/** Implemented this way so that we can use a FloatList in a for loop. */
@Override
public Iterator<Float> iterator() {
// }
//
Expand All @@ -701,6 +815,7 @@ public Iterator<Float> iterator() {

public void remove() {
FloatList.this.remove(index);
index--;
}

public Float next() {
Expand All @@ -726,7 +841,8 @@ public float[] array() {


/**
* Copy as many values as possible into the specified array.
* Copy values into the specified array. If the specified array is null or
* not the same size, a new array will be allocated.
* @param array
*/
public float[] array(float[] array) {
Expand Down Expand Up @@ -784,17 +900,23 @@ public String join(String separator) {
}


public void print() {
for (int i = 0; i < count; i++) {
System.out.format("[%d] %f%n", i, data[i]);
}
}


/**
* Return this dictionary as a String in JSON format.
*/
public String toJSON() {
return "[ " + join(", ") + " ]";
}


@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName() + " size=" + size() + " [ ");
for (int i = 0; i < size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(i + ": " + data[i]);
}
sb.append(" ]");
return sb.toString();
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
}
}

0 comments on commit 3b61f43

Please sign in to comment.