- java.lang.Object
-
- processing.data.LongDict
-
public class LongDict extends Object
A simple class to use a String as a lookup for an int value.- See Also:
FloatDict,StringDict
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classLongDict.Entry
-
Constructor Summary
Constructors Constructor Description LongDict()LongDict(int length)Create a new lookup with a specific size.LongDict(BufferedReader reader)Read a set of entries from a Reader that has each key/value pair on a single line, separated by a tab.LongDict(Object[][] pairs)Constructor to allow (more intuitive) inline initialization, e.g.:LongDict(String[] keys, long[] values)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(String key, long amount)voidclear()Remove all entries.LongDictcopy()Returns a duplicate copy of this object.protected voidcreate(String what, long much)protected voidcrop()voiddiv(String key, long amount)Iterable<LongDict.Entry>entries()Iterator<LongDict.Entry>entryIterator()longget(String key)Return a value for the specified key.longget(String key, long alternate)FloatDictgetPercent()Sum all of the values in this dictionary, then return a new FloatDict of each key, divided by the total sum.booleanhasKey(String key)voidincrement(String key)Increase the value associated with a specific key by 1.voidincrement(LongDict dict)Merge another dictionary into this one.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()return the key corresponding to the maximum value or null if no entrieslongmaxValue()intminIndex()StringminKey()longminValue()voidmult(String key, long amount)voidprint()longremove(String key)longremoveIndex(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, long amount)Create a new key/value pair or change the value of one.voidsetIndex(int index, String key, long value)intsize()Returns the number of key/value pairsprotected voidsortImpl(boolean useKeys, boolean reverse, boolean stable)voidsortKeys()Sort the keys alphabetically (ignoring case).voidsortKeysReverse()Sort the keys alphabetically in reverse (ignoring case).voidsortValues()Sort by values in ascending order.voidsortValues(boolean stable)Set true to ensure that the order returned is identical.voidsortValuesReverse()Sort by values in descending order.voidsortValuesReverse(boolean stable)voidsub(String key, long amount)longsum()voidswap(int a, int b)StringtoJSON()Return this dictionary as a String in JSON format.StringtoString()longvalue(int index)int[]valueArray()Create a new array and copy each of the values into it.int[]valueArray(int[] array)Fill an already-allocated array with the values (more efficient than creating a new array each time).Iterator<Long>valueIterator()Iterable<Long>values()voidwrite(PrintWriter writer)Write tab-delimited entries to a PrintWriter
-
-
-
Field Detail
-
count
protected int count
Number of elements in the table
-
keys
protected String[] keys
-
values
protected long[] values
-
-
Constructor Detail
-
LongDict
public LongDict()
-
LongDict
public LongDict(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.
-
LongDict
public LongDict(BufferedReader reader)
Read a set of entries from a Reader that has each key/value pair on a single line, separated by a tab.
-
LongDict
public LongDict(String[] keys, long[] values)
-
LongDict
public LongDict(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()
Returns the number of key/value pairs
-
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<LongDict.Entry> entries()
-
entryIterator
public Iterator<LongDict.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 long value(int index)
-
valueArray
public int[] valueArray()
Create a new array and copy each of the values into it.
-
valueArray
public int[] valueArray(int[] 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.- Parameters:
array- values to copy into the array
-
get
public long get(String key)
Return a value for the specified key.
-
get
public long get(String key, long alternate)
-
set
public void set(String key, long amount)
Create a new key/value pair or change the value of one.
-
setIndex
public void setIndex(int index, String key, long value)
-
hasKey
public boolean hasKey(String key)
-
increment
public void increment(String key)
Increase the value associated with a specific key by 1.
-
increment
public void increment(LongDict dict)
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().
-
add
public void add(String key, long amount)
-
sub
public void sub(String key, long amount)
-
mult
public void mult(String key, long amount)
-
div
public void div(String key, long amount)
-
minIndex
public int minIndex()
-
minKey
public String minKey()
-
minValue
public long minValue()
-
maxIndex
public int maxIndex()
-
maxKey
public String maxKey()
return the key corresponding to the maximum value or null if no entries
-
maxValue
public long maxValue()
-
sum
public long sum()
-
index
public int index(String what)
-
create
protected void create(String what, long much)
-
remove
public long remove(String key)
-
removeIndex
public long 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()
Sort the keys alphabetically in reverse (ignoring case). Uses the value as a tie-breaker (only really possible with a key that has a case change).
-
sortValues
public void sortValues()
Sort by values in ascending order. The smallest 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()
Sort by values in descending order. The largest value will be at [0].
-
sortValuesReverse
public void sortValuesReverse(boolean stable)
-
sortImpl
protected void sortImpl(boolean useKeys, boolean reverse, boolean stable)
-
getPercent
public FloatDict 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:
- an IntDict with the original keys, mapped to their pct of the total
-
copy
public LongDict 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 to a PrintWriter
-
toJSON
public String toJSON()
Return this dictionary as a String in JSON format.
-
-