34#include "nfx/detail/containers/CompilerSupport.h"
36#include <nfx/Hashing.h>
43#include <initializer_list>
53namespace nfx::containers
68 template <
typename TKey,
70 hashing::Hash32or64 HashType = uint32_t,
71 HashType Seed = (
sizeof( HashType ) == 4 ? hashing::constants::FNV_OFFSET_BASIS_32 : hashing::constants::FNV_OFFSET_BASIS_64 ),
72 typename THasher = hashing::Hasher<HashType, Seed>,
73 typename KeyEqual = std::equal_to<>>
80 static_assert( std::is_same_v<HashType, uint32_t> || std::is_same_v<HashType, uint64_t>,
81 "HashType must be uint32_t or uint64_t" );
83 static_assert( std::is_invocable_r_v<HashType, THasher, TKey>,
84 "THasher must be callable with TKey and return HashType" );
86 static_assert( std::is_invocable_r_v<bool, KeyEqual, TKey, TKey>,
87 "KeyEqual must be callable with two TKey arguments and return bool" );
144 inline FastHashMap( std::initializer_list<std::pair<TKey, TValue>> init );
152 template <
typename InputIt>
198 template <typename KeyType = TKey>
199 [[nodiscard]] inline TValue*
find( const KeyType& key ) noexcept;
207 template <typename KeyType = TKey>
208 [[nodiscard]] inline const TValue*
find( const KeyType& key ) const noexcept;
217 template <typename KeyType = TKey>
218 [[nodiscard]] inline
bool contains( const KeyType& key ) const noexcept;
226 inline TValue&
at( const TKey& key );
234 inline const TValue&
at( const TKey& key ) const;
244 inline TValue& operator[]( const TKey& key );
254 inline TValue& operator[]( TKey&& key );
263 template <typename KeyType = TKey>
264 inline TValue&
at( const KeyType& key );
273 template <typename KeyType = TKey>
274 inline const TValue&
at( const KeyType& key ) const;
287 inline
bool insert( const TKey& key, const TValue& value );
296 inline
bool insert( const TKey& key, TValue&& value );
305 inline
bool insert( TKey&& key, TValue&& value );
338 template <typename... Args>
339 inline
void emplace( const TKey& key, Args&&... args );
347 template <typename... Args>
348 inline
void emplace( TKey&& key, Args&&... args );
358 template <typename... Args>
369 template <typename... Args>
387 template <typename KeyType = TKey>
388 inline
bool erase( const KeyType& key ) noexcept;
420 template <typename KeyType = TKey>
421 [[nodiscard]] inline std::optional<std::pair<TKey, TValue>>
extract( const KeyType& key );
447 [[nodiscard]] inline
size_t size() const noexcept;
453 [[nodiscard]] inline
size_t capacity() const noexcept;
459 [[nodiscard]] inline
bool isEmpty() const noexcept;
520 [[nodiscard]]
bool operator==( const
FastHashMap& other ) const noexcept;
532 std::optional<std::pair<TKey, TValue>> data;
541 static constexpr size_t INITIAL_CAPACITY = 32;
546 static constexpr size_t MAX_LOAD_FACTOR_PERCENT = 75;
551 std::vector<Bucket> m_buckets;
554 size_t m_capacity{ INITIAL_CAPACITY };
555 size_t m_mask{ INITIAL_CAPACITY - 1 };
560 NFX_CONTAINERS_NO_UNIQUE_ADDRESS hasher m_hasher;
565 NFX_CONTAINERS_NO_UNIQUE_ADDRESS KeyEqual m_keyEqual;
577 template <
typename ValueType>
578 inline void insertOrAssignInternal(
const TKey& key, ValueType&& value );
586 template <
typename ValueType>
587 inline void insertOrAssignInternal( TKey&& key, ValueType&& value );
593 inline bool shouldResize() const noexcept;
598 inline
void resize();
604 inline
void eraseAtPosition(
size_t pos ) noexcept;
614 template <typename KeyType1, typename KeyType2>
615 inline
bool keysEqual( const KeyType1& k1, const KeyType2& k2 ) const noexcept;
627 friend class ConstIterator;
716 inline void skipToOccupied();
722 Bucket* m_bucket =
nullptr;
723 Bucket* m_end =
nullptr;
735 friend class FastHashMap;
830 inline void skipToOccupied();
836 const Bucket* m_bucket =
nullptr;
837 const Bucket* m_end =
nullptr;
842#include "nfx/detail/containers/FastHashMap.inl"
std::pair< Iterator, bool > tryEmplace(const TKey &key, Args &&... args)
Try to emplace a value if key doesn't exist.
ConstIterator cend() const noexcept
Get const iterator to end (explicit const).
bool insert(const TKey &key, const TValue &value)
Insert a key-value pair only if key doesn't exist (copy semantics).
void merge(FastHashMap &other)
Merge another FastHashMap into this one.
void insertOrAssign(const TKey &key, TValue &&value)
Insert or update a key-value pair (move semantics).
TValue & at(const TKey &key)
Checked element access with bounds checking.
Iterator begin() noexcept
Get iterator to beginning of occupied buckets.
KeyEqual key_equal
Type alias for key equality comparator.
HashType hash_type
Type alias for hash type (uint32_t or uint64_t).
TValue * find(const KeyType &key) noexcept
Fast lookup with heterogeneous key types (C++ idiom: pointer return).
THasher hasher
Type alias for hasher type.
std::optional< std::pair< TKey, TValue > > extract(const KeyType &key)
Extract a key-value pair from the map without destroying it.
size_t capacity() const noexcept
Get the current capacity of the hash table.
bool erase(const KeyType &key) noexcept
Remove a key-value pair from the map.
void emplace(const TKey &key, Args &&... args)
Emplace a value in-place for the given key.
std::ptrdiff_t difference_type
Type alias for difference type.
void clear() noexcept
Clear all elements from the map.
ConstIterator const_iterator
Type alias for const iterator.
bool isEmpty() const noexcept
Check if the map is empty.
ConstIterator cbegin() const noexcept
Get const iterator to beginning of occupied buckets (explicit const).
TValue mapped_type
Type alias for mapped value type.
std::pair< const TKey, TValue > value_type
Type alias for key-value pair type.
size_t size_type
Type alias for size type.
void swap(FastHashMap &other) noexcept
Swap contents with another map.
Iterator end() noexcept
Get iterator to end (past last occupied bucket).
Iterator iterator
Type alias for iterator.
FastHashMap(InputIt first, InputIt last)
Construct map from iterator range.
void reserve(size_t minCapacity)
Reserve capacity for at least the specified number of elements.
bool contains(const KeyType &key) const noexcept
Check if a key exists in the map.
FastHashMap(FastHashMap &&) noexcept
Move constructor.
FastHashMap()
Default constructor with initial capacity of 32 elements.
TKey key_type
Type alias for key type.
FastHashMap(size_t initialCapacity)
Constructor with specified initial capacity.
size_t size() const noexcept
Get the number of elements in the map.
FastHashMap(std::initializer_list< std::pair< TKey, TValue > > init)
Construct map from initializer_list.
Iterator for HashMap that skips empty buckets.
Iterator & operator++()
Pre-increment operator to advance to next occupied bucket.
Iterator()=default
Default constructor creates an invalid iterator.
std::forward_iterator_tag iterator_category
STL iterator category (forward iterator).
std::ptrdiff_t difference_type
STL iterator difference type.
reference operator*() const
Dereference operator to access key-value pair.
Iterator(Bucket *bucket, Bucket *end)
Construct iterator from bucket range.
Iterator operator++(int)
Post-increment operator to advance to next occupied bucket.
bool operator==(const Iterator &other) const
Equality comparison operator.
value_type * pointer
STL iterator pointer type.
std::pair< const TKey, TValue > value_type
STL iterator value type (key-value pair).
pointer operator->() const
Arrow operator to access key-value pair members.
bool operator!=(const Iterator &other) const
Inequality comparison operator.
value_type & reference
STL iterator reference type.
Const iterator for HashMap that skips empty buckets.
bool operator!=(const ConstIterator &other) const
Inequality comparison operator.
ConstIterator(const Iterator &it)
Convert from non-const iterator.
std::pair< const TKey, TValue > value_type
STL iterator value type (const key-value pair).
ConstIterator(const Bucket *bucket, const Bucket *end)
Construct const iterator from bucket range.
std::forward_iterator_tag iterator_category
STL iterator category (forward iterator).
std::ptrdiff_t difference_type
STL iterator difference type.
pointer operator->() const
Arrow operator to access key-value pair members.
bool operator==(const ConstIterator &other) const
Equality comparison operator.
const value_type * pointer
STL iterator pointer type.
ConstIterator()=default
Default constructor creates an invalid iterator.
ConstIterator operator++(int)
Post-increment operator to advance to next occupied bucket.
const value_type & reference
STL iterator reference type.
reference operator*() const
Dereference operator to access key-value pair.
ConstIterator & operator++()
Pre-increment operator to advance to next occupied bucket.