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 "hxptr.hpp"
17#include "hxallocator.hpp"
18
19HX_NS_BEGIN_
20
21#include "detail/hxpow2_allocator.hpp"
22
32template<typename T_,
33 typename deleter_t_=hxdefault_delete,
34 uint32_t table_size_bits_=hxallocator_dynamic_capacity>
35class hxhandle_table : private deleter_t_ {
36public:
37 // The capacity and a guard bit must fit in 32-bits to avoid 64-bit emulation.
38 static_assert(table_size_bits_ <= 30u, "hxhandle_table is designed for 2^30 or less slots");
39
40 using value_t = T_;
41
45 explicit hxhandle_table(deleter_t_ deleter_=deleter_t_());
46
49
51 hxattr_nodiscard hxsize_t capacity(void) const { return static_cast<hxsize_t>(m_table_.get_mask_()); }
52
56 template<typename deleter_u_>
57 void clear(deleter_u_&& deleter_) noexcept;
58
60 void clear(void) noexcept { this->clear(this->deleter()); }
61
63 hxattr_nodiscard bool empty(void) const { return m_size_ == 0; }
64
69 template<typename deleter_u_>
70 bool erase(hxhandle_t handle_, deleter_u_&& deleter_) noexcept;
71
75 bool erase(hxhandle_t handle_) noexcept { return this->erase(handle_, this->deleter()); }
76
81 template<typename callable_t_>
82 hxsize_t erase_if(callable_t_&& callable_) noexcept;
83
88
90 hxattr_nodiscard hxinline hxattr_flatten bool full(void) const { return m_free_head_ == m_table_.data(); }
91
94 hxattr_nodiscard T_* get(hxhandle_t handle_) noexcept;
95
98 hxattr_nodiscard const T_* get(hxhandle_t handle_) const noexcept;
99
101 hxattr_nodiscard const deleter_t_& deleter(void) const;
102
104 hxattr_nodiscard deleter_t_& deleter(void);
105
109 hxhandle_t insert(T_* ptr_) noexcept;
110
115 template<typename deleter_u_>
117
119 hxattr_nodiscard hxinline hxattr_flatten hxsize_t max_size(void) const { return static_cast<hxsize_t>(m_table_.get_mask_()); }
120
122 void release_all(void) noexcept { this->clear(hxdo_not_delete()); }
123
127 void set_size_bits(uint32_t bits_);
128
130 hxattr_nodiscard hxsize_t size(void) const { return static_cast<hxsize_t>(m_size_); }
131
132private:
133 class slot_t_ {
134 public:
135 uint64_t m_key_;
136 union {
137 T_* m_ptr_;
138 slot_t_* m_next_;
139 };
140 };
141
142 void build_free_list_(void);
143
144 hxdetail_::hxpow2_allocator_<slot_t_, table_size_bits_, false> m_table_;
145 uint32_t m_size_;
146 slot_t_* m_free_head_;
147};
148
149#include "detail/hxhandle_table.inl"
150HX_NS_END_
hxdefault_delete - A callable that deletes objects of type T using hxdelete.
Definition hxmemory_manager.h:254
hxdo_not_delete - A version of hxdefault_delete that does not delete the object.
Definition hxmemory_manager.h:267
hxsize_t max_size(void) const
Returns the maximum number of values that can be stored.
Definition hxhandle_table.hpp:119
hxsize_t capacity(void) const
Returns the number of slots in the table.
Definition hxhandle_table.hpp:51
deleter_t_ & deleter(void)
Returns a reference to the stored deleter.
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.
const T * get(hxhandle_t handle_) const noexcept
const version of get.
bool full(void) const
Checks if the table is full.
Definition hxhandle_table.hpp:90
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.
bool empty(void) const
Checks if the table is empty.
Definition hxhandle_table.hpp:63
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 slots and an optional deleter instance...
T value_t
Definition hxhandle_table.hpp:40
void clear(deleter_u_ &&deleter_) noexcept
Removes all values and if deleter is true then calls deleter() on every value.
bool erase(hxhandle_t handle_) noexcept
Removes and calls the stored deleter on the value referenced by handle.
Definition hxhandle_table.hpp:75
hxsize_t erase_if(callable_t_ &&callable_) noexcept
Returns the number of values destroyed.
bool erase(hxhandle_t handle_, deleter_u_ &&deleter_) noexcept
Releases the value referenced by handle if deleter is true then calls deleter on it.
hxsize_t size(void) const
Returns the number of values in the table.
Definition hxhandle_table.hpp:130
void release_all(void) noexcept
Clears the table without deleting any values.
Definition hxhandle_table.hpp:122
void clear(void) noexcept
Removes all values and calls the stored deleter on every value.
Definition hxhandle_table.hpp:60
~hxhandle_table(void)
Destructs the table and deletes all owned values.
T * get(hxhandle_t handle_) noexcept
Returns the value referenced by handle if it resolves, otherwise hxnull.
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: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
Provides core macros, memory management and feature detection.
uint64_t hxhandle_t
hxhandle_t - An opaque 64-bit handle.
Definition libhatchet.h:215
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209