libhatchet
Loading...
Searching...
No Matches
hxflat_set.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxallocator.hpp"
17#include "hxrange.hpp"
19#include "hxkey.hpp"
20
21HX_NS_BEGIN_
22
23#if HX_CPLUSPLUS >= 202002L
25template<typename T_>
26concept hxflat_set_concept_ = requires(T_& x_) {
27 sizeof(T_);
28 x_.~T_();
29};
31#else
32#define hxflat_set_concept_ typename
33#endif
34
60template<hxflat_set_concept_ key_t_,
61 typename compare_t_=hxkey_less_t<key_t_>,
62 bool multi_t_=true,
64class hxflat_set : private hxallocator<key_t_, capacity_> {
65public:
66 using key_t = key_t_;
67 using compare_t = compare_t_;
68
70 using iterator = const key_t_*;
71
73 using const_iterator = const key_t_*;
74
77 explicit hxflat_set(void);
78
83 hxflat_set(const hxflat_set& x_) noexcept;
84
89 hxflat_set(hxflat_set&& x_) noexcept;
90
94 hxflat_set(std::initializer_list<key_t_> x_) noexcept;
95
97 ~hxflat_set(void) noexcept;
98
99#if HX_CPLUSPLUS >= 202302L
105 template<typename self_t_, typename callable_t_>
107 this self_t_&& self_, const key_t_& key_, callable_t_&& callable_)
108 -> hxremove_cvref_t<decltype(
109 hxforward<callable_t_>(callable_)(
111
115 template<typename self_t_, typename callable_t_>
117 this self_t_&& self_, const key_t_* it_, callable_t_&& callable_)
118 -> hxremove_cvref_t<decltype(
119 hxforward<callable_t_>(callable_)(
121#endif // HX_CPLUSPLUS >= 202302L
122
126 void operator=(const hxflat_set& x_) noexcept;
127
131 template<hxsize_t capacity_x_>
133
137 void operator=(hxflat_set&& x_) noexcept;
138
142 hxattr_nodiscard const key_t_* operator[](hxsize_t index_) const;
143
147 template<hxsize_t capacity_x_>
149
153 template<hxsize_t capacity_x_>
155
159 template<hxrange_concept_ range_t_>
160 void add_range(range_t_& range_) noexcept;
161
166 template<hxrange_concept_ range_t_,
167 hxenable_if_t<!hxis_lvalue_reference<range_t_>(), int> = 0>
168 void add_range(range_t_&& range_) noexcept;
169
171 const key_t_* begin(void) const { return this->data(); }
172
175
177 const key_t_* cbegin(void) const { return this->data(); }
178
180 const key_t_* cend(void) const { return m_end_; }
181
183 void clear(void) noexcept;
184
187 hxattr_nodiscard hxsize_t count(const key_t_& key_) const;
188
190 using hxallocator<key_t_, capacity_>::data;
191
195 template<typename... args_t_>
196 const key_t_* emplace(args_t_&&... args_) noexcept;
197
199 hxattr_nodiscard bool empty(void) const { return m_end_ == this->data(); }
200
202 const key_t_* end(void) const { return m_end_; }
203
207 hxsize_t erase(const key_t_& key_) noexcept;
208
212 const key_t_* erase(const key_t_* it_) noexcept;
213
217 hxattr_nodiscard const key_t_* find(const key_t_& key_) const;
218
220 hxattr_nodiscard bool full(void) const { return m_end_ == this->data() + this->capacity(); }
221
224 hxattr_nodiscard bool has_value(const key_t_& key_) const;
225
231 const key_t_* insert(const key_t_& key_) noexcept;
232
233 const key_t_* insert(key_t_&& key_) noexcept;
234
237 hxattr_nodiscard const key_t_* keys(void) const noexcept;
238
242 hxattr_nodiscard const key_t_* lower_bound(const key_t_& key_) const;
243
245 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
246
247#if HX_CPLUSPLUS >= 202302L
253 template<typename self_t_, typename callable_t_>
255 this self_t_&& self_, const key_t_& key_, callable_t_&& callable_)
256 -> decltype(self_.end());
257
262 template<typename self_t_, typename callable_t_>
264 this self_t_&& self_, const key_t_* it_, callable_t_&& callable_)
265 -> decltype(self_.end());
266#endif // HX_CPLUSPLUS >= 202302L
267
275 void reserve(hxsize_t cap_,
278
280 hxattr_nodiscard hxsize_t size(void) const { return m_end_ - this->data(); }
281
284 void swap(hxflat_set& x_) noexcept;
285
289 hxattr_nodiscard const key_t_* upper_bound(const key_t_& key_) const;
290
291#if HX_CPLUSPLUS >= 202302L
296 template<typename self_t_, typename... args_t_>
298 this self_t_&& self_, const key_t_& key_, args_t_&&... args_);
299
303 template<typename self_t_, typename... args_t_>
305 this self_t_&& self_, const key_t_* it_, args_t_&&... args_);
306#endif // HX_CPLUSPLUS >= 202302L
307
308private:
310 template<hxflat_set_concept_, typename, bool, hxsize_t> friend class hxflat_set;
311
312 template<typename key_u_>
313 const key_t_* insert_at_(key_t_* it_, key_u_&& key_) noexcept;
314
315 key_t_* m_end_;
317};
318
319#include "detail/hxflat_set.inl"
320HX_NS_END_
hxallocator(void)
Definition hxallocator.hpp:35
const key_t_ * data(void) const
Definition hxallocator.hpp:45
bool operator==(const hxflat_set< key_t_, compare_t_, multi_t_, capacity_x_ > &x_) const
Returns true if this set and x contain the same keys in the same order using hxkey_equal.
~hxflat_set(void) noexcept
Destructs the set and destroys all keys.
hxflat_set(void)
Constructs an empty set.
const key_t_ * insert(key_t_ &&key_) noexcept
hxflat_set(std::initializer_list< key_t_ > x_) noexcept
Constructs a set by inserting every key from x in order using insert.
hxsize_t size(void) const
Returns the number of elements in the set.
Definition hxflat_set.hpp:280
hxsize_t capacity(void) const
Returns the capacity of the set or 0 if unallocated.
bool operator<(const hxflat_set< key_t_, compare_t_, multi_t_, capacity_x_ > &x_) const
Returns true if this set compares less than x lexicographically, using hxkey_equal and hxkey_less on ...
const key_t_ * keys(void) const noexcept
Returns a const pointer to the underlying key storage.
void swap(hxflat_set &x_) noexcept
Swaps the contents with x.
const key_t_ * upper_bound(const key_t_ &key_) const
Returns a const pointer to the first element whose key is ordered after key.
key_t_ value_or(this self_t_ &&self_, const key_t_ &key_, args_t_ &&... args_)
Returns a copy of the first key matching key, or a key constructed from args when lookup fails.
void operator=(hxflat_set &&x_) noexcept
Move assigns from a temporary set using swap.
bool has_value(const key_t_ &key_) const
Returns true if a key matching key exists.
const key_t_ * lower_bound(const key_t_ &key_) const
Returns a const pointer to the first element whose key is not ordered before key.
const key_t_ * const_iterator
const_iterator - Const random access iterator over keys.
Definition hxflat_set.hpp:73
hxflat_set(const hxflat_set &x_) noexcept
Copy constructs from another hxflat_set.
const key_t_ * erase(const key_t_ *it_) noexcept
Removes the element at the pointer position and destroys the key.
compare_t_ compare_t
Definition hxflat_set.hpp:67
void add_range(range_t_ &&range_) noexcept
Inserts every key from a temporary range by moving each key with insert.
const key_t_ * iterator
iterator - Const random access iterator over keys.
Definition hxflat_set.hpp:70
const key_t_ * insert(const key_t_ &key_) noexcept
Inserts a key.
bool full(void) const
Returns true if the set has reached its capacity.
Definition hxflat_set.hpp:220
const key_t_ * operator[](hxsize_t index_) const
Returns a const pointer to the element at index.
key_t_ value_or(this self_t_ &&self_, const key_t_ *it_, args_t_ &&... args_)
Iterator overload of value_or.
hxflat_set(hxflat_set &&x_) noexcept
Move constructs from a temporary hxflat_set.
hxsize_t max_size(void) const
Returns the capacity of the set.
Definition hxflat_set.hpp:245
bool empty(void) const
Checks if the set contains no elements.
Definition hxflat_set.hpp:199
const key_t_ * end(void) const
Returns a const pointer past the last element.
Definition hxflat_set.hpp:202
const key_t_ * find(const key_t_ &key_) const
Returns a const pointer to the key if found, or end() if not present.
const key_t_ * emplace(args_t_ &&... args_) noexcept
Constructs and inserts a key from args, returning a pointer to the new or existing key.
void clear(void) noexcept
Removes all elements and destroys their keys.
const key_t_ * begin(void) const
Returns a const pointer to the first element.
Definition hxflat_set.hpp:171
void add_range(range_t_ &range_) noexcept
Inserts every key from a range referenced by an lvalue by copying each key with insert.
void operator=(const hxflat_set< key_t_, compare_t_, multi_t_, capacity_x_ > &x_) noexcept
Cross-capacity copy assignment.
key_t_ key_t
Definition hxflat_set.hpp:66
void reserve(hxsize_t cap_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment)
Allocates storage for cap keys.
void operator=(const hxflat_set &x_) noexcept
Assigns the contents of x to this set.
auto or_else(this self_t_ &&self_, const key_t_ &key_, callable_t_ &&callable_) -> decltype(self_.end())
Returns a pointer to the first key matching key, or the result of calling callable when lookup fails.
hxsize_t count(const key_t_ &key_) const
Returns the number of elements with the given key.
auto and_then(this self_t_ &&self_, const key_t_ &key_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxforward_like< self_t_ >(hxdeclval< const key_t_ & >())))>
Returns the result of calling callable with the first key matching key, otherwise returns hxnil.
auto or_else(this self_t_ &&self_, const key_t_ *it_, callable_t_ &&callable_) -> decltype(self_.end())
Iterator overload of or_else.
hxsize_t erase(const key_t_ &key_) noexcept
Removes all elements with the given key and destroys their keys.
auto and_then(this self_t_ &&self_, const key_t_ *it_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxforward_like< self_t_ >(hxdeclval< const key_t_ & >())))>
Iterator overload of and_then.
const key_t_ * cend(void) const
Returns a const pointer past the last element.
Definition hxflat_set.hpp:180
const key_t_ * cbegin(void) const
Returns a const pointer to the first element (alias for begin).
Definition hxflat_set.hpp:177
hxkey_less_t<T> - A constexpr callable that returns true if the first object is less than the second.
Definition hxkey.hpp:91
Similar to std::allocator.
HX_NS_BEGIN_ constexpr hxsize_t hxallocator_dynamic_capacity
hxallocator_dynamic_capacity - A capacity value that allows for dynamic allocation.
Definition hxallocator.hpp:20
Implements or includes std::initializer_list depending on HX_USE_LIBCXX.
User-specializable key-equal, key-less, and key-hash callables.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:80
HX_C_BEGIN_ typedef unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:62
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:75
hxattr_noexcept hxsystem_allocator_t allocator_
Definition hxmemory_manager.h:120
hxattr_noexcept hxsystem_allocator_t hxalignment_t alignment_
Definition hxmemory_manager.h:121
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:67
Read-only range utilities for libhatchet.
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
constexpr T && hxforward(hxremove_reference_t< T > &&x_)
hxforward - Implements std::forward.
Definition hxutility.h:353
hxremove_cv_t< hxremove_reference_t< T > > hxremove_cvref_t
hxremove_cvref_t<T> - Returns T with const, volatile, and references removed.
Definition hxutility.h:103
T && hxdeclval(void) noexcept
hxdeclval - Implements std::declval.
constexpr hxforward_like_t_< like_t_, value_t_ > hxforward_like(value_t_ &value_)
hxforward_like<T> - Returns value with the constness and value category of T.
Definition hxutility.h:387
Provides core macros, memory management and feature detection.
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:207