/** * Merge another dictionary into this one. Calling this increment() * since it doesn't make sense in practice for the other dictionary types, * even though it's technically an add(). */ public void increment(IntDict dict) { for (int i = 0; i < dict.count; i++) { add(dict.key(i), dict.value(i)); } }
public int minValue() { checkMinMax("minValue"); return values[minIndex()]; }
/** * @webref intdict: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 Dict with the original keys, mapped to their pct of the total */ public FloatDict getPercent() { double sum = 0; for (int 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; }
/** * 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; }
public int maxValue() { checkMinMax("maxIndex"); return values[maxIndex()]; }
/** * @webref intdict:method * @brief Multiply a value */ public void mult(String key, int amount) { int index = index(key); if (index != -1) { values[index] *= amount; } }
/** Returns a duplicate copy of this object. */ public IntDict copy() { IntDict outgoing = new IntDict(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; }
/** * @webref intdict:method * @brief Subtract from a value */ public void sub(String key, int amount) { add(key, -amount); }
/** * Return a copy of the internal keys array. This array can be modified. * * @webref intdict:method * @brief Return a copy of the internal keys array */ public String[] keyArray() { return keyArray(null); }
public int maxIndex() { checkMinMax("maxIndex"); int index = 0; int value = values[0]; for (int i = 1; i < count; i++) { if (values[i] > value) { index = i; value = values[i]; } } return index; }
@Override public Iterator<String> iterator() { return keyIterator(); } };
public String next() { return key(++index); }
/** return the key corresponding to the maximum value or null if no entries */ public String maxKey() { //checkMinMax("maxKey"); int index = maxIndex(); if (index == -1) { return null; } return keys[index]; }