mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 21:59:20 +01:00
several fixes, cleanups, and speed improvements to dictionary classes
This commit is contained in:
@@ -133,74 +133,20 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
// private void crop() {
|
||||
// if (count != keys.length) {
|
||||
// keys = PApplet.subset(keys, 0, count);
|
||||
// values = PApplet.subset(values, 0, count);
|
||||
// }
|
||||
// }
|
||||
protected void crop() {
|
||||
if (count != keys.length) {
|
||||
keys = PApplet.subset(keys, 0, count);
|
||||
values = PApplet.subset(values, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the internal array being used to store the keys. Allocated but
|
||||
* unused entries will be removed. This array should not be modified.
|
||||
*
|
||||
* @webref intdict:method
|
||||
* @brief Return the internal array being used to store the keys
|
||||
*/
|
||||
// public String[] keys() {
|
||||
// crop();
|
||||
// return keys;
|
||||
// }
|
||||
|
||||
|
||||
// public Iterable<String> keys() {
|
||||
// return new Iterable<String>() {
|
||||
//
|
||||
// @Override
|
||||
// public Iterator<String> iterator() {
|
||||
// return new Iterator<String>() {
|
||||
// int index = -1;
|
||||
//
|
||||
// public void remove() {
|
||||
// removeIndex(index);
|
||||
// }
|
||||
//
|
||||
// public String next() {
|
||||
// return key(++index);
|
||||
// }
|
||||
//
|
||||
// public boolean hasNext() {
|
||||
// return index+1 < size();
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
|
||||
// Use this with 'for' loops
|
||||
public Iterable<String> keys() {
|
||||
return new Iterable<String>() {
|
||||
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return keyIterator();
|
||||
// return new Iterator<String>() {
|
||||
// int index = -1;
|
||||
//
|
||||
// public void remove() {
|
||||
// removeIndex(index);
|
||||
// }
|
||||
//
|
||||
// public String next() {
|
||||
// return key(++index);
|
||||
// }
|
||||
//
|
||||
// public boolean hasNext() {
|
||||
// return index+1 < size();
|
||||
// }
|
||||
// };
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -233,6 +179,7 @@ public class IntDict {
|
||||
* @brief Return a copy of the internal keys array
|
||||
*/
|
||||
public String[] keyArray() {
|
||||
crop();
|
||||
return keyArray(null);
|
||||
}
|
||||
|
||||
@@ -292,6 +239,7 @@ public class IntDict {
|
||||
* @brief Create a new array and copy each of the values into it
|
||||
*/
|
||||
public int[] valueArray() {
|
||||
crop();
|
||||
return valueArray(null);
|
||||
}
|
||||
|
||||
@@ -428,7 +376,9 @@ public class IntDict {
|
||||
|
||||
// return the index of the minimum value
|
||||
public int minIndex() {
|
||||
checkMinMax("minIndex");
|
||||
//checkMinMax("minIndex");
|
||||
if (count == 0) return -1;
|
||||
|
||||
int index = 0;
|
||||
int value = values[0];
|
||||
for (int i = 1; i < count; i++) {
|
||||
@@ -441,23 +391,30 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
// return the minimum value
|
||||
// return the key for the minimum value
|
||||
public String minKey() {
|
||||
checkMinMax("minKey");
|
||||
int index = minIndex();
|
||||
if (index == -1) {
|
||||
return null;
|
||||
}
|
||||
return keys[index];
|
||||
}
|
||||
|
||||
|
||||
// return the minimum value, or throw an error if there are no values
|
||||
public int minValue() {
|
||||
checkMinMax("minValue");
|
||||
return values[minIndex()];
|
||||
}
|
||||
|
||||
|
||||
// return the key for the minimum value
|
||||
public String minKey() {
|
||||
checkMinMax("minKey");
|
||||
return keys[minIndex()];
|
||||
}
|
||||
|
||||
|
||||
// return the index of the max value
|
||||
public int maxIndex() {
|
||||
checkMinMax("maxIndex");
|
||||
//checkMinMax("maxIndex");
|
||||
if (count == 0) {
|
||||
return -1;
|
||||
}
|
||||
int index = 0;
|
||||
int value = values[0];
|
||||
for (int i = 1; i < count; i++) {
|
||||
@@ -470,17 +427,21 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
// return the maximum value
|
||||
public int maxValue() {
|
||||
checkMinMax("maxValue");
|
||||
return values[maxIndex()];
|
||||
/** 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];
|
||||
}
|
||||
|
||||
|
||||
// return the key corresponding to the maximum value
|
||||
public String maxKey() {
|
||||
checkMinMax("maxKey");
|
||||
return keys[maxIndex()];
|
||||
// return the maximum value or throw an error if zero length
|
||||
public int maxValue() {
|
||||
checkMinMax("maxIndex");
|
||||
return values[maxIndex()];
|
||||
}
|
||||
|
||||
|
||||
@@ -541,8 +502,8 @@ public class IntDict {
|
||||
keys[b] = tkey;
|
||||
values[b] = tvalue;
|
||||
|
||||
indices.put(keys[a], Integer.valueOf(a));
|
||||
indices.put(keys[b], Integer.valueOf(b));
|
||||
// indices.put(keys[a], Integer.valueOf(a));
|
||||
// indices.put(keys[b], Integer.valueOf(b));
|
||||
}
|
||||
|
||||
|
||||
@@ -554,7 +515,7 @@ public class IntDict {
|
||||
* @brief Sort the keys alphabetically
|
||||
*/
|
||||
public void sortKeys() {
|
||||
sortImpl(true, false);
|
||||
sortImpl(true, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -562,10 +523,10 @@ public class IntDict {
|
||||
* tie-breaker (only really possible with a key that has a case change).
|
||||
*
|
||||
* @webref intdict:method
|
||||
* @brief Sort the keys alphabetially in reverse
|
||||
* @brief Sort the keys alphabetically in reverse
|
||||
*/
|
||||
public void sortKeysReverse() {
|
||||
sortImpl(true, true);
|
||||
sortImpl(true, true, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -576,9 +537,20 @@ public class IntDict {
|
||||
* @brief Sort by values in ascending order
|
||||
*/
|
||||
public void sortValues() {
|
||||
sortImpl(false, false);
|
||||
sortValues(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set true to ensure that the order returned is identical. Slightly
|
||||
* slower because the tie-breaker for identical values compares the keys.
|
||||
* @param stable
|
||||
*/
|
||||
public void sortValues(boolean stable) {
|
||||
sortImpl(false, false, stable);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort by values in descending order. The largest value will be at [0].
|
||||
*
|
||||
@@ -586,11 +558,17 @@ public class IntDict {
|
||||
* @brief Sort by values in descending order
|
||||
*/
|
||||
public void sortValuesReverse() {
|
||||
sortImpl(false, true);
|
||||
sortValuesReverse(true);
|
||||
}
|
||||
|
||||
|
||||
protected void sortImpl(final boolean useKeys, final boolean reverse) {
|
||||
public void sortValuesReverse(boolean stable) {
|
||||
sortImpl(false, true, stable);
|
||||
}
|
||||
|
||||
|
||||
protected void sortImpl(final boolean useKeys, final boolean reverse,
|
||||
final boolean stable) {
|
||||
Sort s = new Sort() {
|
||||
@Override
|
||||
public int size() {
|
||||
@@ -603,11 +581,11 @@ public class IntDict {
|
||||
if (useKeys) {
|
||||
diff = keys[a].compareToIgnoreCase(keys[b]);
|
||||
if (diff == 0) {
|
||||
return values[a] - values[b];
|
||||
diff = values[a] - values[b];
|
||||
}
|
||||
} else { // sort values
|
||||
diff = values[a] - values[b];
|
||||
if (diff == 0) {
|
||||
if (diff == 0 && stable) {
|
||||
diff = keys[a].compareToIgnoreCase(keys[b]);
|
||||
}
|
||||
}
|
||||
@@ -620,18 +598,24 @@ public class IntDict {
|
||||
}
|
||||
};
|
||||
s.run();
|
||||
|
||||
// Set the indices after sort/swaps (performance fix 160411)
|
||||
indices = new HashMap<String, Integer>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
indices.put(keys[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return an IntDict with the original keys, mapped to their pct of the total
|
||||
*/
|
||||
public FloatDict getPercent() {
|
||||
double sum = 0;
|
||||
for (int value : valueArray()) {
|
||||
sum += value;
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += values[i];
|
||||
}
|
||||
FloatDict outgoing = new FloatDict();
|
||||
for (int i = 0; i < size(); i++) {
|
||||
@@ -655,6 +639,13 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
public void print() {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
System.out.println(keys[i] + " = " + values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write tab-delimited entries out to
|
||||
* @param writer
|
||||
@@ -667,13 +658,6 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
public void print() {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
System.out.println(keys[i] + " = " + values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
Reference in New Issue
Block a user