55#include < vector>
66#include < mutex>
77
8+ /* *
9+ * Thread-safe container for storing cell usage tree nodes.
10+ * @tparam T node type
11+ */
812template <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 ;
1216
1317 public:
14- explicit DynamicArray (size_t initial_size) : size_(0 ), cap_(0 ) {
18+ explicit CellUsageContainer (size_t initial_size) : size_(0 ), cap_(0 ) {
1519 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 );
1721 size_ = initial_size;
1822 }
1923
2024 const T& operator [](size_t i) const {
21- return pointer_[i / kBlockSize ][i % kBlockSize ];
25+ return pointer_[i / BLOCK_SIZE ][i % BLOCK_SIZE ];
2226 }
2327
2428 T& operator [](size_t i) {
25- return pointer_[i / kBlockSize ][i % kBlockSize ];
29+ return pointer_[i / BLOCK_SIZE ][i % BLOCK_SIZE ];
2630 }
2731
2832 size_t emplace_back () {
2933 size_t pos = size_.fetch_add (1 );
3034 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);
3337 }
3438 return pos;
3539 }
3640
37- ~DynamicArray () {
41+ ~CellUsageContainer () {
3842 for (size_t i = 0 ; i < cap_; ++i) {
3943 delete[] pointer_[i];
4044 }
@@ -53,7 +57,7 @@ class DynamicArray {
5357
5458 std::vector<T**> storage_;
5559
56- void resize (size_t current_cap, size_t target_cap) {
60+ void ensure_capacity (size_t current_cap, size_t target_cap) {
5761 std::lock_guard lock (resize_lock_);
5862 if (current_cap != cap_) {
5963 return ;
@@ -64,7 +68,7 @@ class DynamicArray {
6468 new_data[i] = pointer_[i];
6569 }
6670 for (size_t i = cap_; i < target_cap; ++i) {
67- new_data[i] = new T[kBlockSize ];
71+ new_data[i] = new T[BLOCK_SIZE ];
6872 }
6973 storage_.emplace_back (new_data);
7074 pointer_.store (new_data);
0 commit comments