[Bf-blender-cvs] [eac00ae3701] temp-chunk-list: add stack functions

Jacques Lucke noreply at git.blender.org
Sat Sep 10 13:27:15 CEST 2022


Commit: eac00ae37015c8fb09c451a7b236a8a7eb174f8b
Author: Jacques Lucke
Date:   Sat Sep 10 01:13:40 2022 +0200
Branches: temp-chunk-list
https://developer.blender.org/rBeac00ae37015c8fb09c451a7b236a8a7eb174f8b

add stack functions

===================================================================

M	source/blender/blenlib/BLI_chunk_list.hh
M	source/blender/blenlib/tests/BLI_chunk_list_test.cc

===================================================================

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



More information about the Bf-blender-cvs mailing list