libhatchet
Loading...
Searching...
No Matches
hxhandle_map.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
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
17#include "hxallocator.hpp"
18#include "hxutility.h"
19
20HX_NS_BEGIN_
21
23// Makes the mask a compile time constant when non-0.
24template<hxsize_t mask_>
25class hxhandle_map_mask_ {
26public:
27 hxinline_constexpr uint32_t get_mask_(void) const { return static_cast<uint32_t>(mask_); }
28 hxinline void set_mask_(uint32_t) const { } // GCOVR_EXCL_LINE. Unreachable code below c++17.
29};
30
31template<>
32class hxhandle_map_mask_<hxallocator_dynamic_capacity> {
33public:
34 hxinline hxattr_nodiscard uint32_t get_mask_(void) const { return m_mask_; }
35 hxinline void set_mask_(uint32_t mask_) { m_mask_ = mask_; }
36private:
37 uint32_t m_mask_;
38};
40
51template<typename T_, uint32_t table_size_bits_=hxallocator_dynamic_capacity>
53public:
54 static_assert(table_size_bits_ < 31u, "Slot index must fit in the low 31-bits of the handle");
55
57 using iterator = T_*;
58
60 using const_iterator = const T_*;
61
62 using value_t = T_;
63
66
69
72 hxinline hxattr_flatten const T_* begin(void) const { return this->data(); }
73
76 hxinline hxattr_flatten T_* begin(void) { return this->data(); }
77
79 hxinline hxattr_nodiscard hxsize_t capacity(void) const { return static_cast<hxsize_t>(m_mask_.get_mask_()); }
80
83 hxinline hxattr_flatten hxattr_nodiscard const T_* cbegin(void) const { return this->data(); }
84
87 hxinline hxattr_flatten hxattr_nodiscard const T_* cend(void) const { return this->data() + this->size(); }
88
90 void clear(void) noexcept;
91
93 hxinline hxattr_flatten hxattr_nodiscard const T_* data(void) const { return m_values_.data(); }
94
96 hxinline hxattr_flatten hxattr_nodiscard T_* data(void) { return m_values_.data(); }
97
100 hxinline hxattr_flatten hxattr_nodiscard const T_* end(void) const { return this->data() + this->size(); }
101
104 hxinline hxattr_flatten hxattr_nodiscard T_* end(void) { return this->data() + this->size(); }
105
107 hxinline hxattr_nodiscard bool empty(void) const { return m_size_ == 0u; }
108
112 bool erase(hxhandle_t handle_) noexcept;
113
117 template<typename callable_t_>
118 hxsize_t erase_if(callable_t_&& callable_) noexcept;
119
121 hxattr_nodiscard hxinline hxattr_flatten bool full(void) const { return m_size_ == m_mask_.get_mask_(); }
122
126 hxattr_nodiscard T_* get(hxhandle_t handle_) noexcept;
127
130 hxattr_nodiscard const T_* get(hxhandle_t handle_) const noexcept;
131
135
139 template<typename... args_t_>
140 hxhandle_t insert(args_t_&&... args_) noexcept;
141
143 hxinline hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
144
148 void set_size_bits(uint32_t bits_);
149
151 hxinline hxattr_nodiscard hxsize_t size(void) const { return static_cast<hxsize_t>(m_size_); }
152
153private:
154 // See detail/hxhandle_map.inl for the design.
155 static hxinline_constexpr hxsize_t s_capacity_ =
156 table_size_bits_ == hxallocator_dynamic_capacity
158 : static_cast<hxsize_t>(1) << table_size_bits_;
159
160 static hxinline_constexpr hxsize_t s_value_capacity_ =
161 table_size_bits_ == hxallocator_dynamic_capacity
163 : s_capacity_ - 1;
164
165 struct slot_t_ {
166 uint64_t m_handle_;
167 uint32_t m_index_;
168 uint32_t m_backref_;
169 };
170
171 void build_free_list_(void);
172
173 uint32_t m_size_;
174 hxhandle_map_mask_<s_value_capacity_> m_mask_;
175 hxallocator<slot_t_, s_capacity_> m_slots_;
176 hxallocator<T_, s_value_capacity_> m_values_;
177};
178
179#include "detail/hxhandle_map.inl"
180HX_NS_END_
bool empty(void) const
Checks if the map is empty.
Definition hxhandle_map.hpp:107
const T * data(void) const
Returns a const pointer to the first of size contiguous values.
Definition hxhandle_map.hpp:93
T * data(void)
Returns a pointer to the first of size contiguous values.
Definition hxhandle_map.hpp:96
~hxhandle_map()
Destructs the map and destroys all values.
T * end(void)
Returns a T* to the end of the values.
Definition hxhandle_map.hpp:104
bool erase(hxhandle_t handle_) noexcept
Returns true if handle resolved and its value was destroyed.
hxsize_t capacity(void) const
Returns the number of values that can be stored.
Definition hxhandle_map.hpp:79
void clear(void) noexcept
Destroys all values and invalidates their handles.
const T * end(void) const
Returns a const T* to the end of the values.
Definition hxhandle_map.hpp:100
const T * get(hxhandle_t handle_) const noexcept
const version of get.
hxsize_t size(void) const
Returns the number of values in the map.
Definition hxhandle_map.hpp:151
bool full(void) const
Checks if the map is full.
Definition hxhandle_map.hpp:121
const T * begin(void) const
Returns a const T* to the beginning of the values (alias for data).
Definition hxhandle_map.hpp:72
hxhandle_t insert(args_t_ &&... args_) noexcept
Returns a non-zero handle referencing a value constructed at the end of data from args.
hxsize_t erase_if(callable_t_ &&callable_) noexcept
Returns the number of values destroyed.
void set_size_bits(uint32_t bits_)
Sets the number of index bits and allocates memory for the map (only for dynamic capacity).
const T * const_iterator
const_iterator - Const random access iterator.
Definition hxhandle_map.hpp:60
const T * cbegin(void) const
Returns a const T* to the beginning of the values (alias for begin).
Definition hxhandle_map.hpp:83
T * iterator
iterator - Random access iterator.
Definition hxhandle_map.hpp:57
T * get(hxhandle_t handle_) noexcept
Returns a pointer to 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_map.hpp:143
hxhandle_map(void)
Constructs an empty map with a capacity of 2^table_size_bits - 1.
T * begin(void)
Returns a T* to the beginning of the values (alias for data).
Definition hxhandle_map.hpp:76
T value_t
Definition hxhandle_map.hpp:62
hxhandle_t handle_at(hxsize_t index_) const noexcept
Returns the handle for the value at begin() + index.
const T * cend(void) const
Returns a const T* to the end of the values.
Definition hxhandle_map.hpp:87
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
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:120
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:272
#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
A few utility functions and most standard C++ meta programming functions.
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