5
5
#include < vector>
6
6
#include < mutex>
7
7
8
+ /* *
9
+ * Thread-safe container for storing cell usage tree nodes.
10
+ * @tparam T node type
11
+ */
8
12
template <typename T>
9
- class DynamicArray {
10
- static constexpr size_t kBlockSize = std::max(1ul , 4096 / sizeof (T));
11
- static constexpr size_t kDefaultSize = 512 ;
13
+ class CellUsageContainer {
14
+ static constexpr size_t BLOCK_SIZE = std::max(1ul , 4096 / sizeof (T));
15
+ static constexpr size_t DEFAULT_CAP = 512 ;
12
16
13
17
public:
14
- explicit DynamicArray (size_t initial_size) : size_(0 ), cap_(0 ) {
18
+ explicit CellUsageContainer (size_t initial_size) : size_(0 ), cap_(0 ) {
15
19
static_assert (std::atomic<T**>::is_always_lock_free);
16
- resize (0 , (std::max (kDefaultSize , initial_size) + kBlockSize - 1 ) / kBlockSize );
20
+ ensure_capacity (0 , (std::max (DEFAULT_CAP , initial_size) + BLOCK_SIZE - 1 ) / BLOCK_SIZE );
17
21
size_ = initial_size;
18
22
}
19
23
20
24
const T& operator [](size_t i) const {
21
- return pointer_[i / kBlockSize ][i % kBlockSize ];
25
+ return pointer_[i / BLOCK_SIZE ][i % BLOCK_SIZE ];
22
26
}
23
27
24
28
T& operator [](size_t i) {
25
- return pointer_[i / kBlockSize ][i % kBlockSize ];
29
+ return pointer_[i / BLOCK_SIZE ][i % BLOCK_SIZE ];
26
30
}
27
31
28
32
size_t emplace_back () {
29
33
size_t pos = size_.fetch_add (1 );
30
34
size_t current_cap;
31
- while (pos / kBlockSize >= (current_cap = cap_.load ())) {
32
- resize (current_cap, 2 * current_cap);
35
+ while (pos / BLOCK_SIZE >= (current_cap = cap_.load ())) {
36
+ ensure_capacity (current_cap, 2 * current_cap);
33
37
}
34
38
return pos;
35
39
}
36
40
37
- ~DynamicArray () {
41
+ ~CellUsageContainer () {
38
42
for (size_t i = 0 ; i < cap_; ++i) {
39
43
delete[] pointer_[i];
40
44
}
@@ -53,7 +57,7 @@ class DynamicArray {
53
57
54
58
std::vector<T**> storage_;
55
59
56
- void resize (size_t current_cap, size_t target_cap) {
60
+ void ensure_capacity (size_t current_cap, size_t target_cap) {
57
61
std::lock_guard lock (resize_lock_);
58
62
if (current_cap != cap_) {
59
63
return ;
@@ -64,7 +68,7 @@ class DynamicArray {
64
68
new_data[i] = pointer_[i];
65
69
}
66
70
for (size_t i = cap_; i < target_cap; ++i) {
67
- new_data[i] = new T[kBlockSize ];
71
+ new_data[i] = new T[BLOCK_SIZE ];
68
72
}
69
73
storage_.emplace_back (new_data);
70
74
pointer_.store (new_data);
0 commit comments