Libraries.Containers.HashTable Documentation
The HashTable class is a data structure that stores and allows access to items through the use of a key. In the hash table keys and values are paired. Some basic examples and explanations of Lists can be found below.
Example Code
use Libraries.Containers.HashTable
class Main
action Main
//make the hash table
HashTable<text, integer> phoneBook
//add a value(2626984) with a key(Jane)
phoneBook:Add("Jane", 2626984)
//get it back
integer value = phoneBook:GetValue("Jane")
end
end
Inherits from: Libraries.Language.Object
Summary
Actions Summary Table
Actions | Description |
---|---|
Add(Key key, Value value) | This action adds an item to the hash table, given the key-value pair. |
Compare(Libraries.Language.Object object) | This action compares two object hash codes and returns an integer. |
Copy() | This action removes a key-value pair given a key. |
CopyToKeyArray() | This action gets an array that contains all the keys in the hash table. |
CopyToValueArray() | This action gets an array that contains all the values in the hash table. |
Empty() | This action empties or clears out the hash table. |
Equals(Libraries.Language.Object object) | This action determines if two objects are equal based on their hash code values. |
GetHashCode() | This action gets the hash code for an object. |
GetKey(Value value) | This action gets the key that matches the value. |
GetKeyIterator() | This action gets an iterator that iterates over all the keys in the hash table. |
GetSize() | This action gets the number of items in the hash table. |
GetValue(Key key) | This action gets a value with a given key. |
GetValueIterator() | This action gets an iterator that iterates over all the values in the hash table. |
HasKey(Key key) | This action determines if the hash table contains a certain key. |
HasValue(Value value) | This action determines if the hash table contains a certain value. |
IsEmpty() | This action determines if a hash table is empty. |
RemoveAllKeys(Key key) | This action removes a key-value pair given a key. |
RemoveAllValues(Value value) | This action removes all instances of a key-value pair given a key. |
RemoveKey(Key key) | This action removes a key-value pair given a key. |
RemoveValue(Value value) | This action removes a key-value pair given a value. |
Set(Key key, Value value) | This action adds an item to the hash table, given the key-value pair. |
SetSize(integer size) | This action sets the size of the hash table. |
Actions Documentation
Add(Key key, Value value)
This action adds an item to the hash table, given the key-value pair. The implementation of this action is identical to that of Set.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Parameters
- Libraries.Language.Object: The key used to access the value.
- Libraries.Language.Object: The value to be stored.
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.
Example Code
Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)
Parameters
- Libraries.Language.Object: The object to compare to.
Return
integer: The Compare result, Smaller, Equal, or Larger.
Copy()
This action removes a key-value pair given a key. As the HashTable class requires unique values, this action really only removes the one value
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllKeys("Jane")
Return
Libraries.Language.Object: True if any key-value pairs were removed and false if none were removed.
CopyToKeyArray()
This action gets an array that contains all the keys in the hash table. This method requires iterating over all elements in the array, and as such, should be used sparing.
Example Code
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()
Return
Libraries.Containers.Array: The iterator of keys.
CopyToValueArray()
This action gets an array that contains all the values in the hash table. This method requires iterating over all elements in the array, and as such, should be used sparingly.
Example Code
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()
Return
Libraries.Containers.Array: The iterator of keys.
Empty()
This action empties or clears out the hash table.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:Empty()
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Example Code
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
GetHashCode()
This action gets the hash code for an object.
Example Code
Object o
integer hash = o:GetHashCode()
Return
integer: The integer hash code of the object.
GetKey(Value value)
This action gets the key that matches the value. Warning: this method is inefficient, you should access items in a hash table through their keys.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
text key = phoneBook:GetKey(2626984)
Parameters
- Libraries.Language.Object: The value that matches up to a key.
Return
Libraries.Language.Object: The key that matches the key-value pair.
GetKeyIterator()
This action gets an iterator that iterates over all the keys in the hash table.
Example Code
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Iterator<text> keysIterator = phoneBook:GetKeyIterator()
Return
Libraries.Containers.Iterator: The iterator of keys.
GetSize()
This action gets the number of items in the hash table.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
integer size = phoneBook:GetSize()
Return
integer: The number of items in the hash table .
GetValue(Key key)
This action gets a value with a given key.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
integer value = phoneBook:GetValue("Jane")
Parameters
- Libraries.Language.Object: The key that matches up to a value.
Return
Libraries.Language.Object: The value that matches the key-value pair.
GetValueIterator()
This action gets an iterator that iterates over all the values in the hash table.
Example Code
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Iterator<integer> keysIterator = phoneBook:GetValueIterator()
Return
Libraries.Containers.Iterator: The iterator of values.
HasKey(Key key)
This action determines if the hash table contains a certain key.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:HasKey("Jane")
Parameters
- Libraries.Language.Object: The key to find.
Return
boolean: True if the key is in the hash table and false if it is not.
HasValue(Value value)
This action determines if the hash table contains a certain value.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:HasValue(2626984)
Parameters
- Libraries.Language.Object: The value to find.
Return
boolean: True if the value is in the hash table and false if it is not.
IsEmpty()
This action determines if a hash table is empty.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean empty = phoneBook:IsEmpty()
Return
boolean: True if the hash table is empty and false if it contains any items.
RemoveAllKeys(Key key)
This action removes a key-value pair given a key. As the HashTable class requires unique values, this action really only removes the one value
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllKeys("Jane")
Parameters
- Libraries.Language.Object: The key of the key-value pair to be removed.
Return
boolean: True if any key-value pairs were removed and false if none were removed.
RemoveAllValues(Value value)
This action removes all instances of a key-value pair given a key. As the HashTable class requires all values are unique, this only removes at most one value.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllValues(2626984)
Parameters
Return
boolean: True if any key-value pairs were removed and false if none were removed.
RemoveKey(Key key)
This action removes a key-value pair given a key.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
integer value = phoneBook:RemoveKey("Jane")
Parameters
- Libraries.Language.Object: The key of the key-value pair to be removed.
Return
Libraries.Language.Object: The value that was removed.
RemoveValue(Value value)
This action removes a key-value pair given a value.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
text key = phoneBook:RemoveValue(2626984)
Parameters
- Libraries.Language.Object: The value of the key-value pair to be removed.
Return
Libraries.Language.Object: The key that was removed.
Set(Key key, Value value)
This action adds an item to the hash table, given the key-value pair. It functions identically to the add action and exists only as a convenience action, given that there are also actions named with the prefix Get.
Example Code
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Parameters
- Libraries.Language.Object: The key used to access the value.
- Libraries.Language.Object: The value to be stored.
SetSize(integer size)
This action sets the size of the hash table. If this table already has entries, it forces it to resize, manually rehashing all objects. For this reason, we recommend selecting a size before adding items to the table.
Example Code
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:SetSize(20)
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()