Luna::HashMap
An container that contains key-value pairs with unique keys using open-addressing hashing algorithm.
template <typename _Kty, typename _Ty, typename _Hash, typename _KeyEqual, typename _Alloc>
class Luna::HashMap
Remark
LunaSDK provides two kinds of hashing-based containers: open-addressing containers and closed-addressing containers. The following containers are open-addressing containers, implemented using Robinhood hashing:
-
SelfIndexedHashmap The following containers are closed-addressing containers, implemented using buckets and per-bucket linked-lists:
Open addressing (also known as closed hashing) algorithms store elements directly in hash table arrays, while closed addressing (also known as open hashing) algorithms allocate dedicated memory for every element, and stores pointers to such elements in hash table arrays. In open-addressing containers, one hash table slot can only store on element, the second element with the same hash value must be relocated to another empty slot; in closed-addressing containers, all elements with the same hash value can be stored in the same hash table slot, usually stored as linked lists. See Open vs Closed Addressing for a detailed comparison of open addressing and closed addressing.
Prefer HashMap and HashSet instead of UnorderedMap and UnorderedSet, since it performs better in memory fragmentation, memory locality and cache performance. Use UnorderedMap and UnorderedSet if you have the following requirements:
-
You want to insert multiple elements with the same key to the map, which is only supported by closed-addressing maps. Use UnorderedMultiMap, SelfIndexedUnorderedMultiMap and UnorderedMultiSet in such case.
-
You element type has very big size, usually larger than 256, causing allocating element memory in data table become unacceptable because it will waste a lot of memory when the load factor is low. Closed-addressing maps only allocate memory for alive elements, making it consuming much less memory than open-addressing maps when the element size is big. Closed-addressing maps also support extracting element nodes from one map and insert them to another maps without the need of allocating memory for elements, making it efficient to transfer elements between maps.
Member functions
-
Constructs an empty map.
-
HashMap(const allocator_type &alloc)
Constructs an empty map with an custom allocator.
-
Constructs a map by coping elements from another map.
-
HashMap(const HashMap &rhs, const allocator_type &alloc)
Constructs a map with an custom allocator and with elements copied from another map.
-
Constructs a map by moving elements from another map.
-
HashMap(HashMap &&rhs, const allocator_type &alloc)
Constructs a map with an custom allocator and with elements moved from another map.
-
HashMap & operator=(const HashMap &rhs)
Replaces elements of the map by coping elements from another map.
-
HashMap & operator=(HashMap &&rhs)
Replaces elements of the map by moving elements from another map.
-
Gets one iterator to the first element of the map.
-
Gets one constant iterator to the first element of the map.
-
Gets one constant iterator to the first element of the map.
-
Gets one iterator to the one past last element of the map.
-
Gets one constant iterator to the one past last element of the map.
-
Gets one constant iterator to the one past last element of the map.
-
Checks whether this map is empty, that is, the size of this map is
0
. -
Gets the size of the map, that is, the number of elements in the map.
-
Gets the capacity of the map, that is, the number of elements the hash table can hold before expanding the hash table.
-
Gets the hash table size of the map, that is, the number of slots of the hash table array.
-
Gets the load factor of the map, which can be computed by
(f32)size() / (f32)hash_table_size()
. -
Gets the maximum load factor allowed for the map.
-
Sets the maximum load factor allowed for the map.
-
Removes all elements in the map.
-
Reduces the hash table size to a minimum value that satisfy the maximum load factor limitation.
-
Gets the hash function used by this map.
-
Gets the equality comparison function used by this map.
-
void rehash(usize new_data_table_size)
Changes the data table size and rehashes all elements to insert them to the new data table.
-
Expands the data table size to the specified value.
-
iterator find(const key_type &key)
Finds the specified element in the map.
-
const_iterator find(const key_type &key) const
Finds the specified element in the map.
-
usize count(const key_type &key) const
Gets the number of elements whose key is equal to the specified key.
-
bool contains(const key_type &key) const
Checks whether at least one element with the specified key exists.
-
Pair< iterator, bool > insert(const value_type &value)
Inserts the specified key-value pair to the map.
-
Pair< iterator, bool > insert(value_type &&value)
Inserts the specified key-value pair to the map.
-
Pair< iterator, bool > insert_or_assign(const key_type &key, _M &&value)
Assigns the value to the element with the specified key, or inserts the key-value pair to the map if such element is not found.
-
Pair< iterator, bool > insert_or_assign(key_type &&key, _M &&value)
Assigns the value to the element with the specified key, or inserts the key-value pair to the map if such element is not found.
-
Pair< iterator, bool > emplace(_Args &&... args)
Constructs one element directly in the map using the provided arguments.
-
iterator erase(const_iterator pos)
Removes one element from the map.
-
usize erase(const key_type &key)
Removes elements with the specified key from the map.
-
Swaps elements of this map with the specified map.
-
allocator_type get_allocator() const
Gets the allocator used by this map.