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
StackHashSet.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
31
32#pragma once
33
35
36#include <array>
37#include <cstddef>
38#include <functional>
39#include <initializer_list>
40#include <memory>
41#include <optional>
42#include <utility>
43
44namespace nfx::containers
45{
46 //=====================================================================
47 // StackHashSet class
48 //=====================================================================
49
63 template <typename TKey,
64 size_t N = 8,
65 typename KeyEqual = std::equal_to<>>
66 class StackHashSet final
67 {
68 public:
69 //----------------------------------------------
70 // STL-compatible type aliases
71 //----------------------------------------------
72
74 using key_type = TKey;
75
77 using value_type = TKey;
78
80 using size_type = size_t;
81
83 using difference_type = std::ptrdiff_t;
84
86 using key_equal = KeyEqual;
87
88 //----------------------------------------------
89 // Construction
90 //----------------------------------------------
91
95 inline StackHashSet();
96
101 inline StackHashSet( std::initializer_list<TKey> init );
102
109 template <typename InputIt>
110 inline StackHashSet( InputIt first, InputIt last );
111
112 //----------------------------------------------
113 // Capacity
114 //----------------------------------------------
115
121 [[nodiscard]] inline bool isEmpty() const noexcept;
122
128 [[nodiscard]] inline size_t size() const noexcept;
129
135 [[nodiscard]] static constexpr size_t stackCapacity() noexcept { return N; }
136
137 //----------------------------------------------
138 // Modifiers
139 //----------------------------------------------
140
149 inline std::pair<const value_type*, bool> insert( const TKey& key );
150
159 inline std::pair<const value_type*, bool> insert( TKey&& key );
160
167 template <typename... Args>
168 inline std::pair<const value_type*, bool> emplace( Args&&... args );
169
175 inline size_t erase( const TKey& key );
176
181 inline void clear() noexcept;
182
191 [[nodiscard]] inline std::optional<TKey> extract( const TKey& key );
192
200 inline void merge( StackHashSet& other );
201
207 inline void merge( StackHashSet&& other );
208
209 //----------------------------------------------
210 // Lookup
211 //----------------------------------------------
212
219 [[nodiscard]] inline const TKey* find( const TKey& key ) const noexcept;
220
227 [[nodiscard]] inline bool contains( const TKey& key ) const;
228
237 template <typename K>
238 [[nodiscard]] inline bool contains( const K& key ) const
239 requires requires( KeyEqual eq, const K& k, const TKey& t ) { eq( k, t ); };
240
248 inline const TKey& at( const TKey& key ) const;
249
250 //----------------------------------------------
251 // Iteration
252 //----------------------------------------------
253
262 template <typename Func>
263 inline void forEach( Func&& func );
264
273 template <typename Func>
274 inline void forEach( Func&& func ) const;
275
276 private:
277 //----------------------------------------------
278 // Internal storage
279 //----------------------------------------------
280
284 struct StackEntry
285 {
286 std::optional<TKey> data;
287 };
288
292 std::array<StackEntry, N> m_stack;
293
297 size_t m_stackSize;
298
302 std::unique_ptr<FastHashSet<TKey>> m_heap;
303
308 [[nodiscard]] inline bool isOnStack() const noexcept;
309
314 inline void transitionToHeap();
315 };
316} // namespace nfx::containers
317
318#include "nfx/detail/containers/StackHashSet.inl"
Hash set with Robin Hood hashing and heterogeneous lookup.
size_t size() const noexcept
Get the number of elements in the set.
size_t erase(const TKey &key)
Erase element by key.
std::pair< const value_type *, bool > emplace(Args &&... args)
Emplace a key with in-place construction.
void forEach(Func &&func) const
Apply a function to each key (const version).
bool contains(const TKey &key) const
Check if a key exists in the set.
const TKey & at(const TKey &key) const
Access key in the set with bounds checking.
TKey value_type
Type alias for value type (same as key_type for sets).
KeyEqual key_equal
Type alias for key equality comparator.
std::ptrdiff_t difference_type
Type alias for difference type.
size_t size_type
Type alias for size type.
TKey key_type
Type alias for key type.
StackHashSet()
Default constructor with empty stack storage.
void forEach(Func &&func)
Apply a function to each key.
bool isEmpty() const noexcept
Check if the set is empty.
void merge(StackHashSet &other)
Merge another StackHashSet into this one.
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).
static constexpr size_t stackCapacity() noexcept
Get the maximum stack capacity before heap allocation.
std::optional< TKey > extract(const TKey &key)
Extract a key from the set without destroying it.
StackHashSet(InputIt first, InputIt last)
Construct set from iterator range.
StackHashSet(std::initializer_list< TKey > init)
Construct set from initializer_list.
const TKey * find(const TKey &key) const noexcept
Find key in the set (const pointer version).
void clear() noexcept
Clear all elements from the set.