- java.lang.Object
-
- processing.data.DoubleDict
-
public class DoubleDict extends Object
A simple table class to use a String as a lookup for an double value.- See Also:
IntDict,StringDict
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classDoubleDict.Entry
-
Constructor Summary
Constructors Constructor Description DoubleDict()DoubleDict(int length)Create a new lookup with a specific size.DoubleDict(BufferedReader reader)Read a set of entries from a Reader that has each key/value pair on a single line, separated by a tab.DoubleDict(Object[][] pairs)Constructor to allow (more intuitive) inline initialization, e.g.:DoubleDict(String[] keys, double[] values)DoubleDict(Map<String,Double> incoming)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(String key, double amount)voidclear()Remove all entries.DoubleDictcopy()Returns a duplicate copy of this object.protected voidcreate(String what, double much)protected voidcrop()voiddiv(String key, double amount)Iterable<DoubleDict.Entry>entries()Iterator<DoubleDict.Entry>entryIterator()doubleget(String key)Return a value for the specified key.doubleget(String key, double alternate)DoubleDictgetPercent()Sum all of the values in this dictionary, then return a new FloatDict of each key, divided by the total sum.booleanhasKey(String key)intindex(String what)Stringkey(int index)String[]keyArray()Return a copy of the internal keys array.String[]keyArray(String[] outgoing)Iterator<String>keyIterator()Iterable<String>keys()intmaxIndex()StringmaxKey()The key for a max value; null if empty or everything is NaN (no max).doublemaxValue()The max value.intminIndex()StringminKey()doubleminValue()voidmult(String key, double amount)voidprint()doubleremove(String key)doubleremoveIndex(int index)voidresize(int length)Resize the internal data, this can only be used to shrink the list.voidsave(File file)Save tab-delimited entries to a file (TSV format, UTF-8 encoding)voidset(String key, double amount)voidsetIndex(int index, String key, double value)intsize()protected voidsortImpl(boolean useKeys, boolean reverse, boolean stable)voidsortKeys()Sort the keys alphabetically (ignoring case).voidsortKeysReverse()voidsortValues()Sort by values in descending order (largest value will be at [0]).voidsortValues(boolean stable)Set true to ensure that the order returned is identical.voidsortValuesReverse()voidsortValuesReverse(boolean stable)voidsub(String key, double amount)doublesum()voidswap(int a, int b)StringtoJSON()Return this dictionary as a String in JSON format.StringtoString()doublevalue(int index)double[]valueArray()Create a new array and copy each of the values into it.double[]valueArray(double[] array)Fill an already-allocated array with the values (more efficient than creating a new array each time).Iterator<Double>valueIterator()Iterable<Double>values()voidwrite(PrintWriter writer)Write tab-delimited entries out to
-
-
-
Field Detail
-
count
protected int count
Number of elements in the table
-
keys
protected String[] keys
-
values
protected double[] values
-
-
Constructor Detail
-
DoubleDict
public DoubleDict()
-
DoubleDict
public DoubleDict(int length)
Create a new lookup with a specific size. This is more efficient than not specifying a size. Use it when you know the rough size of the thing you're creating.
-
DoubleDict
public DoubleDict(BufferedReader reader)
Read a set of entries from a Reader that has each key/value pair on a single line, separated by a tab.
-
DoubleDict
public DoubleDict(String[] keys, double[] values)
-
DoubleDict
public DoubleDict(Object[][] pairs)
Constructor to allow (more intuitive) inline initialization, e.g.:new FloatDict(new Object[][] { { "key1", 1 }, { "key2", 2 } });
-
-
Method Detail
-
size
public int size()
-
resize
public void resize(int length)
Resize the internal data, this can only be used to shrink the list. Helpful for situations like sorting and then grabbing the top 50 entries.
-
clear
public void clear()
Remove all entries.
-
entries
public Iterable<DoubleDict.Entry> entries()
-
entryIterator
public Iterator<DoubleDict.Entry> entryIterator()
-
key
public String key(int index)
-
crop
protected void crop()
-
keyArray
public String[] keyArray()
Return a copy of the internal keys array. This array can be modified.
-
value
public double value(int index)
-
valueArray
public double[] valueArray()
Create a new array and copy each of the values into it.
-
valueArray
public double[] valueArray(double[] array)
Fill an already-allocated array with the values (more efficient than creating a new array each time). If 'array' is null, or not the same size as the number of values, a new array will be allocated and returned.
-
get
public double get(String key)
Return a value for the specified key.
-
get
public double get(String key, double alternate)
-
set
public void set(String key, double amount)
-
setIndex
public void setIndex(int index, String key, double value)
-
hasKey
public boolean hasKey(String key)
-
add
public void add(String key, double amount)
-
sub
public void sub(String key, double amount)
-
mult
public void mult(String key, double amount)
-
div
public void div(String key, double amount)
-
minIndex
public int minIndex()
-
minKey
public String minKey()
-
minValue
public double minValue()
-
maxIndex
public int maxIndex()
-
maxKey
public String maxKey()
The key for a max value; null if empty or everything is NaN (no max).
-
maxValue
public double maxValue()
The max value. (Or NaN if no entries or they're all NaN.)
-
sum
public double sum()
-
index
public int index(String what)
-
create
protected void create(String what, double much)
-
remove
public double remove(String key)
-
removeIndex
public double removeIndex(int index)
-
swap
public void swap(int a, int b)
-
sortKeys
public void sortKeys()
Sort the keys alphabetically (ignoring case). Uses the value as a tie-breaker (only really possible with a key that has a case change).
-
sortKeysReverse
public void sortKeysReverse()
-
sortValues
public void sortValues()
Sort by values in descending order (largest value will be at [0]).
-
sortValues
public void sortValues(boolean stable)
Set true to ensure that the order returned is identical. Slightly slower because the tie-breaker for identical values compares the keys.- Parameters:
stable-
-
sortValuesReverse
public void sortValuesReverse()
-
sortValuesReverse
public void sortValuesReverse(boolean stable)
-
sortImpl
protected void sortImpl(boolean useKeys, boolean reverse, boolean stable)
-
getPercent
public DoubleDict getPercent()
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.- Returns:
- a FloatDict with the original keys, mapped to their pct of the total
-
copy
public DoubleDict copy()
Returns a duplicate copy of this object.
-
print
public void print()
-
save
public void save(File file)
Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
-
write
public void write(PrintWriter writer)
Write tab-delimited entries out to- Parameters:
writer-
-
toJSON
public String toJSON()
Return this dictionary as a String in JSON format.
-
-