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 "hxkey.hpp"
18#include "hxsort.hpp"
19
20HX_NS_BEGIN_
21
47template<typename key_t_,
48 typename compare_t_=hxkey_less_t<key_t_>,
49 bool multi_t_=true,
51class hxflat_set : private hxallocator<key_t_, capacity_> {
52public:
53 using key_t = key_t_;
54 using compare_t = compare_t_;
55
57 using iterator = const key_t_*;
58
60 using const_iterator = const key_t_*;
61
64 explicit hxflat_set(void);
65
69 hxflat_set(const hxflat_set& x_) noexcept;
70
74 hxflat_set(hxflat_set&& x_) noexcept;
75
77 ~hxflat_set(void) noexcept;
78
82 void operator=(const hxflat_set& x_) noexcept;
83
87 template<hxsize_t capacity_x_>
89
93 void operator=(hxflat_set&& x_) noexcept;
94
98 hxattr_nodiscard const key_t_* operator[](hxsize_t index_) const;
99
101 const key_t_* begin(void) const { return this->data(); }
102
105
107 const key_t_* cbegin(void) const { return this->data(); }
108
110 const key_t_* cend(void) const { return m_end_; }
111
113 void clear(void) noexcept;
114
117 hxattr_nodiscard hxsize_t count(const key_t_& key_) const;
118
120 using hxallocator<key_t_, capacity_>::data;
121
123 hxattr_nodiscard bool empty(void) const { return m_end_ == this->data(); }
124
126 const key_t_* end(void) const { return m_end_; }
127
131 template<hxsize_t capacity_x_>
133
137 hxsize_t erase(const key_t_& key_) noexcept;
138
142 const key_t_* erase(const key_t_* pos_) noexcept;
143
147 hxattr_nodiscard const key_t_* find(const key_t_& key_) const;
148
150 hxattr_nodiscard bool full(void) const { return m_end_ == this->data() + this->capacity(); }
151
157 const key_t_* insert(const key_t_& key_) noexcept;
158
161 const key_t_* insert(key_t_&& key_) noexcept;
162
166 template<hxsize_t capacity_x_>
168
172 hxattr_nodiscard const key_t_* lower_bound(const key_t_& key_) const;
173
175 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
176
182 void reserve(hxsize_t cap_,
184 hxalignment_t alignment_=hxalignment);
185
187 hxattr_nodiscard hxsize_t size(void) const { return m_end_ - this->data(); }
188
191 void swap(hxflat_set& x_) noexcept;
192
196 hxattr_nodiscard const key_t_* upper_bound(const key_t_& key_) const;
197
198private:
200 template<typename, typename, bool, hxsize_t> friend class hxflat_set;
201
202 template<typename key_u_>
203 const key_t_* insert_at_(key_t_* pos_, key_u_&& key_) noexcept;
204
205 key_t_* m_end_;
207};
208
209// Without the "requires" keyword these end up being ambiguous.
210#if HX_CPLUSPLUS >= 202002L
213template<typename key_t_, typename compare_t_, bool multi_t_,
214 hxsize_t capacity_x_, hxsize_t capacity_y_>
220
224template<typename key_t_, typename compare_t_, bool multi_t_,
225 hxsize_t capacity_x_, hxsize_t capacity_y_>
231
235template<typename key_t_, typename compare_t_, bool multi_t_>
241
242#endif // HX_CPLUSPLUS >= 202002L
243#include "detail/hxflat_set.inl"
244HX_NS_END_
hxallocator(void)
Definition hxallocator.hpp:35
const key_t_ * data(void) const
Definition hxallocator.hpp:45
hxflat_set - A sorted associative container that stores keys in a single array.
Definition hxflat_set.hpp:51
~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
Move overload of insert.
hxsize_t size(void) const
Returns the number of elements in the set.
Definition hxflat_set.hpp:187
hxsize_t capacity(void) const
Returns the capacity of the set or 0 if unallocated.
void swap(hxflat_set &x_) noexcept
Swaps the contents with x.
const key_t_ * erase(const key_t_ *pos_) noexcept
Removes the element at the pointer position and destroys the key.
const key_t_ * upper_bound(const key_t_ &key_) const
Returns a const pointer to the first element whose key is ordered after key.
void operator=(hxflat_set &&x_) noexcept
Move assigns from a temporary set using swap.
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:60
hxflat_set(const hxflat_set &x_) noexcept
Copy constructs from another hxflat_set.
compare_t_ compare_t
Definition hxflat_set.hpp:54
const key_t_ * iterator
iterator - Random access iterator over keys.
Definition hxflat_set.hpp:57
const key_t_ * insert(const key_t_ &key_) noexcept
Inserts a key.
bool equal(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.
bool full(void) const
Returns true if the set has reached its capacity.
Definition hxflat_set.hpp:150
const key_t_ * operator[](hxsize_t index_) const
Returns a const pointer to the element at index.
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:175
bool empty(void) const
Checks if the set contains no elements.
Definition hxflat_set.hpp:123
const key_t_ * end(void) const
Returns a const pointer past the last element.
Definition hxflat_set.hpp:126
const key_t_ * find(const key_t_ &key_) const
Returns a const pointer to the key if found, or hxnull if not present.
bool less(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 ...
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:101
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:53
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.
hxsize_t count(const key_t_ &key_) const
Returns the number of elements with the given key.
hxsize_t erase(const key_t_ &key_) noexcept
Removes all elements with the given key and destroys their keys.
const key_t_ * cend(void) const
Returns a const pointer past the last element.
Definition hxflat_set.hpp:110
const key_t_ * cbegin(void) const
Returns a const pointer to the first element (alias for begin).
Definition hxflat_set.hpp:107
hxkey_less_t<T> - A constexpr callable that invokes hxkey_less.
Definition hxkey.hpp:131
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
bool hxkey_equal(const hxflat_set< key_t_, compare_t_, multi_t_, capacity_x_ > &x_, const hxflat_set< key_t_, compare_t_, multi_t_, capacity_y_ > &y_)
bool hxkey_equal(hxflat_set<K>& x, hxflat_set<K>& y) - Compares the contents of x and y for equivalen...
Definition hxflat_set.hpp:215
void hxswap(hxflat_set< key_t_, compare_t_, multi_t_, hxallocator_dynamic_capacity > &x_, hxflat_set< key_t_, compare_t_, multi_t_, hxallocator_dynamic_capacity > &y_) noexcept
void hxswap(hxflat_set<K>& x, hxflat_set<K>& y) - Exchanges the contents of x and y.
Definition hxflat_set.hpp:236
bool hxkey_less(const hxflat_set< key_t_, compare_t_, multi_t_, capacity_x_ > &x_, const hxflat_set< key_t_, compare_t_, multi_t_, capacity_y_ > &y_)
bool hxkey_less(hxflat_set<K>& x, hxflat_set<K>& y) - Compares the contents of x and y lexicographica...
Definition hxflat_set.hpp:226
User-overloadable key-equal, key-less, and key-hash functions.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:82
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:77
unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:64
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:69
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:120
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:82
Standard sorting utilities.
void hxswap_memcpy(T &x_, T &y_)
hxswap_memcpy - Exchanges the contents of x and y using memcpy and a stack temporary.
Definition hxutility.h:354
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:209