|
GCSIM
|
implementation of the hash_map; using constant number of buckets and chaining. More...
#include <hash-map.hpp>
Public Member Functions | |
| hash_map () | |
| creates the instance of the hash_map. | |
| hash_map (size_t hash_map_capacity) | |
| creates the instance of the hash_map. | |
| ~hash_map () | |
| frees the memory allocated for the buckets. | |
| hash_map (const hash_map &)=delete | |
| deleted copy constructor. | |
| hash_map & | operator= (const hash_map &)=delete |
| deleted assignment operator. | |
| hash_map (hash_map &&other) noexcept | |
| constructs the hash_map instance from an existing one. | |
| hash_map & | operator= (hash_map &&other) noexcept |
| constructs new hash_map by assigning it an existing one. | |
| template<typename KK , typename VV > requires std::is_constructible_v<K, KK&&> && std::is_constructible_v<V, VV&&> | |
| void | insert (KK &&key, VV &&value) |
| inserts new (key, value) pair; or updates value if key already exists. | |
| V * | find (const K &key) noexcept |
| looks up element in a hash_map. | |
| const V * | find (const K &key) const noexcept |
| looks up element in a hash_map. | |
| bool | erase (const K &key) |
| removes the (key, value) pair from the hash_map. | |
| bool | contains (const K &key) const noexcept |
| looks up if element exists in a hash_map. | |
| V & | operator[] (const K &key) |
| gets the value for the specific key. | |
| const V & | operator[] (const K &key) const |
| gets the value for the specific key. | |
| map_entry ** | get_buckets () noexcept |
| gets the first bucket | |
| size_t | get_size () const noexcept |
| gets the size of the hash_map. | |
| size_t | get_capacity () const noexcept |
| gets the capacity of the hash_map. | |
| bool | empty () const noexcept |
| checks if the hash_map is empty. | |
| void | clear () |
| clears all entries from the hash_map. | |
Private Types | |
| using | map_entry = hash_map_entry< K, V > |
Private Member Functions | |
| size_t | calculate_bucket (const K &key) const noexcept |
| calculates the index of the bucket. | |
| void | delete_entries_from_bucket (map_entry *head) noexcept |
| frees all entries from a bucket. | |
| void | clear_buckets () noexcept |
| frees all entries from each bucket. | |
| double | load_factor () const noexcept |
| calculates the current load factor. | |
| void | resize () |
| resizes the hash_map and rehashes all entries. | |
Private Attributes | |
| map_entry ** | buckets |
| list containing the linked-list of entries. | |
| size_t | size |
| number of entries in the hash_map. | |
| size_t | capacity |
| number of buckets. | |
| Hash | hash_function |
| hash function for key hashing. | |
implementation of the hash_map; using constant number of buckets and chaining.
| K | - type of the key. |
| V | - type of the value. Hash - hash function; defaults to std::hash<K>. |
|
private |
|
inline |
creates the instance of the hash_map.
allocates DEFAULT_MAP_CAPACITY buckets. size defaults to 0. capacity set to DEFAULT_MAP_CAPACITY. sets each bucket linked list to nullptr.
|
inline |
creates the instance of the hash_map.
| hash_map_capacity | - number of buckets. |
| std::invalid_argument | if hash_map_capacity is 0. |
allocates hash_map_capacity buckets. size defaults to 0. sets each bucket linked list to nullptr.
|
inline |
frees the memory allocated for the buckets.
|
delete |
deleted copy constructor.
|
inlinenoexcept |
|
inlineprivatenoexcept |
calculates the index of the bucket.
| key | - const reference to a key. |
|
inline |
clears all entries from the hash_map.
|
inlineprivatenoexcept |
frees all entries from each bucket.
|
inlinenoexcept |
|
inlineprivatenoexcept |
frees all entries from a bucket.
| head | - pointer to the first element of the bucket linked list. |
|
inlinenoexcept |
|
inline |
removes the (key, value) pair from the hash_map.
| key | - const reference to a key. |
|
inlinenoexcept |
looks up element in a hash_map.
| key | - const reference to a key. |
|
inlinenoexcept |
looks up element in a hash_map.
| key | - const reference to a key. |
|
inlinenoexcept |
gets the first bucket
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
inserts new (key, value) pair; or updates value if key already exists.
| KK | - Key forwarding type. |
| VV | - Value forwarding type. |
| key | - rvalue of the key. |
| value | - rvalue of the value. |
|
inlineprivatenoexcept |
calculates the current load factor.
|
delete |
deleted assignment operator.
|
inline |
gets the value for the specific key.
| key | - const reference to a key of the element. |
| std::out_of_range | if key doesn't exist. |
|
inline |
gets the value for the specific key.
| key | - const reference to a key of the element. |
| std::out_of_range | if key doesn't exist. |
|
inlineprivate |
resizes the hash_map and rehashes all entries.
doubles the capacity and redistributes all entries.
|
private |
list containing the linked-list of entries.
|
private |
number of buckets.
|
private |
hash function for key hashing.
|
private |
number of entries in the hash_map.