- DESCRIPTION
- Defines a lookup table that uses chained hashing (bins of linked lists) to associate key/value pairs. All the keys must be of the same datatype and all the values must be of the same datatype, but the keys can be different datatypes from the values.
- The following requirements must be met for the following types to be used as KeyType:
-
primitive (Int32, Real64, etc)
No restrictions.
compound
You must define the following class method:
Global::hash_code(CompoundType c).Int32
reference
You must override hash_code.Int32 in the class type.
Class String already defines hash_code.
See Object::hash_code() for more information on creating hash codes.
- HashTable defaults to 16 bins with an average bin size of 3 - once the average number of entries per bin exceeds 3 the number of bins is doubled. Alternate values may be specified in the initializer.
- Example:
local HashTable<<String,Int32>> numbers()
numbers[ "one" ] = 1
numbers[ "two" ] = 2
numbers[ "three" ] = 3
...
local HashTable<<String,Int32>> factors()
factors[ "hundred" ] = 10^2
factors[ "thousand" ] = 10^3
factors[ "million" ] = 10^6
...
local String word_form = "two million"
local String[] words = word_form.tidy.split
println( numbers[words[0]] * factors[words[1]] )
# prints: 2000000
- PROPERTIES
- bins : HashTableBin<<KeyType,ValueType>>[]
- num_entries : Int32
- average_bin_size : Real64
- Limit of the average bin size. If there are 16 bins and an average_bin_size of 3, the table will expand once there are 3 items in each bin (3*16=48 items total, 48/16 bins=3 avg) or when a single bin has 48 items (48 / 16 = 3 avg). This value is used to calculate the max_entries before the table expands.
- max_entries : Real64
- Recalculated from average_bin_size each time the table expands.
- max_bins : Int32
- The number of bins (default: 512 ) at which the table will stop auto-expanding # and instead continue to fill the existing bins however large they may get.
- hash_mask : Int32
- Internal use. Set to the number of bins-1. E.g. for 16 bins the mask is 15, since hash_code & 15 -> 0..15
- METHODS
- init()
- Default initializer, necessary if you ever make a singleton class that extends HashTable.
- init(Int32 num_bins[,average_bin_size])
- Initializes this hash table with the given number of bins (default 16) and the given average_bin_size (default 3.0).
- clear()
- Removes all mappings from this hash table.
- count().Int32
- Returns the number of mappings in this table.
- set(KeyType key,ValueType value)
- Associates the given value with the given key. When get() is called with 'key', 'value' will be returned.
- set(Mapping<<KeyType,ValueType>> entry)
- Associates the given value with the given key. When get() is called with 'key', 'value' will be returned.
- get(KeyType key).ValueType
- Retrieves the value (previously mapped with set()) associated with 'key'. Throws a NoSuchElementError if 'key' has not been defined.
- find(KeyType key).Mapping<<KeyType,ValueType>>
- Finds the underlying Mapping that defines the mapping between 'key' and 'value'. Returns a null reference if no mapping is present. Useful for optimizing conditional table operations.
- Example without find():
if (table.contains(id))
person = table[id]
endIf
Example with find() - a bit faster:
local var mapping = table.find(id)
if (mapping isNot null)
person = mapping.value
endIf
- contains(KeyType key).Logical
- Returns "true" if 'key' has an associated value in this table that can be retrieved with get().
- remove(KeyType key).ValueType
- Removes the mapping between 'key' and its value from this table. Throws a NoSuchElementError if 'key' has not been defined.
- get_bin(KeyType key).HashTableBin<<KeyType,ValueType>>
- add(HashTable<<KeyType,ValueType>> other)
- Adds all entries in the compatible hash table to this hash table.
- expand_table()
- Resizes this hash table to have double the number of bins as before.
- to_String().String
- Returns a string representation of this hashtable.
- keys().HashTableKeyReader<<KeyType,ValueType>>
- Returns a reader that iterates through this hash table's keys. These are not guaranteed to be an any particular order.
- Example:
local HashTable<<String,Int32>> ages()
ages["Abe"] = 34
ages["Murphy"] = 30
ages["Matt"] = 28
forEach (name in ages.keys)
println( "$ is $ years old." (name,ages[name]) )
# prints:
# Abe is 34 years old.
# Matt is 28 years old.
# Murphy is 30 years old.
endForEach
- values().HashTableValueReader<<KeyType,ValueType>>
- Returns a reader that iterates through this hash table's values. These are not guaranteed to be an any particular order.
|