|
nfx-containers 0.6.0
Modern C++20 header-only library providing high-performance hash containers with Robin Hood and perfect hashing
|
Hash map with small buffer optimization for stack-allocated storage. More...
#include <nfx/containers/StackHashMap.h>
Public Types | |
| using | key_type = TKey |
| Type alias for key type. | |
| using | mapped_type = TValue |
| Type alias for mapped value type. | |
| using | value_type = std::pair<const TKey, TValue> |
| Type alias for key-value pair type. | |
| using | size_type = size_t |
| Type alias for size type. | |
| using | difference_type = std::ptrdiff_t |
| Type alias for difference type. | |
| using | key_equal = KeyEqual |
| Type alias for key equality comparator. | |
Public Member Functions | |
| StackHashMap () | |
| Default constructor with empty stack storage. | |
| StackHashMap (std::initializer_list< std::pair< TKey, TValue > > init) | |
| Construct map from initializer_list. | |
| template<typename InputIt> | |
| StackHashMap (InputIt first, InputIt last) | |
| Construct map from iterator range. | |
| bool | isEmpty () const noexcept |
| Check if the map is empty. | |
| size_t | size () const noexcept |
| Get the number of elements in the map. | |
| TValue & | at (const TKey &key) |
| Checked element access with bounds checking. | |
| const TValue & | at (const TKey &key) const |
| Checked const element access with bounds checking. | |
| TValue & | operator[] (const TKey &key) |
| STL-compatible subscript operator (insert-if-missing). | |
| TValue & | operator[] (TKey &&key) |
| STL-compatible subscript operator with move semantics. | |
| std::pair< value_type *, bool > | insert (const std::pair< TKey, TValue > &value) |
| Insert a key-value pair (copy semantics). | |
| std::pair< value_type *, bool > | insert (std::pair< TKey, TValue > &&value) |
| Insert a key-value pair (move semantics). | |
| template<typename... Args> | |
| std::pair< value_type *, bool > | emplace (Args &&... args) |
| Emplace a key-value pair with in-place construction. | |
| void | insertOrAssign (const TKey &key, const TValue &value) |
| Insert or assign a key-value pair (copy value). | |
| void | insertOrAssign (const TKey &key, TValue &&value) |
| Insert or assign a key-value pair (move value). | |
| void | insertOrAssign (TKey &&key, TValue &&value) |
| Insert or assign a key-value pair (move key and value). | |
| size_t | erase (const TKey &key) |
| Erase element by key. | |
| void | clear () noexcept |
| Clear all elements from the map. | |
| std::optional< std::pair< TKey, TValue > > | extract (const TKey &key) |
| Extract a key-value pair from the map without destroying it. | |
| void | merge (StackHashMap &other) |
| Merge another StackHashMap into this one. | |
| void | merge (StackHashMap &&other) |
| Merge another StackHashMap into this one (rvalue overload). | |
| TValue * | find (const TKey &key) noexcept |
| Find a value by key. | |
| const TValue * | find (const TKey &key) const noexcept |
| Find a value by key (const version). | |
| size_t | count (const TKey &key) const |
| Count occurrences of key in map. | |
| bool | contains (const TKey &key) const |
| Check if a key exists in the map. | |
| template<typename K> requires requires( KeyEqual eq, const K& k, const TKey& t ) { eq( k, t ); } | |
| bool | contains (const K &key) const |
| Check if a key exists in the map (heterogeneous lookup). | |
| template<typename Func> | |
| void | forEach (Func &&func) |
| Apply a function to each key-value pair. | |
| template<typename Func> | |
| void | forEach (Func &&func) const |
| Apply a function to each key-value pair (const version). | |
Static Public Member Functions | |
| static constexpr size_t | stackCapacity () noexcept |
| Get the maximum stack capacity before heap allocation. | |
Hash map with small buffer optimization for stack-allocated storage.
| TKey | Key type (supports heterogeneous lookup for compatible types) |
| TValue | Value type |
| N | Maximum stack capacity before heap allocation (default: 8) |
| KeyEqual | Key equality comparator (default: std::equal_to<> for transparent comparison) |
StackHashMap uses a hybrid storage strategy:
This is ideal for small maps (config options, local caches) where heap allocation overhead would dominate performance. The transition to heap is transparent and automatic.
Definition at line 68 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::difference_type = std::ptrdiff_t |
Type alias for difference type.
Definition at line 88 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::key_equal = KeyEqual |
Type alias for key equality comparator.
Definition at line 91 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::key_type = TKey |
Type alias for key type.
Definition at line 76 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::mapped_type = TValue |
Type alias for mapped value type.
Definition at line 79 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::size_type = size_t |
Type alias for size type.
Definition at line 85 of file StackHashMap.h.
| using nfx::containers::StackHashMap< TKey, TValue, N, KeyEqual >::value_type = std::pair<const TKey, TValue> |
Type alias for key-value pair type.
Definition at line 82 of file StackHashMap.h.
|
inline |
Construct map from initializer_list.
| init | Initializer list of key/value pairs |
|
inline |
Construct map from iterator range.
| InputIt | Input iterator type (must dereference to std::pair-like type) |
| first | Beginning of range to copy from |
| last | End of range (exclusive) |
|
inline |
Checked element access with bounds checking.
| key | The key to access |
| std::out_of_range | if key is not found |
|
inline |
Checked const element access with bounds checking.
| key | The key to access |
| std::out_of_range | if key is not found |
|
inlinenoexcept |
Clear all elements from the map.
Clears stack storage or heap storage, does not deallocate heap map
|
inlinenodiscard |
Check if a key exists in the map (heterogeneous lookup).
| K | Key type (supports heterogeneous lookup for compatible types) |
| key | The key to search for |
|
inlinenodiscard |
Check if a key exists in the map.
| key | The key to search for |
|
inlinenodiscard |
Count occurrences of key in map.
| key | The key to search for |
|
inline |
Emplace a key-value pair with in-place construction.
| Args | Argument types for constructing the pair |
| args | Arguments forwarded to construct the pair |
|
inline |
Erase element by key.
| key | The key to erase |
|
inlinenodiscard |
Extract a key-value pair from the map without destroying it.
| key | The key to extract |
Removes the element from the map and returns the key-value pair. If the key is not found, returns std::nullopt.
|
inlinenodiscardnoexcept |
Find a value by key (const version).
| key | The key to search for |
|
inlinenodiscardnoexcept |
Find a value by key.
| key | The key to search for |
|
inline |
Apply a function to each key-value pair.
| Func | Function type with signature void(const TKey&, TValue&) or compatible |
| func | Function to apply to each element |
Iterates over all elements in unspecified order. For stack storage, iteration order matches insertion order (up to N elements). For heap storage, iteration order is unspecified (hash table order).
|
inline |
Apply a function to each key-value pair (const version).
| Func | Function type with signature void(const TKey&, const TValue&) or compatible |
| func | Function to apply to each element |
Iterates over all elements in unspecified order. For stack storage, iteration order matches insertion order (up to N elements). For heap storage, iteration order is unspecified (hash table order).
|
inline |
Insert a key-value pair (copy semantics).
| value | The key-value pair to insert |
If key already exists, returns pointer to existing element and false. Otherwise inserts new element and returns pointer with true. May trigger transition to heap storage.
|
inline |
Insert a key-value pair (move semantics).
| value | The key-value pair to insert (moved) |
If key already exists, returns pointer to existing element and false. Otherwise inserts new element and returns pointer with true. May trigger transition to heap storage.
|
inline |
Insert or assign a key-value pair (copy value).
| key | The key to insert or assign to |
| value | The value to assign |
If key exists, assigns new value. Otherwise inserts new pair. May trigger transition to heap storage.
|
inline |
Insert or assign a key-value pair (move value).
| key | The key to insert or assign to |
| value | The value to assign (moved) |
If key exists, assigns new value. Otherwise inserts new pair. May trigger transition to heap storage.
|
inline |
Insert or assign a key-value pair (move key and value).
| key | The key to insert or assign to (moved) |
| value | The value to assign (moved) |
If key exists, assigns new value. Otherwise inserts new pair. May trigger transition to heap storage.
|
inlinenodiscardnoexcept |
Check if the map is empty.
|
inline |
Merge another StackHashMap into this one (rvalue overload).
| other | The map to merge from (elements are moved) |
Same as merge(StackHashMap&) but accepts rvalue references.
|
inline |
Merge another StackHashMap into this one.
| other | The map to merge from (elements are moved, not copied) |
Attempts to insert each element from other into this map. Elements that already exist in this map are left in other. After the operation, other contains only elements that were not inserted.
|
inline |
STL-compatible subscript operator (insert-if-missing).
| key | The key to access or insert |
If key doesn't exist, inserts default-constructed value. Requires TValue to be default-constructible. May trigger transition to heap storage if stack capacity exceeded.
|
inline |
STL-compatible subscript operator with move semantics.
| key | The key to access or insert (moved if new) |
If key doesn't exist, inserts default-constructed value. Requires TValue to be default-constructible. May trigger transition to heap storage if stack capacity exceeded.
|
inlinenodiscardnoexcept |
Get the number of elements in the map.
|
inlinestaticnodiscardconstexprnoexcept |
Get the maximum stack capacity before heap allocation.
Definition at line 140 of file StackHashMap.h.