34#include "nfx/detail/containers/CompilerSupport.h"
36#include <nfx/Hashing.h>
43#include <initializer_list>
52namespace nfx::containers
66 template <
typename TKey,
67 hashing::Hash32or64 HashType = uint32_t,
68 HashType Seed = (
sizeof( HashType ) == 4 ? hashing::constants::FNV_OFFSET_BASIS_32 : hashing::constants::FNV_OFFSET_BASIS_64 ),
69 typename THasher = hashing::Hasher<HashType, Seed>,
70 typename KeyEqual = std::equal_to<>>
77 static_assert( std::is_same_v<HashType, uint32_t> || std::is_same_v<HashType, uint64_t>,
78 "HashType must be uint32_t or uint64_t" );
80 static_assert( std::is_invocable_r_v<HashType, THasher, TKey>,
81 "THasher must be callable with TKey and return HashType" );
83 static_assert( std::is_invocable_r_v<bool, KeyEqual, TKey, TKey>,
84 "KeyEqual must be callable with two TKey arguments and return bool" );
148 template <
typename InputIt>
199 template <
typename KeyType = TKey>
200 [[nodiscard]]
inline const TKey*
find(
const KeyType& key )
const noexcept;
209 template <
typename KeyType = TKey>
210 [[nodiscard]]
inline bool contains(
const KeyType& key )
const noexcept;
220 template <
typename KeyType = TKey>
221 inline const TKey&
at(
const KeyType& key )
const;
252 template <
typename... Args>
262 template <
typename... Args>
263 inline std::pair<Iterator, bool>
tryEmplace( Args&&... args );
283 template <
typename KeyType = TKey>
284 inline bool erase(
const KeyType& key )
noexcept;
316 template <typename KeyType = TKey>
317 [[nodiscard]] inline std::optional<TKey>
extract( const KeyType& key );
343 [[nodiscard]] inline
size_t size() const noexcept;
350 [[nodiscard]] inline
size_t capacity() const noexcept;
357 [[nodiscard]] inline
bool isEmpty() const noexcept;
418 [[nodiscard]]
bool operator==( const
FastHashSet& other ) const noexcept;
430 std::optional<TKey> data;
441 static constexpr size_t INITIAL_CAPACITY = 32;
448 static constexpr size_t MAX_LOAD_FACTOR_PERCENT = 75;
455 std::vector<Bucket> m_buckets;
458 size_t m_capacity{ INITIAL_CAPACITY };
459 size_t m_mask{ INITIAL_CAPACITY - 1 };
468 NFX_CONTAINERS_NO_UNIQUE_ADDRESS hasher m_hasher;
475 NFX_CONTAINERS_NO_UNIQUE_ADDRESS KeyEqual m_keyEqual;
486 inline bool insertInternal(
const TKey& key );
493 inline bool insertInternal( TKey&& key );
499 inline bool shouldResize() const noexcept;
504 inline
void resize();
510 inline
void eraseAtPosition(
size_t pos ) noexcept;
520 template <typename KeyType1, typename KeyType2>
521 inline
bool keysEqual( const KeyType1& k1, const KeyType2& k2 ) const noexcept;
535 friend class ConstIterator;
624 inline void skipToOccupied();
630 Bucket* m_bucket =
nullptr;
631 Bucket* m_end =
nullptr;
645 friend class FastHashSet;
740 inline void skipToOccupied();
746 const Bucket* m_bucket =
nullptr;
747 const Bucket* m_end =
nullptr;
752#include "nfx/detail/containers/FastHashSet.inl"
void merge(FastHashSet &other)
Merge elements from another set into this set.
FastHashSet(InputIt first, InputIt last)
Construct set from iterator range.
KeyEqual key_equal
Type alias for key equality comparator.
~FastHashSet()=default
Destructor.
HashType hash_type
Type alias for hash type (uint32_t or uint64_t).
THasher hasher
Type alias for hasher type.
ConstIterator cbegin() const noexcept
Get const iterator to beginning of occupied buckets (explicit const).
Iterator erase(ConstIterator first, ConstIterator last) noexcept
Erase range of elements.
void clear() noexcept
Clear all elements from the set.
Iterator erase(ConstIterator pos) noexcept
Erase element at iterator position.
FastHashSet()
Default constructor with initial capacity of 32 elements.
const TKey & at(const KeyType &key) const
Checked element access with bounds checking.
bool insert(const TKey &key)
Insert a key into the set (copy semantics).
std::pair< Iterator, bool > tryEmplace(Args &&... args)
Try to emplace a key if it doesn't exist.
std::optional< TKey > extract(const KeyType &key)
Extract and remove a key from the set.
Iterator begin() noexcept
Get iterator to beginning of occupied buckets.
TKey value_type
Type alias for value type (same as key for sets).
bool isEmpty() const noexcept
Check if the set is empty.
size_t size() const noexcept
Get the number of elements in the set.
ConstIterator cend() const noexcept
Get const iterator to end (explicit const).
void swap(FastHashSet &other) noexcept
Swap contents with another set.
size_t size_type
Type alias for size type.
ConstIterator const_iterator
Type alias for const iterator.
const TKey * find(const KeyType &key) const noexcept
Fast lookup with heterogeneous key types (C++ idiom: pointer return).
bool contains(const KeyType &key) const noexcept
Check if a key exists in the set.
FastHashSet(size_t initialCapacity)
Constructor with specified initial capacity.
bool insert(TKey &&key)
Insert a key into the set (move semantics).
size_t capacity() const noexcept
Get the current capacity of the hash table.
std::ptrdiff_t difference_type
Type alias for difference type.
bool emplace(Args &&... args)
Construct and insert a key in-place.
TKey key_type
Type alias for key type.
FastHashSet & operator=(FastHashSet &&other) noexcept
Move assignment operator.
FastHashSet(FastHashSet &&other) noexcept
Move constructor.
Iterator iterator
Type alias for iterator.
FastHashSet(const FastHashSet &)=default
Copy constructor.
void reserve(size_t minCapacity)
Reserve capacity for at least the specified number of elements.
FastHashSet & operator=(const FastHashSet &)=default
Copy assignment operator.
bool erase(const KeyType &key) noexcept
Remove a key from the set.
Iterator end() noexcept
Get iterator to end (past last occupied bucket).
FastHashSet(std::initializer_list< TKey > init)
Construct set from initializer_list.
Forward iterator for FastHashSet that skips empty buckets.
bool operator==(const Iterator &other) const
Equality comparison operator.
reference operator*() const
Dereference operator to access key.
const TKey value_type
STL iterator value type (key).
const TKey & reference
STL iterator reference type.
bool operator!=(const Iterator &other) const
Inequality comparison operator.
Iterator()=default
Default constructor creates an invalid iterator.
std::forward_iterator_tag iterator_category
STL iterator category (forward iterator).
Iterator(Bucket *bucket, Bucket *end)
Construct iterator from bucket range.
Iterator operator++(int)
Post-increment operator to advance to next occupied bucket.
pointer operator->() const
Arrow operator to access key.
Iterator & operator++()
Pre-increment operator to advance to next occupied bucket.
std::ptrdiff_t difference_type
STL iterator difference type.
const TKey * pointer
STL iterator pointer type.
Const forward iterator for FastHashSet that skips empty buckets.
ConstIterator operator++(int)
Post-increment operator to advance to next occupied bucket.
bool operator!=(const ConstIterator &other) const
Inequality comparison operator.
pointer operator->() const
Arrow operator to access key.
const TKey * pointer
STL iterator pointer type.
std::forward_iterator_tag iterator_category
STL iterator category (forward iterator).
const TKey value_type
STL iterator value type (const key).
bool operator==(const ConstIterator &other) const
Equality comparison operator.
std::ptrdiff_t difference_type
STL iterator difference type.
ConstIterator & operator++()
Pre-increment operator to advance to next occupied bucket.
ConstIterator(const Iterator &it)
Convert from non-const iterator.
reference operator*() const
Dereference operator to access key.
const TKey & reference
STL iterator reference type.
ConstIterator(const Bucket *bucket, const Bucket *end)
Construct const iterator from bucket range.
ConstIterator()=default
Default constructor creates an invalid iterator.