libhatchet
Loading...
Searching...
No Matches
hxhandle_table.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 "hxptr.hpp"
18
19HX_NS_BEGIN_
20
21#include "detail/hxpow2_allocator.hpp"
22
23#if HX_CPLUSPLUS >= 202002L
25template<typename T_>
26concept hxhandle_table_concept_ = requires(T_& x_) {
27 sizeof(T_);
28 x_.~T_();
29};
31#else
32#define hxhandle_table_concept_ typename
33#endif
34
44template<hxhandle_table_concept_ T_,
45 typename deleter_t_=hxdefault_delete,
46 uint32_t table_size_bits_=hxallocator_dynamic_capacity>
47class hxhandle_table : private deleter_t_ {
48public:
49 // The capacity and a guard bit must fit in 32-bits to avoid 64-bit emulation.
50 static_assert(table_size_bits_ <= 30u, "hxhandle_table is designed for 2^30 or less slots");
51
52 using value_t = T_;
53
57 explicit hxhandle_table(deleter_t_ deleter_=deleter_t_());
58
61
62#if HX_CPLUSPLUS >= 202302L
68 template<typename self_t_, typename callable_t_>
70 this self_t_&& self_, hxhandle_t handle_, callable_t_&& callable_)
71 -> hxremove_cvref_t<decltype(
72 hxforward<callable_t_>(callable_)(
74#endif // HX_CPLUSPLUS >= 202302L
75
78 return static_cast<hxsize_t>(m_table_.get_mask_());
79 }
80
84 template<typename deleter_u_>
85 void clear(deleter_u_&& deleter_) noexcept;
86
88 void clear(void) noexcept { this->clear(this->deleter()); }
89
91 hxattr_nodiscard const deleter_t_& deleter(void) const;
92
93 hxattr_nodiscard deleter_t_& deleter(void);
94
96 hxattr_nodiscard bool empty(void) const { return m_size_ == 0; }
97
102 template<typename callable_t_>
103 hxsize_t erase_if(callable_t_&& callable_) noexcept;
104
109
112 bool full(void) const {
113 return m_free_head_ == m_table_.data();
114 }
115
119 hxhandle_t insert(T_* ptr_) noexcept;
120
125 template<typename deleter_u_>
127
130 hxsize_t max_size(void) const {
131 return static_cast<hxsize_t>(m_table_.get_mask_());
132 }
133
134#if HX_CPLUSPLUS >= 202302L
140 template<typename self_t_, typename callable_t_>
142 this self_t_&& self_, hxhandle_t handle_, callable_t_&& callable_)
143 -> decltype(self_.value(handle_));
144#endif // HX_CPLUSPLUS >= 202302L
145
147 void release_all(void) noexcept { this->clear(hxdo_not_delete()); }
148
153 template<typename deleter_u_>
154 bool reset(hxhandle_t handle_, deleter_u_&& deleter_) noexcept;
155
159 bool reset(hxhandle_t handle_) noexcept { return this->reset(handle_, this->deleter()); }
160
165 void set_size_bits(uint32_t bits_);
166
168 hxattr_nodiscard hxsize_t size(void) const { return static_cast<hxsize_t>(m_size_); }
169
173 hxattr_nodiscard const T_* value(hxhandle_t handle_) const noexcept;
174
175 hxattr_nodiscard T_* value(hxhandle_t handle_) noexcept;
176
177#if HX_CPLUSPLUS >= 202302L
182 template<typename self_t_, typename... args_t_>
184 this self_t_&& self_, hxhandle_t handle_, args_t_&&... args_);
185#endif // HX_CPLUSPLUS >= 202302L
186
187private:
188 class slot_t_ {
189 public:
190 uint64_t m_key_;
191 union {
192 T_* m_ptr_;
193 slot_t_* m_next_;
194 };
195 };
196
197 void build_free_list_(void);
198
199 hxdetail_::hxpow2_allocator_<slot_t_, table_size_bits_, false> m_table_;
200 uint32_t m_size_;
201 slot_t_* m_free_head_;
202};
203
204#include "detail/hxhandle_table.inl"
205HX_NS_END_
hxdefault_delete - A callable that deletes objects of type T using hxdelete.
Definition hxmemory_manager.h:265
hxdo_not_delete - A version of hxdefault_delete that does not delete the object.
Definition hxmemory_manager.h:278
const T * value(hxhandle_t handle_) const noexcept
Returns the value referenced by handle if it resolves, otherwise hxnull.
hxsize_t max_size(void) const
Returns the maximum number of values that can be stored.
Definition hxhandle_table.hpp:130
hxsize_t capacity(void) const
Returns the number of slots in the table.
Definition hxhandle_table.hpp:77
deleter_t_ & deleter(void)
hxhandle_t insert(hxptr< T, deleter_u_ > &&ptr_) noexcept
Takes ownership of the value owned by ptr and returns a non-zero handle referencing it,...
hxhandle_t insert(T *ptr_) noexcept
Takes ownership of ptr and returns a non-zero handle referencing it, or hxnull_handle if ptr is null.
T value_or(this self_t_ &&self_, hxhandle_t handle_, args_t_ &&... args_)
Returns a copy of the value referenced by handle, or a value constructed from args when the handle do...
auto and_then(this self_t_ &&self_, hxhandle_t handle_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxforward_like< self_t_ >(hxdeclval< T & >())))>
Returns the result of calling callable with the value referenced by handle, otherwise returns hxnil.
bool full(void) const
Checks if the table is full.
Definition hxhandle_table.hpp:112
hxptr< T, deleter_t_ > extract(hxhandle_t handle_) noexcept
Returns an hxptr owning the value referenced by handle, or an empty hxptr if handle does not resolve.
auto or_else(this self_t_ &&self_, hxhandle_t handle_, callable_t_ &&callable_) -> decltype(self_.value(handle_))
Returns a pointer to the value referenced by handle, or the result of calling callable when the handl...
bool empty(void) const
Checks if the table is empty.
Definition hxhandle_table.hpp:96
const deleter_t_ & deleter(void) const
Returns a const reference to the stored deleter.
void set_size_bits(uint32_t bits_)
Sets the number of index bits and allocates memory for the table (only for dynamic capacity).
hxhandle_table(deleter_t_ deleter_=deleter_t_())
Constructs an empty table with a capacity of 2^table_size_bits - 1 slots and an optional deleter inst...
T value_t
Definition hxhandle_table.hpp:52
void clear(deleter_u_ &&deleter_) noexcept
Removes all values and if deleter is true then calls deleter() on every value.
hxsize_t erase_if(callable_t_ &&callable_) noexcept
Returns the number of values destroyed.
bool reset(hxhandle_t handle_, deleter_u_ &&deleter_) noexcept
Releases the value referenced by handle if deleter is true then calls deleter on it.
bool reset(hxhandle_t handle_) noexcept
Removes and calls the stored deleter on the value referenced by handle.
Definition hxhandle_table.hpp:159
hxsize_t size(void) const
Returns the number of values in the table.
Definition hxhandle_table.hpp:168
void release_all(void) noexcept
Clears the table without deleting any values.
Definition hxhandle_table.hpp:147
void clear(void) noexcept
Removes all values and calls the stored deleter on every value.
Definition hxhandle_table.hpp:88
~hxhandle_table(void)
Destructs the table and deletes all owned values.
T * value(hxhandle_t handle_) noexcept
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
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
A unique owning pointer.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:90
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.
uint64_t hxhandle_t
hxhandle_t - An opaque 64-bit handle.
Definition libhatchet.h:213
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:207