nfx-containers 0.6.0
Modern C++20 header-only library providing high-performance hash containers with Robin Hood and perfect hashing
Loading...
Searching...
No Matches
nfx::containers::StackHashSet< TKey, N, KeyEqual > Class Template Referencefinal

Hash set with small buffer optimization for stack-allocated storage. More...

#include <nfx/containers/StackHashSet.h>

Public Types

using key_type = TKey
 Type alias for key type.
using value_type = TKey
 Type alias for value type (same as key_type for sets).
using size_type = size_t
 Type alias for size type.
using difference_type = std::ptrdiff_t
 Type alias for difference type.
using key_equal = KeyEqual
 Type alias for key equality comparator.

Public Member Functions

 StackHashSet ()
 Default constructor with empty stack storage.
 StackHashSet (std::initializer_list< TKey > init)
 Construct set from initializer_list.
template<typename InputIt>
 StackHashSet (InputIt first, InputIt last)
 Construct set from iterator range.
bool isEmpty () const noexcept
 Check if the set is empty.
size_t size () const noexcept
 Get the number of elements in the set.
std::pair< const value_type *, bool > insert (const TKey &key)
 Insert a key (copy semantics).
std::pair< const value_type *, bool > insert (TKey &&key)
 Insert a key (move semantics).
template<typename... Args>
std::pair< const value_type *, bool > emplace (Args &&... args)
 Emplace a key with in-place construction.
size_t erase (const TKey &key)
 Erase element by key.
void clear () noexcept
 Clear all elements from the set.
std::optional< TKey > extract (const TKey &key)
 Extract a key from the set without destroying it.
void merge (StackHashSet &other)
 Merge another StackHashSet into this one.
void merge (StackHashSet &&other)
 Merge another StackHashSet into this one (rvalue overload).
const TKey * find (const TKey &key) const noexcept
 Find key in the set (const pointer version).
bool contains (const TKey &key) const
 Check if a key exists in the set.
template<typename K>
requires requires( KeyEqual eq, const K& k, const TKey& t ) { eq( k, t ); }
bool contains (const K &key) const
 Check if a key exists in the set (heterogeneous lookup).
const TKey & at (const TKey &key) const
 Access key in the set with bounds checking.
template<typename Func>
void forEach (Func &&func)
 Apply a function to each key.
template<typename Func>
void forEach (Func &&func) const
 Apply a function to each key (const version).

Static Public Member Functions

static constexpr size_t stackCapacity () noexcept
 Get the maximum stack capacity before heap allocation.

Detailed Description

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
class nfx::containers::StackHashSet< TKey, N, KeyEqual >

Hash set with small buffer optimization for stack-allocated storage.

Template Parameters
TKeyKey type (supports heterogeneous lookup for compatible types)
NMaximum stack capacity before heap allocation (default: 8)
KeyEqualKey equality comparator (default: std::equal_to<> for transparent comparison)

StackHashSet uses a hybrid storage strategy:

  • Small capacity (≤N): Linear search in stack-allocated array (cache-friendly, zero heap allocations)
  • Large capacity (>N): Automatic transition to FastHashSet (Robin Hood hashing on heap)

This is ideal for small sets (config flags, tags, local caches) where heap allocation overhead would dominate performance. The transition to heap is transparent and automatic.

Definition at line 66 of file StackHashSet.h.

Member Typedef Documentation

◆ difference_type

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
using nfx::containers::StackHashSet< TKey, N, KeyEqual >::difference_type = std::ptrdiff_t

Type alias for difference type.

Definition at line 83 of file StackHashSet.h.

◆ key_equal

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
using nfx::containers::StackHashSet< TKey, N, KeyEqual >::key_equal = KeyEqual

Type alias for key equality comparator.

Definition at line 86 of file StackHashSet.h.

◆ key_type

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
using nfx::containers::StackHashSet< TKey, N, KeyEqual >::key_type = TKey

Type alias for key type.

Definition at line 74 of file StackHashSet.h.

◆ size_type

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
using nfx::containers::StackHashSet< TKey, N, KeyEqual >::size_type = size_t

Type alias for size type.

Definition at line 80 of file StackHashSet.h.

◆ value_type

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
using nfx::containers::StackHashSet< TKey, N, KeyEqual >::value_type = TKey

Type alias for value type (same as key_type for sets).

Definition at line 77 of file StackHashSet.h.

Constructor & Destructor Documentation

◆ StackHashSet() [1/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
nfx::containers::StackHashSet< TKey, N, KeyEqual >::StackHashSet ( std::initializer_list< TKey > init)
inline

Construct set from initializer_list.

Parameters
initInitializer list of keys

◆ StackHashSet() [2/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
template<typename InputIt>
nfx::containers::StackHashSet< TKey, N, KeyEqual >::StackHashSet ( InputIt first,
InputIt last )
inline

Construct set from iterator range.

Template Parameters
InputItInput iterator type (must dereference to TKey)
Parameters
firstBeginning of range to copy from
lastEnd of range (exclusive)

Member Function Documentation

◆ at()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
const TKey & nfx::containers::StackHashSet< TKey, N, KeyEqual >::at ( const TKey & key) const
inline

Access key in the set with bounds checking.

Parameters
keyThe key to search for
Returns
Const reference to the stored key
Exceptions
std::out_of_rangeif key is not found
Note
For sets, at() returns the key itself (useful for retrieving stored key)

◆ clear()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
void nfx::containers::StackHashSet< TKey, N, KeyEqual >::clear ( )
inlinenoexcept

Clear all elements from the set.

Clears stack storage or heap storage, does not deallocate heap set

◆ contains() [1/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
template<typename K>
requires requires( KeyEqual eq, const K& k, const TKey& t ) { eq( k, t ); }
bool nfx::containers::StackHashSet< TKey, N, KeyEqual >::contains ( const K & key) const
inlinenodiscard

Check if a key exists in the set (heterogeneous lookup).

Template Parameters
KKey type (supports heterogeneous lookup for compatible types)
Parameters
keyThe key to search for
Returns
true if key exists, false otherwise
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Enabled only if KeyEqual supports comparing K with TKey

◆ contains() [2/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
bool nfx::containers::StackHashSet< TKey, N, KeyEqual >::contains ( const TKey & key) const
inlinenodiscard

Check if a key exists in the set.

Parameters
keyThe key to search for
Returns
true if key exists, false otherwise
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ emplace()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
template<typename... Args>
std::pair< const value_type *, bool > nfx::containers::StackHashSet< TKey, N, KeyEqual >::emplace ( Args &&... args)
inline

Emplace a key with in-place construction.

Template Parameters
ArgsArgument types for constructing the key
Parameters
argsArguments forwarded to construct the key
Returns
Pair of pointer to element and bool indicating insertion success

◆ erase()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
size_t nfx::containers::StackHashSet< TKey, N, KeyEqual >::erase ( const TKey & key)
inline

Erase element by key.

Parameters
keyThe key to erase
Returns
Number of elements erased (0 or 1)

◆ extract()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
std::optional< TKey > nfx::containers::StackHashSet< TKey, N, KeyEqual >::extract ( const TKey & key)
inlinenodiscard

Extract a key from the set without destroying it.

Parameters
keyThe key to extract
Returns
std::optional containing the extracted key if found, std::nullopt otherwise

Removes the element from the set and returns the key. If the key is not found, returns std::nullopt.

Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ find()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
const TKey * nfx::containers::StackHashSet< TKey, N, KeyEqual >::find ( const TKey & key) const
inlinenodiscardnoexcept

Find key in the set (const pointer version).

Parameters
keyThe key to search for
Returns
Pointer to the key if found, nullptr otherwise
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ forEach() [1/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
template<typename Func>
void nfx::containers::StackHashSet< TKey, N, KeyEqual >::forEach ( Func && func)
inline

Apply a function to each key.

Template Parameters
FuncFunction type with signature void(TKey&) or compatible
Parameters
funcFunction to apply to each element

Iterates over all elements in unspecified order. For stack storage, iteration order matches insertion order (up to N elements). For heap storage, iteration order is unspecified (hash table order).

◆ forEach() [2/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
template<typename Func>
void nfx::containers::StackHashSet< TKey, N, KeyEqual >::forEach ( Func && func) const
inline

Apply a function to each key (const version).

Template Parameters
FuncFunction type with signature void(const TKey&) or compatible
Parameters
funcFunction to apply to each element

Iterates over all elements in unspecified order. For stack storage, iteration order matches insertion order (up to N elements). For heap storage, iteration order is unspecified (hash table order).

◆ insert() [1/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
std::pair< const value_type *, bool > nfx::containers::StackHashSet< TKey, N, KeyEqual >::insert ( const TKey & key)
inline

Insert a key (copy semantics).

Parameters
keyThe key to insert
Returns
Pair of pointer to element and bool indicating insertion success

If key already exists, returns pointer to existing element and false. Otherwise inserts new element and returns pointer with true. May trigger transition to heap storage.

◆ insert() [2/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
std::pair< const value_type *, bool > nfx::containers::StackHashSet< TKey, N, KeyEqual >::insert ( TKey && key)
inline

Insert a key (move semantics).

Parameters
keyThe key to insert (moved)
Returns
Pair of pointer to element and bool indicating insertion success

If key already exists, returns pointer to existing element and false. Otherwise inserts new element and returns pointer with true. May trigger transition to heap storage.

◆ isEmpty()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
bool nfx::containers::StackHashSet< TKey, N, KeyEqual >::isEmpty ( ) const
inlinenodiscardnoexcept

Check if the set is empty.

Returns
true if size() == 0, false otherwise
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ merge() [1/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
void nfx::containers::StackHashSet< TKey, N, KeyEqual >::merge ( StackHashSet< TKey, N, KeyEqual > && other)
inline

Merge another StackHashSet into this one (rvalue overload).

Parameters
otherThe set to merge from (elements are moved)

Same as merge(StackHashSet&) but accepts rvalue references.

◆ merge() [2/2]

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
void nfx::containers::StackHashSet< TKey, N, KeyEqual >::merge ( StackHashSet< TKey, N, KeyEqual > & other)
inline

Merge another StackHashSet into this one.

Parameters
otherThe set to merge from (elements are moved, not copied)

Attempts to insert each element from other into this set. Elements that already exist in this set are left in other. After the operation, other contains only elements that were not inserted.

◆ size()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
size_t nfx::containers::StackHashSet< TKey, N, KeyEqual >::size ( ) const
inlinenodiscardnoexcept

Get the number of elements in the set.

Returns
Current number of keys stored
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ stackCapacity()

template<typename TKey, size_t N = 8, typename KeyEqual = std::equal_to<>>
constexpr size_t nfx::containers::StackHashSet< TKey, N, KeyEqual >::stackCapacity ( )
inlinestaticnodiscardconstexprnoexcept

Get the maximum stack capacity before heap allocation.

Returns
Stack capacity (template parameter N)
Note
This function is marked [[nodiscard]] - the return value should not be ignored

Definition at line 135 of file StackHashSet.h.


The documentation for this class was generated from the following file: