/** * Returns a normalized version of this array. Called getPercent() for * consistency with the Dict classes. It's a getter method because it needs * to returns a new list (because IntList/Dict can't do percentages or * normalization in place on int values). */ public FloatList getPercent() { double sum = 0; for (float value : array()) { sum += value; } FloatList outgoing = new FloatList(count); for (int i = 0; i < count; i++) { double percent = data[i] / sum; outgoing.set(i, (float) percent); } return outgoing; }
/** * @webref floatlist:method * @brief Return the smallest value */ public float min() { checkMinMax("min"); int index = minIndex(); return index == -1 ? Float.NaN : data[index]; }
/** * @webref floatlist:method * @brief Return the largest value */ public float max() { checkMinMax("max"); int index = maxIndex(); return index == -1 ? Float.NaN : data[index]; }
public FloatList copy() { FloatList outgoing = new FloatList(data); outgoing.count = count; return outgoing; }
/** * Returns a normalized version of this array. Called getPercent() for * consistency with the Dict classes. It's a getter method because it needs * to returns a new list (because IntList/Dict can't do percentages or * normalization in place on int values). */ public FloatList getPercent() { double sum = 0; for (float value : array()) { sum += value; } FloatList outgoing = new FloatList(count); for (int i = 0; i < count; i++) { double percent = data[i] / sum; outgoing.set(i, (float) percent); } return outgoing; }
/** * 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<Object> iter) { this(10); 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(); }
public void append(float[] values) { for (float v : values) { append(v); } }
public int minIndex() { checkMinMax("minIndex"); float m = Float.NaN; int mi = -1; for (int i = 0; i < count; i++) { // find one good value to start if (data[i] == data[i]) { m = data[i]; mi = i; // calculate the rest for (int j = i+1; j < count; j++) { float d = data[j]; if (!Float.isNaN(d) && (d < m)) { m = data[j]; mi = j; } } break; } } return mi; }
/** * Returns the actual array being used to store the data. For advanced users, * this is the fastest way to access a large list. Suitable for iterating * with a for() loop, but modifying the list will have terrible consequences. */ public float[] values() { crop(); return data; }
/** * Create a new array with a copy of all the values. * @return an array sized by the length of the list with each of the values. * @webref floatlist:method * @brief Create a new array with a copy of all the values */ public float[] array() { return array(null); }
public FloatList getSubset(int start) { return getSubset(start, count - start); }
public void insert(int index, float value) { insert(index, new float[] { value }); }
/** Replace the first instance of a particular value */ public boolean replaceValue(float value, float newValue) { if (Float.isNaN(value)) { for (int i = 0; i < count; i++) { if (Float.isNaN(data[i])) { data[i] = newValue; return true; } } } else { int index = index(value); if (index != -1) { data[index] = newValue; return true; } } return false; }
/** * @webref floatlist:method * @brief Return the largest value */ public float max() { checkMinMax("max"); int index = maxIndex(); return index == -1 ? Float.NaN : data[index]; }
public FloatList copy() { FloatList outgoing = new FloatList(data); outgoing.count = count; return outgoing; }