[Bf-blender-cvs] [2646b9016f4] functions: more utility functions for vector and stack

Jacques Lucke noreply at git.blender.org
Sat Dec 14 14:46:49 CET 2019


Commit: 2646b9016f4ff69c4eacab91fc73b6d67c9f9c72
Author: Jacques Lucke
Date:   Sat Dec 14 14:45:37 2019 +0100
Branches: functions
https://developer.blender.org/rB2646b9016f4ff69c4eacab91fc73b6d67c9f9c72

more utility functions for vector and stack

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

M	source/blender/blenlib/BLI_stack_cxx.h
M	source/blender/blenlib/BLI_vector.h
M	tests/gtests/blenlib/BLI_stack_cxx_test.cc
M	tests/gtests/blenlib/BLI_vector_test.cc

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

diff --git a/source/blender/blenlib/BLI_stack_cxx.h b/source/blender/blenlib/BLI_stack_cxx.h
index 7915acadfac..1ed6a6e91dc 100644
--- a/source/blender/blenlib/BLI_stack_cxx.h
+++ b/source/blender/blenlib/BLI_stack_cxx.h
@@ -76,6 +76,11 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class St
     m_elements.append(std::move(value));
   }
 
+  void push_multiple(ArrayRef<T> values)
+  {
+    m_elements.extend(values);
+  }
+
   /**
    * Remove the element from the top of the stack and return it.
    * This will assert when the stack is empty.
diff --git a/source/blender/blenlib/BLI_vector.h b/source/blender/blenlib/BLI_vector.h
index d26a2a728b0..0f011621420 100644
--- a/source/blender/blenlib/BLI_vector.h
+++ b/source/blender/blenlib/BLI_vector.h
@@ -307,6 +307,13 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     return index;
   }
 
+  void append_non_duplicates(const T &value)
+  {
+    if (!this->contains(value)) {
+      this->append(value);
+    }
+  }
+
   void append_unchecked(const T &value)
   {
     BLI_assert(m_end < m_capacity_end);
@@ -355,6 +362,13 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     this->extend_unchecked(start, amount);
   }
 
+  void extend_non_duplicates(ArrayRef<T> array)
+  {
+    for (const T &value : array) {
+      this->append_non_duplicates(value);
+    }
+  }
+
   void extend_unchecked(ArrayRef<T> array)
   {
     this->extend_unchecked(array.begin(), array.size());
diff --git a/tests/gtests/blenlib/BLI_stack_cxx_test.cc b/tests/gtests/blenlib/BLI_stack_cxx_test.cc
index 436f1f307b9..971c128d925 100644
--- a/tests/gtests/blenlib/BLI_stack_cxx_test.cc
+++ b/tests/gtests/blenlib/BLI_stack_cxx_test.cc
@@ -32,6 +32,17 @@ TEST(stack, Push)
   EXPECT_EQ(stack.size(), 2);
 }
 
+TEST(stack, PushMultiple)
+{
+  IntStack stack;
+  EXPECT_EQ(stack.size(), 0);
+  stack.push_multiple({1, 2, 3});
+  EXPECT_EQ(stack.size(), 3);
+  EXPECT_EQ(stack.pop(), 3);
+  EXPECT_EQ(stack.pop(), 2);
+  EXPECT_EQ(stack.pop(), 1);
+}
+
 TEST(stack, Pop)
 {
   IntStack stack;
diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc
index c2ea3f10ff6..6cf67fb2488 100644
--- a/tests/gtests/blenlib/BLI_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_vector_test.cc
@@ -226,6 +226,28 @@ TEST(vector, AppendAndGetIndex)
   EXPECT_EQ(vec.append_and_get_index(10), 4);
 }
 
+TEST(vector, AppendNonDuplicates)
+{
+  IntVector vec;
+  vec.append_non_duplicates(4);
+  EXPECT_EQ(vec.size(), 1);
+  vec.append_non_duplicates(5);
+  EXPECT_EQ(vec.size(), 2);
+  vec.append_non_duplicates(4);
+  EXPECT_EQ(vec.size(), 2);
+}
+
+TEST(vector, ExtendNonDuplicates)
+{
+  IntVector vec;
+  vec.extend_non_duplicates({1, 2});
+  EXPECT_EQ(vec.size(), 2);
+  vec.extend_non_duplicates({3, 4});
+  EXPECT_EQ(vec.size(), 4);
+  vec.extend_non_duplicates({0, 1, 2, 3});
+  EXPECT_EQ(vec.size(), 5);
+}
+
 TEST(vector, Fill)
 {
   IntVector vec(5);



More information about the Bf-blender-cvs mailing list