1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup bli
*
* This class offers a useful abstraction for other containers that implement hash tables using
* open addressing. It handles the following aspects:
* - Allocation and deallocation of the open addressing array.
* - Optional small object optimization.
* - Keeps track of how many elements and dummies are in the table.
*
* The nice thing about this abstraction is that it does not get in the way of any performance
* optimizations. The data that is actually stored in the table is still fully defined by the
* actual hash table implementation.
*/
#pragma once
#include <cmath>
#include "BLI_utildefines.h"
#include "BLI_memory_utils_cxx.h"
#include "BLI_math_base.h"
#include "BLI_allocator.h"
namespace BLI {
template<typename Item, uint32_t ItemsInSmallStorage = 1, typename Allocator = GuardedAllocator>
class OpenAddressingArray {
private:
static constexpr uint32_t slots_per_item = Item::slots_per_item;
static constexpr float max_load_factor = 0.5f;
/* Invariants:
* 2^m_item_exponent = m_item_amount
* m_item_amount * slots_per_item = m_slots_total
* m_slot_mask = m_slots_total - 1
* m_slots_set_or_dummy < m_slots_total
*/
/* Array containing the actual hash table. Might be a pointer to the inlined storage. */
Item *m_items;
/* Number of items in the hash table. Must be a power of two. */
uint32_t m_item_amount;
/* Exponent of the current item amount. */
uint8_t m_item_exponent;
/* Number of elements that could be stored in the table when the load factor is 1. */
uint32_t m_slots_total;
/* Number of elements that are not empty. */
uint32_t m_slots_set_or_dummy;
/* Number of dummy entries. */
uint32_t m_slots_dummy;
/* Max number of slots that can be non-empty according to the load factor. */
uint32_t m_slots_usable;
/* Can be used to map a hash value into the range of valid slot indices. */
uint32_t m_slot_mask;
Allocator m_allocator;
char m_local_storage[sizeof(Item) * ItemsInSmallStorage];
public:
explicit OpenAddressingArray(uint8_t item_exponent = 0)
{
m_slots_total = ((uint32_t)1 << item_exponent) * slots_per_item;
m_slots_set_or_dummy = 0;
m_slots_dummy = 0;
m_slots_usable = (uint32_t)((float)m_slots_total * max_load_factor);
m_slot_mask = m_slots_total - 1;
m_item_amount = m_slots_total / slots_per_item;
m_item_exponent = item_exponent;
if (m_item_amount <= ItemsInSmallStorage) {
m_items = this->small_storage();
}
else {
m_items = (Item *)m_allocator.allocate_aligned(
(uint32_t)sizeof(Item) * m_item_amount, std::alignment_of<Item>::value, __func__);
}
for (uint32_t i = 0; i < m_item_amount; i++) {
new (m_items + i) Item();
}
}
~OpenAddressingArray()
{
if (m_items != nullptr) {
for (uint32_t i = 0; i < m_item_amount; i++) {
m_items[i].~Item();
}
if (!this->is_in_small_storage()) {
m_allocator.deallocate((void *)m_items);
}
}
}
OpenAddressingArray(const OpenAddressingArray &other)
{
m_slots_total = other.m_slots_total;
m_slots_set_or_dummy = other.m_slots_set_or_dummy;
m_slots_dummy = other.m_slots_dummy;
m_slots_usable = other.m_slots_usable;
m_slot_mask = other.m_slot_mask;
m_item_amount = other.m_item_amount;
m_item_exponent = other.m_item_exponent;
if (m_item_amount <= ItemsInSmallStorage) {
m_items = this->small_storage();
}
else {
m_items = (Item *)m_allocator.allocate_aligned(
sizeof(Item) * m_item_amount, std::alignment_of<Item>::value, __func__);
}
uninitialized_copy_n(other.m_items, m_item_amount, m_items);
}
OpenAddressingArray(OpenAddressingArray &&other) noexcept
{
m_slots_total = other.m_slots_total;
m_slots_set_or_dummy = other.m_slots_set_or_dummy;
m_slots_dummy = other.m_slots_dummy;
m_slots_usable = other.m_slots_usable;
m_slot_mask = other.m_slot_mask;
m_item_amount = other.m_item_amount;
m_item_exponent = other.m_item_exponent;
if (other.is_in_small_storage()) {
m_items = this->small_storage();
uninitialized_relocate_n(other.m_items, m_item_amount, m_items);
}
else {
m_items = other.m_items;
}
other.m_items = nullptr;
other.~OpenAddressingArray();
new (&other) OpenAddressingArray();
}
OpenAddressingArray &operator=(const OpenAddressingArray &other)
{
if (this == &other) {
return *this;
}
this->~OpenAddressingArray();
new (this) OpenAddressingArray(other);
return *this;
}
OpenAddressingArray &operator=(OpenAddressingArray &&other)
{
if (this == &other) {
return *this;
}
this->~OpenAddressingArray();
new (this) OpenAddressingArray(std::move(other));
return *this;
}
/* Prepare a new array that can hold a minimum of min_usable_slots elements. All entries are
* empty. */
OpenAddressingArray init_reserved(uint32_t min_usable_slots) const
{
float min_total_slots = (float)min_usable_slots / max_load_factor;
uint32_t min_total_items = (uint32_t)std::ceil(min_total_slots / (float)slots_per_item);
uint8_t item_exponent = (uint8_t)log2_ceil_u(min_total_items);
OpenAddressingArray grown(item_exponent);
grown.m_slots_set_or_dummy = this->slots_set();
return grown;
}
/**
* Amount of items in the array times the number of slots per item.
*/
uint32_t slots_total() const
{
return m_slots_total;
}
/**
* Amount of slots that are initialized with some value that is not empty or dummy.
*/
uint32_t slots_set() const
{
return m_slots_set_or_dummy - m_slots_dummy;
}
/**
* Amount of slots that can be used before the array should grow.
*/
uint32_t slots_usable() const
{
return m_slots_usable;
}
/**
* Update the counters after one empty element is used for a newly added element.
*/
void update__empty_to_set()
{
m_slots_set_or_dummy++;
}
/**
* Update the counters after one previously dummy element becomes set.
*/
void update__dummy_to_set()
{
m_slots_dummy--;
}
/**
* Update the counters after one previously set element becomes a dummy.
*/
void update__set_to_dummy()
{
m_slots_dummy++;
}
/**
* Access the current slot mask for this array.
*/
uint32_t slot_mask() const
{
return m_slot_mask;
}
/**
* Access the item for a specific item index.
* Note: The item index is not necessarily the slot index.
*/
const Item &item(uint32_t item_index) const
{
return m_items[item_index];
}
Item &item(uint32_t item_index)
{
return m_items[item_index];
}
uint8_t item_exponent() const
{
return m_item_exponent;
}
uint32_t item_amount() const
{
return m_item_amount;
}
bool should_grow() const
{
return m_slots_set_or_dummy >= m_slots_usable;
}
Item *begin()
{
return m_items;
}
Item *end()
{
return m_items + m_item_amount;
}
const Item *begin() const
{
return m_items;
}
const Item *end() const
{
return m_items + m_item_amount;
}
private:
Item *small_storage() const
{
return reinterpret_cast<Item *>((char *)m_local_storage);
}
bool is_in_small_storage() const
{
return m_items == this->small_storage();
}
};
} // namespace BLI
|