35#include "nfx/detail/containers/CompilerSupport.h"
37#include <nfx/Hashing.h>
44#include <initializer_list>
53namespace nfx::containers
72 template <
typename TKey,
74 hashing::Hash32or64 HashType = uint32_t,
75 HashType Seed = (
sizeof( HashType ) == 4 ? hashing::constants::FNV_OFFSET_BASIS_32 : hashing::constants::FNV_OFFSET_BASIS_64 ),
76 typename THasher = hashing::Hasher<HashType, Seed>,
77 typename KeyEqual = std::equal_to<>>
84 static_assert( std::is_same_v<HashType, uint32_t> || std::is_same_v<HashType, uint64_t>,
85 "HashType must be uint32_t or uint64_t" );
87 static_assert( std::is_invocable_r_v<HashType, THasher, TKey>,
88 "THasher must be callable with TKey and return HashType" );
90 static_assert( std::is_invocable_r_v<bool, KeyEqual, TKey, TKey>,
91 "KeyEqual must be callable with two TKey arguments and return bool" );
156 template <
typename InputIt>
206 template <
typename KeyType = TKey>
207 [[nodiscard]]
inline TValue*
find(
const KeyType& key )
noexcept;
215 template <
typename KeyType = TKey>
216 [[nodiscard]]
inline const TValue*
find(
const KeyType& key )
const noexcept;
225 template <
typename KeyType = TKey>
226 [[nodiscard]]
inline bool contains(
const KeyType& key )
const noexcept;
234 inline TValue&
at(
const TKey& key );
242 inline const TValue&
at(
const TKey& key )
const;
271 template <
typename KeyType = TKey>
272 inline TValue&
at(
const KeyType& key );
281 template <
typename KeyType = TKey>
282 inline const TValue&
at(
const KeyType& key )
const;
296 inline bool insert(
const TKey& key,
const TValue& value );
306 inline bool insert(
const TKey& key, TValue&& value );
316 inline bool insert( TKey&& key, TValue&& value );
356 template <
typename... Args>
357 inline void emplace(
const TKey& key, Args&&... args );
366 template <
typename... Args>
367 inline void emplace( TKey&& key, Args&&... args );
378 template <
typename... Args>
379 inline std::pair<Iterator, bool>
tryEmplace(
const TKey& key, Args&&... args );
390 template <
typename... Args>
391 inline std::pair<Iterator, bool>
tryEmplace( TKey&& key, Args&&... args );
411 template <
typename KeyType = TKey>
412 inline bool erase(
const KeyType& key )
noexcept;
445 template <typename KeyType = TKey>
446 [[nodiscard]] inline std::optional<std::pair<TKey, TValue>>
extract( const KeyType& key );
472 [[nodiscard]] inline
size_t size() const noexcept;
478 [[nodiscard]] inline
size_t capacity() const noexcept;
484 [[nodiscard]] inline
bool isEmpty() const noexcept;
557 std::pair<TKey, TValue> data;
581 static constexpr size_t INITIAL_CAPACITY = 32;
586 static constexpr size_t MAX_LOAD_FACTOR_PERCENT = 75;
591 std::vector<Bucket> m_buckets;
596 size_t m_capacity{ INITIAL_CAPACITY };
597 size_t m_mask{ INITIAL_CAPACITY - 1 };
602 NFX_CONTAINERS_NO_UNIQUE_ADDRESS hasher m_hasher;
607 NFX_CONTAINERS_NO_UNIQUE_ADDRESS KeyEqual m_keyEqual;
619 template <
typename ValueType>
620 inline void insertOrAssignInternal(
const TKey& key, ValueType&& value );
628 template <
typename ValueType>
629 inline void insertOrAssignInternal( TKey&& key, ValueType&& value );
635 inline bool shouldResize() const noexcept;
641 inline
void resize();
647 inline
void eraseAtPosition(
size_t pos ) noexcept;
653 inline
void unlinkNode( Node* node ) noexcept;
659 inline
void appendNode( Node* node ) noexcept;
669 template <typename KeyType1, typename KeyType2>
670 inline
bool keysEqual( const KeyType1& k1, const KeyType2& k2 ) const noexcept;
682 friend class ConstIterator;
683 friend class OrderedHashMap;
784 inline Iterator( Node* node,
const OrderedHashMap* container );
786 Node* m_node =
nullptr;
787 const OrderedHashMap* m_container =
nullptr;
799 friend class OrderedHashMap;
906 inline ConstIterator(
const Node* node,
const OrderedHashMap* container );
908 const Node* m_node =
nullptr;
909 const OrderedHashMap* m_container =
nullptr;
914#include "nfx/detail/containers/OrderedHashMap.inl"
const TValue * find(const KeyType &key) const noexcept
Fast const lookup with heterogeneous key types (C++ idiom: pointer return).
ConstIterator cbegin() const noexcept
Get const iterator to first element in insertion order (explicit const).
std::pair< const TKey, TValue > value_type
Type alias for key-value pair type.
KeyEqual key_equal
Type alias for key equality comparator.
const TValue & at(const TKey &key) const
Checked const element access with bounds checking.
void swap(OrderedHashMap &other) noexcept
Swap contents with another map.
TValue & operator[](TKey &&key)
STL-compatible subscript operator with move semantics.
void reserve(size_t minCapacity)
Reserve capacity for at least the specified number of elements.
THasher hasher
Type alias for hasher type.
OrderedHashMap(size_t initialCapacity)
Constructor with specified initial capacity.
Iterator end() noexcept
Get iterator to end (past last element in insertion order).
bool contains(const KeyType &key) const noexcept
Check if a key exists in the map.
OrderedHashMap(std::initializer_list< std::pair< TKey, TValue > > init)
Construct map from initializer_list.
std::pair< Iterator, bool > tryEmplace(const TKey &key, Args &&... args)
Try to emplace a value if key doesn't exist.
OrderedHashMap & operator=(OrderedHashMap &&other) noexcept
Move assignment operator.
TValue * find(const KeyType &key) noexcept
Fast lookup with heterogeneous key types (C++ idiom: pointer return).
Iterator iterator
Type alias for iterator.
TValue mapped_type
Type alias for mapped value type.
bool insert(const TKey &key, TValue &&value)
Insert a key-value pair only if key doesn't exist (move semantics).
TKey key_type
Type alias for key type.
std::ptrdiff_t difference_type
Type alias for difference type.
void merge(OrderedHashMap &other)
Merge elements from another map into this map.
OrderedHashMap(InputIt first, InputIt last)
Construct map from iterator range.
void emplace(const TKey &key, Args &&... args)
Emplace a value in-place for the given key.
void emplace(TKey &&key, Args &&... args)
Emplace a value in-place for the given key (move key).
void insertOrAssign(TKey &&key, TValue &&value)
Insert or update a key-value pair (perfect forwarding for both key and value).
void insertOrAssign(const TKey &key, const TValue &value)
Insert or update a key-value pair (copy semantics).
void clear() noexcept
Clear all elements from the map.
Iterator erase(ConstIterator first, ConstIterator last) noexcept
Erase range of elements.
bool isEmpty() const noexcept
Check if the map is empty.
Iterator erase(ConstIterator pos) noexcept
Erase element at iterator position.
OrderedHashMap()
Default constructor with initial capacity of 32 elements.
TValue & at(const TKey &key)
Checked element access with bounds checking.
Iterator begin() noexcept
Get iterator to first element in insertion order.
TValue & at(const KeyType &key)
Checked element access with bounds checking.
bool erase(const KeyType &key) noexcept
Remove a key-value pair from the map.
size_t size() const noexcept
Get the number of elements in the map.
OrderedHashMap & operator=(const OrderedHashMap &other)
Copy assignment operator.
ConstIterator const_iterator
Type alias for const iterator.
OrderedHashMap(OrderedHashMap &&other) noexcept
Move constructor.
ConstIterator cend() const noexcept
Get const iterator to end (explicit const).
bool insert(TKey &&key, TValue &&value)
Insert a key-value pair only if key doesn't exist (perfect forwarding).
const TValue & at(const KeyType &key) const
Checked const element access with bounds checking.
HashType hash_type
Type alias for hash type (uint32_t or uint64_t).
void insertOrAssign(const TKey &key, TValue &&value)
Insert or update a key-value pair (move semantics).
std::pair< Iterator, bool > tryEmplace(TKey &&key, Args &&... args)
Try to emplace a value if key doesn't exist.
std::optional< std::pair< TKey, TValue > > extract(const KeyType &key)
Extract and remove a key-value pair from the map.
TValue & operator[](const TKey &key)
STL-compatible subscript operator (insert-if-missing).
size_t size_type
Type alias for size type.
OrderedHashMap(const OrderedHashMap &other)
Copy constructor.
~OrderedHashMap()
Destructor.
bool insert(const TKey &key, const TValue &value)
Insert a key-value pair only if key doesn't exist (copy semantics).
size_t capacity() const noexcept
Get the current capacity of the hash table.
Iterator for OrderedHashMap that follows insertion order.
value_type * pointer
STL iterator pointer type.
Iterator & operator++()
Pre-increment operator to advance to next element in insertion order.
Iterator & operator--()
Pre-decrement operator to move to previous element in insertion order.
Iterator operator++(int)
Post-increment operator to advance to next element in insertion order.
value_type & reference
STL iterator reference type.
std::bidirectional_iterator_tag iterator_category
STL iterator category (bidirectional iterator).
std::pair< const TKey, TValue > value_type
STL iterator value type (key-value pair).
reference operator*() const
Dereference operator to access key-value pair.
Iterator()=default
Default constructor creates an invalid iterator.
Iterator operator--(int)
Post-decrement operator to move to previous element in insertion order.
std::ptrdiff_t difference_type
STL iterator difference type.
Iterator(Node *node)
Construct iterator from node.
pointer operator->() const
Arrow operator to access key-value pair members.
bool operator!=(const Iterator &other) const
Inequality comparison operator.
bool operator==(const Iterator &other) const
Equality comparison operator.
Const iterator for OrderedHashMap that follows insertion order.
reference operator*() const
Dereference operator to access key-value pair.
ConstIterator(const Node *node)
Construct const iterator from node.
ConstIterator()=default
Default constructor creates an invalid iterator.
ConstIterator & operator--()
Pre-decrement operator to move to previous element in insertion order.
std::bidirectional_iterator_tag iterator_category
STL iterator category (bidirectional iterator).
std::ptrdiff_t difference_type
STL iterator difference type.
ConstIterator & operator++()
Pre-increment operator to advance to next element in insertion order.
const value_type & reference
STL iterator reference type.
std::pair< const TKey, TValue > value_type
STL iterator value type (const key-value pair).
ConstIterator operator++(int)
Post-increment operator to advance to next element in insertion order.
bool operator==(const ConstIterator &other) const
Equality comparison operator.
ConstIterator operator--(int)
Post-decrement operator to move to previous element in insertion order.
bool operator!=(const ConstIterator &other) const
Inequality comparison operator.
const value_type * pointer
STL iterator pointer type.
ConstIterator(const Iterator &it)
Convert from non-const iterator.
pointer operator->() const
Arrow operator to access key-value pair members.