Class HashMap
A hashtable Map of items.
A Map implmentation that stores items as keys using a table as a hashtable. This provides good addition, removal, and lookup characteristics.
Addition, deletion, and lookup of any element are all Θ(1) amortized.
HashMap implements all optional Map and Collection methods.
Implements: Map, Collection, Enumerable
Functions
| HashMap.fromArray(array) | Creates a new HashMap from a map-formatted array. |
| HashMap.fromPairs(collection) | Creates a new HashMap from a map-formatted Collection. |
| HashMap.fromTable(table) | Creates a new HashMap from a map-formatted table. |
| HashMap.new(collection) | Creates a new HashMap. |
Methods
| HashMap:Add(item, value) | Adds an item to the HashMap. |
| HashMap:AddAll(items, value) | Adds all provided items to the HashMap. |
| HashMap:Clear() | Removes everything from the HashMap. |
| HashMap:Contains(item) | Determines whether the HashMap contains an item. |
| HashMap:ContainsAll(items) | Determines whether the HashMap contains all of the provided items. |
| HashMap:ContainsAllKeys(keys) | Determines whether the HashMap contains all of the provided keys. |
| HashMap:ContainsAllValues(values) | Determines whether the HashMap contains all of the provided values. |
| HashMap:ContainsAny(items) | Determines whether the HashMap contains any of the provided items. |
| HashMap:ContainsAnyKeys(keys) | Determines whether the HashMap contains any of the provided keys. |
| HashMap:ContainsAnyValues(values) | Determines whether the HashMap contains any of the provided values. |
| HashMap:ContainsKey(key) | Determines whether the HashMap contains a key. |
| HashMap:ContainsValue(value) | Determines whether the HashMap contains a value. |
| HashMap:Count() | Gets the number of items in the HashMap. |
| HashMap:Empty() | Determines whether the HashMap contains no elements. |
| HashMap:Enumerator() | Creates an enumerator for the HashMap. |
| HashMap:Get(key) | Gets the value of the specified key. |
| HashMap:Keys() | Creates a Collection of this HashMap's keys. |
| HashMap:Pairs() | Creates a Collection of this HashMap's key value pairs. |
| HashMap:Remove(item) | Removes the specified item from the HashMap. |
| HashMap:RemoveAll(items) | Removes all provided items from the HashMap. |
| HashMap:RetainAll(items) | Removes all items except those provided from the HashMap. |
| HashMap:Set(key, value) | Sets the value of the specified key. |
| HashMap:ToArray() | Creates a new array-indexed table of this HashMap. |
| HashMap:ToTable() | Creates a new table of this HashMap. |
| HashMap:Values() | Creates a Collection of this HashMap's values. |
Functions
Methods- HashMap.fromArray(array)
-
Creates a new HashMap from a map-formatted array.
This constructor creates a new HashMap copy of an array of key value pairs. A
typical map-formatted array consists of key value pairs in the indices of
array. Each key value pair should be a table with index 1 as the key of the
pair and index 2 as the value of the pair.
Inherited from:
Parameters:
- array the map-formatted array to copy from
Returns:
-
the new HashMap
Raises:
- if a table is not provided
- if the table is not an array of key value pairs
- HashMap.fromPairs(collection)
-
Creates a new HashMap from a map-formatted Collection.
This constructor creates a new HashMap copy of a Collection of key value
pairs. A typical map-formatted Collection consists of keys value pairs. Each
key value pair should be a table with index 1 as the key of the pair and
index 2 as the value of the pair.
Inherited from:
Parameters:
- collection the map-formatted Collection to copy from
Returns:
-
the new HashMap
Raises:
- if a table is not provided
- if a Collection is not provided
- if the Collection doens't consist of key value pairs
- HashMap.fromTable(table)
-
Creates a new HashMap from a map-formatted table.
This constructor creates a new HashMap copy of the table using its keys. A
typical map-formatted table consists of keys that have a non-nil value.
Inherited from:
Parameters:
- table the map-formatted table to copy from
Returns:
-
the new HashMap
Raises:
if a table is not provided - HashMap.new(collection)
-
Creates a new HashMap.
Creates a HashMap copy of the provided Collection or array-indexed
table if one is provided, otherwise creates an empty HashMap.
Parameters:
- collection the Collection or table to copy
Returns:
-
the new HashMap
Methods
- HashMap:Add(item, value)
-
Adds an item to the HashMap.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
Parameters:
- item the item to add
- value
Returns:
-
true if the HashMap changed as a result, false otherwise
- HashMap:AddAll(items, value)
-
Adds all provided items to the HashMap.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
Parameters:
- items the Collection of items to add to this HashMap
- value
Returns:
-
true if the HashMap changed as a result, false otherwise
- HashMap:Clear()
-
Removes everything from the HashMap.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
- HashMap:Contains(item)
-
Determines whether the HashMap contains an item.
Inherited from:
Parameters:
- item the item to locate in the HashMap
Returns:
-
true if the item is in the HashMap, false otherwise
- HashMap:ContainsAll(items)
-
Determines whether the HashMap contains all of the provided items.
Checks for items provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- items the Collection of items to locate in this HashMap
Returns:
-
true if all items are in the HashMap, false otherwise
- HashMap:ContainsAllKeys(keys)
-
Determines whether the HashMap contains all of the provided keys.
Checks for keys provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
This is effectively the same as ContainsAll.
Inherited from:
Parameters:
- keys the Collection of keys to locate in this HashMap
Returns:
-
true if all keys are in the HashMap, false otherwise
- HashMap:ContainsAllValues(values)
-
Determines whether the HashMap contains all of the provided values.
Checks for values provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- values the Collection of values to locate in this HashMap
Returns:
-
true if all values are in the HashMap, false otherwise
- HashMap:ContainsAny(items)
-
Determines whether the HashMap contains any of the provided items.
Checks for items provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- items the Collection of items to locate in this HashMap
Returns:
-
true if any items are in the HashMap, false otherwise
- HashMap:ContainsAnyKeys(keys)
-
Determines whether the HashMap contains any of the provided keys.
Checks for keys provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
This is effectively the same as ContainsAny.
Inherited from:
Parameters:
- keys the Collection of keys to locate in this HashMap
Returns:
-
true if any keys are in the HashMap, false otherwise
- HashMap:ContainsAnyValues(values)
-
Determines whether the HashMap contains any of the provided values.
Checks for values provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- values the Collection of values to locate in this HashMap
Returns:
-
true if any values are in the HashMap, false otherwise
- HashMap:ContainsKey(key)
-
Determines whether the HashMap contains a key.
This is effectively the same as Contains.
Inherited from:
Parameters:
- key the key to locate in the HashMap
Returns:
-
true if the key is in the HashMap, false otherwise
- HashMap:ContainsValue(value)
-
Determines whether the HashMap contains a value.
Inherited from:
Parameters:
- value the value to locate in the HashMap
Returns:
-
true if the value is in the HashMap, false otherwise
- HashMap:Count()
-
Gets the number of items in the HashMap.
Inherited from:
Returns:
-
the number of items
- HashMap:Empty()
-
Determines whether the HashMap contains no elements.
Inherited from:
Returns:
-
true if the HashMap empty, false otherwise
- HashMap:Enumerator()
-
Creates an enumerator for the HashMap.
The enumerator can be used directly in a generic for loop similar to pairs
or ipairs.
Inherited from:
Returns:
- the enumerator generator
- the invariant state
- the control variable state
- HashMap:Get(key)
-
Gets the value of the specified key.
Inherited from:
Parameters:
- key the key to get
Returns:
-
the value associated with the key
- HashMap:Keys()
-
Creates a Collection of this HashMap's keys.
Inherited from:
Returns:
-
the Collection of keys
- HashMap:Pairs()
-
Creates a Collection of this HashMap's key value pairs.
Index 1 of each pair is the key and index 2 of each pair is the value.
Inherited from:
Returns:
-
the Collection of key value pairs
- HashMap:Remove(item)
-
Removes the specified item from the HashMap.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
Parameters:
- item the item to remove from the HashMap
Returns:
-
true if the HashMap changed as a result, false otherwise
- HashMap:RemoveAll(items)
-
Removes all provided items from the HashMap.
Removes each instance of a provided item.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
Parameters:
- items the Collection of items to remove from this HashMap
Returns:
-
true if the HashMap changed as a result, false otherwise
- HashMap:RetainAll(items)
-
Removes all items except those provided from the HashMap.
Retains only the items contained in the specified Collection. If there are
duplicates they are all kept.
This method is optional. All HashMap implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this HashMap.
Inherited from:
Parameters:
- items the Collection of items to retain in this HashMap
Returns:
-
true if the HashMap changed as a result, false otherwise
- HashMap:Set(key, value)
-
Sets the value of the specified key.
Inherited from:
Parameters:
- key the key to set
- value the value to set
Returns:
-
true if the value of the key changed, false otherwise
- HashMap:ToArray()
-
Creates a new array-indexed table of this HashMap.
The order of the array is the same as the order of the HashMap and uses the
same indexing.
Inherited from:
Returns:
-
the array indexed table
- HashMap:ToTable()
-
Creates a new table of this HashMap.
Lists, being ordered and linear, use no indices that are not array indices,
so this provides a table with all the same array indices as ToArray.
Inherited from:
Returns:
-
the table
- HashMap:Values()
-
Creates a Collection of this HashMap's values.
Inherited from:
Returns:
-
the Collection of values