/** * Sum all of the values in this dictionary, then return a new FloatDict of * each key, divided by the total sum. The total for all values will be ~1.0. * @return a Dict with the original keys, mapped to their pct of the total */ public FloatDict getPercent() { double sum = 0; for (float value : valueArray()) { sum += value; } FloatDict outgoing = new FloatDict(); for (int i = 0; i < size(); i++) { double percent = value(i) / sum; outgoing.set(key(i), (float) percent); } return outgoing; }
/** * @webref floatdict:method * @brief Subtract from a value */ public void sub(String key, float amount) { add(key, -amount); }
public String minKey() { checkMinMax("minKey"); int index = minIndex(); if (index == -1) { return null; } return keys[index]; }
/** The max value. */ public float maxValue() { checkMinMax("maxValue"); return values[maxIndex()]; }
/** * @webref floatdict:method * @brief Remove a key/value pair */ public int remove(String key) { int index = index(key); if (index != -1) { removeIndex(index); } return index; }
/** * Sum all of the values in this dictionary, then return a new FloatDict of * each key, divided by the total sum. The total for all values will be ~1.0. * @return a FloatDict with the original keys, mapped to their pct of the total */ public FloatDict getPercent() { double sum = sum(); FloatDict outgoing = new FloatDict(); for (int i = 0; i < size(); i++) { double percent = value(i) / sum; outgoing.set(key(i), (float) percent); } return outgoing; }
/** * @webref floatdict:method * @brief Multiply a value */ public void mult(String key, float amount) { int index = index(key); if (index != -1) { values[index] *= amount; } }
/** * Sum all of the values in this dictionary, then return a new FloatDict of * each key, divided by the total sum. The total for all values will be ~1.0. * @return an IntDict with the original keys, mapped to their pct of the total */ public FloatDict getPercent() { double sum = sum(); // a little more accuracy FloatDict outgoing = new FloatDict(); for (int i = 0; i < size(); i++) { double percent = value(i) / sum; outgoing.set(key(i), (float) percent); } return outgoing; }
/** Returns a duplicate copy of this object. */ public FloatDict copy() { FloatDict outgoing = new FloatDict(count); System.arraycopy(keys, 0, outgoing.keys, 0, count); System.arraycopy(values, 0, outgoing.values, 0, count); for (int i = 0; i < count; i++) { outgoing.indices.put(keys[i], i); } outgoing.count = count; return outgoing; }
public void remove() { removeIndex(index); index--; }
/** The key for a max value; null if empty or everything is NaN (no max). */ public String maxKey() { //checkMinMax("maxKey"); int index = maxIndex(); if (index == -1) { return null; } return keys[index]; }
/** * @webref floatlist:method * @brief Return the largest value */ // The index of the entry that has the max value. Reference above is incorrect. public int maxIndex() { checkMinMax("maxIndex"); // Will still return NaN if there is 1 or more entries, and they're all NaN float m = Float.NaN; int mi = -1; for (int i = 0; i < count; i++) { // find one good value to start if (values[i] == values[i]) { m = values[i]; mi = i; // calculate the rest for (int j = i+1; j < count; j++) { float d = values[j]; if (!Float.isNaN(d) && (d > m)) { m = values[j]; mi = j; } } break; } } return mi; }
/** * Return a copy of the internal keys array. This array can be modified. * * @webref floatdict:method * @brief Return a copy of the internal keys array */ public String[] keyArray() { return keyArray(null); }
public String next() { return key(++index); }
/** * @webref floatdict:method * @brief Divide a value */ public void div(String key, float amount) { int index = index(key); if (index != -1) { values[index] /= amount; } }