diff options
author | Jacques Lucke <jacques@blender.org> | 2022-09-10 02:13:40 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-09-10 02:13:40 +0300 |
commit | eac00ae37015c8fb09c451a7b236a8a7eb174f8b (patch) | |
tree | a8e35da5ca242704ec051ef0515daeca30b70b06 /source/blender/blenlib | |
parent | f7da4141c40849674d00783828134a2cbe8fdba8 (diff) |
add stack functions
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_chunk_list.hh | 33 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_chunk_list_test.cc | 17 |
2 files changed, 49 insertions, 1 deletions
diff --git a/source/blender/blenlib/BLI_chunk_list.hh b/source/blender/blenlib/BLI_chunk_list.hh index addcc1273ca..fd1d85ac248 100644 --- a/source/blender/blenlib/BLI_chunk_list.hh +++ b/source/blender/blenlib/BLI_chunk_list.hh @@ -142,6 +142,13 @@ class ChunkList { return active_end_ == inline_buffer_; } + int64_t size() const + { + int64_t chunk_size_sum = 0; + this->foreach_chunk([&](const Span<T> chunk) { chunk_size_sum += chunk.size(); }); + return chunk_size_sum; + } + int64_t get_chunk_num() const { if (alloc_info_ == nullptr) { @@ -213,6 +220,32 @@ class ChunkList { } } + T pop_last() + { + BLI_assert(!this->is_empty()); + T value = std::move(*(active_end_ - 1)); + active_end_--; + std::destroy_at(active_end_); + + if (alloc_info_ != nullptr) { + RawChunk &old_chunk = alloc_info_->raw_chunks[alloc_info_->active_chunk]; + if (active_end_ == old_chunk.begin) { + BLI_assert(old_chunk.capacity_end == active_capacity_end_); + old_chunk.end_if_inactive = active_end_; + while (alloc_info_->active_chunk > 0) { + alloc_info_->active_chunk--; + RawChunk &chunk = alloc_info_->raw_chunks[alloc_info_->active_chunk]; + if (chunk.begin < chunk.end_if_inactive) { + active_end_ = chunk.end_if_inactive; + active_capacity_end_ = chunk.capacity_end; + break; + } + } + } + } + return value; + } + class Iterator { private: using iterator_category = std::forward_iterator_tag; diff --git a/source/blender/blenlib/tests/BLI_chunk_list_test.cc b/source/blender/blenlib/tests/BLI_chunk_list_test.cc index 052c6f18799..5e42f779c2e 100644 --- a/source/blender/blenlib/tests/BLI_chunk_list_test.cc +++ b/source/blender/blenlib/tests/BLI_chunk_list_test.cc @@ -9,7 +9,7 @@ namespace blender::tests { TEST(chunk_list, Test) { - const int64_t amount = 1e9; + const int64_t amount = 1e4; for ([[maybe_unused]] const int64_t iter : IndexRange(5)) { { ChunkList<int, 2> list; @@ -50,4 +50,19 @@ TEST(chunk_list, Test) } } +TEST(chunk_list, Stack) +{ + ChunkList<int64_t> list; + const int64_t amount = 1e5; + for (const int64_t i : IndexRange(amount)) { + list.append(i); + } + EXPECT_EQ(list.size(), amount); + for (const int64_t i : IndexRange(amount)) { + const int popped_value = list.pop_last(); + EXPECT_EQ(popped_value, amount - i - 1); + } + EXPECT_EQ(list.size(), 0); +} + } // namespace blender::tests |