39#include <initializer_list>
44namespace nfx::containers
64 template <
typename TKey,
67 typename KeyEqual = std::equal_to<>>
106 inline StackHashMap( std::initializer_list<std::pair<TKey, TValue>> init );
114 template <
typename InputIt>
126 [[nodiscard]]
inline bool isEmpty() const noexcept;
133 [[nodiscard]] inline
size_t size() const noexcept;
140 [[nodiscard]] static constexpr
size_t stackCapacity() noexcept {
return N; }
152 inline TValue&
at(
const TKey& key );
160 inline const TValue&
at(
const TKey& key )
const;
196 inline std::pair<value_type*, bool>
insert(
const std::pair<TKey, TValue>& value );
206 inline std::pair<value_type*, bool>
insert( std::pair<TKey, TValue>&& value );
214 template <
typename... Args>
215 inline std::pair<value_type*, bool>
emplace( Args&&... args );
249 inline size_t erase(
const TKey& key );
265 [[nodiscard]] inline std::optional<std::pair<TKey, TValue>>
extract( const TKey& key );
293 [[nodiscard]] inline TValue*
find( const TKey& key ) noexcept;
301 [[nodiscard]] inline const TValue*
find( const TKey& key ) const noexcept;
309 [[nodiscard]] inline
size_t count( const TKey& key ) const;
317 [[nodiscard]] inline
bool contains( const TKey& key ) const;
327 template <typename K>
328 [[nodiscard]] inline
bool contains( const K& key ) const
329 requires requires( KeyEqual eq, const K& k, const TKey& t ) { eq( k, t ); };
343 template <
typename Func>
354 template <
typename Func>
367 std::optional<std::pair<TKey, TValue>> data;
373 std::array<StackEntry, N> m_stack;
383 std::unique_ptr<FastHashMap<TKey, TValue>> m_heap;
389 [[nodiscard]]
inline bool isOnStack() const noexcept;
395 inline
void transitionToHeap();
399#include "nfx/detail/containers/StackHashMap.inl"
Hash map with Robin Hood hashing and heterogeneous lookup.
std::pair< value_type *, bool > insert(const std::pair< TKey, TValue > &value)
Insert a key-value pair (copy semantics).
TValue & operator[](const TKey &key)
STL-compatible subscript operator (insert-if-missing).
std::optional< std::pair< TKey, TValue > > extract(const TKey &key)
Extract a key-value pair from the map without destroying it.
bool contains(const TKey &key) const
Check if a key exists in the map.
StackHashMap(InputIt first, InputIt last)
Construct map from iterator range.
TValue * find(const TKey &key) noexcept
Find a value by key.
bool isEmpty() const noexcept
Check if the map is empty.
void insertOrAssign(const TKey &key, TValue &&value)
Insert or assign a key-value pair (move value).
const TValue & at(const TKey &key) const
Checked const element access with bounds checking.
std::pair< const TKey, TValue > value_type
Type alias for key-value pair type.
void insertOrAssign(TKey &&key, TValue &&value)
Insert or assign a key-value pair (move key and value).
std::pair< value_type *, bool > emplace(Args &&... args)
Emplace a key-value pair with in-place construction.
void merge(StackHashMap &other)
Merge another StackHashMap into this one.
void forEach(Func &&func) const
Apply a function to each key-value pair (const version).
size_t size_type
Type alias for size type.
size_t erase(const TKey &key)
Erase element by key.
TValue mapped_type
Type alias for mapped value type.
KeyEqual key_equal
Type alias for key equality comparator.
void forEach(Func &&func)
Apply a function to each key-value pair.
std::ptrdiff_t difference_type
Type alias for difference type.
void insertOrAssign(const TKey &key, const TValue &value)
Insert or assign a key-value pair (copy value).
TValue & at(const TKey &key)
Checked element access with bounds checking.
size_t size() const noexcept
Get the number of elements in the map.
size_t count(const TKey &key) const
Count occurrences of key in map.
TValue & operator[](TKey &&key)
STL-compatible subscript operator with move semantics.
StackHashMap()
Default constructor with empty stack storage.
void clear() noexcept
Clear all elements from the map.
static constexpr size_t stackCapacity() noexcept
Get the maximum stack capacity before heap allocation.
std::pair< value_type *, bool > insert(std::pair< TKey, TValue > &&value)
Insert a key-value pair (move semantics).
TKey key_type
Type alias for key type.
StackHashMap(std::initializer_list< std::pair< TKey, TValue > > init)
Construct map from initializer_list.