35#include "nfx/detail/containers/CompilerSupport.h"
37#include <nfx/Hashing.h>
44#include <initializer_list>
53namespace nfx::containers
71 template <
typename TKey,
72 hashing::Hash32or64 HashType = uint32_t,
73 HashType Seed = (
sizeof( HashType ) == 4 ? hashing::constants::FNV_OFFSET_BASIS_32 : hashing::constants::FNV_OFFSET_BASIS_64 ),
74 typename THasher = hashing::Hasher<HashType, Seed>,
75 typename KeyEqual = std::equal_to<>>
82 static_assert( std::is_same_v<HashType, uint32_t> || std::is_same_v<HashType, uint64_t>,
83 "HashType must be uint32_t or uint64_t" );
85 static_assert( std::is_invocable_r_v<HashType, THasher, TKey>,
86 "THasher must be callable with TKey and return HashType" );
88 static_assert( std::is_invocable_r_v<bool, KeyEqual, TKey, TKey>,
89 "KeyEqual must be callable with two TKey arguments and return bool" );
151 template <
typename InputIt>
201 template <
typename KeyType = TKey>
202 [[nodiscard]]
inline const TKey*
find(
const KeyType& key )
const noexcept;
211 template <
typename KeyType = TKey>
212 [[nodiscard]]
inline bool contains(
const KeyType& key )
const noexcept;
222 template <
typename KeyType = TKey>
223 inline const TKey&
at(
const KeyType& key )
const;
256 template <
typename... Args>
267 template <
typename... Args>
268 inline std::pair<Iterator, bool>
tryEmplace( Args&&... args );
288 template <
typename KeyType = TKey>
289 inline bool erase(
const KeyType& key )
noexcept;
322 template <typename KeyType = TKey>
323 [[nodiscard]] inline std::optional<TKey>
extract( const KeyType& key );
349 [[nodiscard]] inline
size_t size() const noexcept;
355 [[nodiscard]] inline
size_t capacity() const noexcept;
361 [[nodiscard]] inline
bool isEmpty() const noexcept;
458 static constexpr size_t INITIAL_CAPACITY = 32;
463 static constexpr size_t MAX_LOAD_FACTOR_PERCENT = 75;
468 std::vector<Bucket> m_buckets;
473 size_t m_capacity{ INITIAL_CAPACITY };
474 size_t m_mask{ INITIAL_CAPACITY - 1 };
479 NFX_CONTAINERS_NO_UNIQUE_ADDRESS hasher m_hasher;
484 NFX_CONTAINERS_NO_UNIQUE_ADDRESS KeyEqual m_keyEqual;
495 inline bool insertInternal(
const TKey& key );
502 inline bool insertInternal( TKey&& key );
508 inline bool shouldResize() const noexcept;
514 inline
void resize();
520 inline
void eraseAtPosition(
size_t pos ) noexcept;
526 inline
void unlinkNode( Node* node ) noexcept;
532 inline
void appendNode( Node* node ) noexcept;
542 template <typename KeyType1, typename KeyType2>
543 inline
bool keysEqual( const KeyType1& k1, const KeyType2& k2 ) const noexcept;
555 friend class ConstIterator;
556 friend class OrderedHashSet;
657 inline Iterator( Node* node,
const OrderedHashSet* container );
659 Node* m_node =
nullptr;
660 const OrderedHashSet* m_container =
nullptr;
672 friend class OrderedHashSet;
779 inline ConstIterator(
const Node* node,
const OrderedHashSet* container );
781 const Node* m_node =
nullptr;
782 const OrderedHashSet* m_container =
nullptr;
787#include "nfx/detail/containers/OrderedHashSet.inl"
ConstIterator cend() const noexcept
Get const iterator to end (explicit const).
void reserve(size_t minCapacity)
Reserve capacity for at least the specified number of elements.
std::optional< TKey > extract(const KeyType &key)
Extract and remove a key from the set.
~OrderedHashSet()
Destructor.
ConstIterator cbegin() const noexcept
Get const iterator to first element in insertion order (explicit const).
Iterator begin() noexcept
Get iterator to first element in insertion order.
size_t size() const noexcept
Get the number of elements in the set.
std::ptrdiff_t difference_type
Type alias for difference type.
OrderedHashSet(std::initializer_list< TKey > init)
Construct set from initializer_list.
OrderedHashSet(size_t initialCapacity)
Constructor with specified initial capacity.
void swap(OrderedHashSet &other) noexcept
Swap contents with another set.
void merge(OrderedHashSet &other)
Merge elements from another set into this set.
OrderedHashSet(const OrderedHashSet &other)
Copy constructor.
KeyEqual key_equal
Type alias for key equality comparator.
OrderedHashSet(InputIt first, InputIt last)
Construct set from iterator range.
bool erase(const KeyType &key) noexcept
Remove a key from the set.
void clear() noexcept
Clear all elements from the set.
OrderedHashSet()
Default constructor with initial capacity of 32 elements.
HashType hash_type
Type alias for hash type (uint32_t or uint64_t).
bool insert(const TKey &key)
Insert a key into the set (copy semantics).
const TKey * find(const KeyType &key) const noexcept
Fast lookup with heterogeneous key types (C++ idiom: pointer return).
const TKey & at(const KeyType &key) const
Checked element access with bounds checking.
bool insert(TKey &&key)
Insert a key into the set (move semantics).
Iterator erase(ConstIterator pos) noexcept
Erase element at iterator position.
OrderedHashSet(OrderedHashSet &&other) noexcept
Move constructor.
std::pair< Iterator, bool > tryEmplace(Args &&... args)
Try to emplace a key if it doesn't exist.
TKey value_type
Type alias for value type (same as key for sets).
TKey key_type
Type alias for key type.
ConstIterator const_iterator
Type alias for const iterator.
Iterator end() noexcept
Get iterator to end (past last element in insertion order).
Iterator erase(ConstIterator first, ConstIterator last) noexcept
Erase range of elements.
size_t size_type
Type alias for size type.
THasher hasher
Type alias for hasher type.
OrderedHashSet & operator=(const OrderedHashSet &other)
Copy assignment operator.
size_t capacity() const noexcept
Get the current capacity of the hash table.
Iterator iterator
Type alias for iterator.
OrderedHashSet & operator=(OrderedHashSet &&other) noexcept
Move assignment operator.
bool isEmpty() const noexcept
Check if the set is empty.
bool emplace(Args &&... args)
Construct and insert a key in-place.
bool contains(const KeyType &key) const noexcept
Check if a key exists in the set.
Iterator for OrderedHashSet that follows insertion order.
const TKey & reference
STL iterator reference type.
std::bidirectional_iterator_tag iterator_category
STL iterator category (bidirectional iterator).
Iterator & operator--()
Pre-decrement operator to move to previous element in insertion order.
bool operator!=(const Iterator &other) const
Inequality comparison operator.
Iterator operator--(int)
Post-decrement operator to move to previous element in insertion order.
bool operator==(const Iterator &other) const
Equality comparison operator.
std::ptrdiff_t difference_type
STL iterator difference type.
reference operator*() const
Dereference operator to access key.
const TKey * pointer
STL iterator pointer type.
Iterator operator++(int)
Post-increment operator to advance to next element in insertion order.
Iterator()=default
Default constructor creates an invalid iterator.
Iterator(Node *node)
Construct iterator from node.
pointer operator->() const
Arrow operator to access key.
const TKey value_type
STL iterator value type (const key).
Iterator & operator++()
Pre-increment operator to advance to next element in insertion order.
Const iterator for OrderedHashSet that follows insertion order.
ConstIterator()=default
Default constructor creates an invalid iterator.
ConstIterator & operator--()
Pre-decrement operator to move to previous element in insertion order.
bool operator==(const ConstIterator &other) const
Equality comparison operator.
std::ptrdiff_t difference_type
STL iterator difference type.
bool operator!=(const ConstIterator &other) const
Inequality comparison operator.
ConstIterator operator++(int)
Post-increment operator to advance to next element in insertion order.
ConstIterator & operator++()
Pre-increment operator to advance to next element in insertion order.
ConstIterator operator--(int)
Post-decrement operator to move to previous element in insertion order.
const TKey value_type
STL iterator value type (const key).
ConstIterator(const Iterator &it)
Convert from non-const iterator.
const TKey & reference
STL iterator reference type.
pointer operator->() const
Arrow operator to access key.
reference operator*() const
Dereference operator to access key.
std::bidirectional_iterator_tag iterator_category
STL iterator category (bidirectional iterator).
ConstIterator(const Node *node)
Construct const iterator from node.
const TKey * pointer
STL iterator pointer type.