Commit: f2c8e39d2985f820701f981d5b396bbb671e9ac5
Author: Jacques Lucke
Date:   Sun Feb 17 13:32:29 2019 +0100
Branches: functions
https://developer.blender.org/rBf2c8e39d2985f820701f981d5b396bbb671e9ac5

small stack

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

A       source/blender/blenlib/BLI_small_stack.hpp
M       source/blender/blenlib/BLI_small_vector.hpp
M       source/blender/blenlib/CMakeLists.txt
A       tests/gtests/blenlib/BLI_small_stack_test.cc
M       tests/gtests/blenlib/BLI_small_vector_test.cc
M       tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_small_stack.hpp 
b/source/blender/blenlib/BLI_small_stack.hpp
new file mode 100644
index 00000000000..51abdc5fd2d
--- /dev/null
+++ b/source/blender/blenlib/BLI_small_stack.hpp
@@ -0,0 +1,43 @@
+#include "BLI_small_vector.hpp"
+
+namespace BLI {
+
+       template<typename T>
+       class SmallStack {
+       private:
+               SmallVector<T> m_elements;
+
+       public:
+               SmallStack() = default;
+
+               uint size() const
+               {
+                       return m_elements.size();
+               }
+
+               bool empty() const
+               {
+                       return this->size() == 0;
+               }
+
+               void push(T value)
+               {
+                       m_elements.append(value);
+               }
+
+               T pop()
+               {
+                       BLI_assert(!this->empty());
+                       T value = m_elements[this->size() - 1];
+                       m_elements.remove_last();
+                       return value;
+               }
+
+               T &peek()
+               {
+                       BLI_assert(!this->empty());
+                       return m_elements[this->size() - 1];
+               }
+       };
+
+} /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_small_vector.hpp 
b/source/blender/blenlib/BLI_small_vector.hpp
index 87c748b1786..4b82ad418fa 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -110,6 +110,18 @@ namespace BLI {
                        return m_size;
                }
 
+               bool empty() const
+               {
+                       return this->size() == 0;
+               }
+
+               void remove_last()
+               {
+                       BLI_assert(!this->empty());
+                       (this->m_elements + m_size - 1)->~T();
+                       m_size--;
+               }
+
                int index(const T &value) const
                {
                        for (uint i = 0; i < m_size; i++) {
diff --git a/source/blender/blenlib/CMakeLists.txt 
b/source/blender/blenlib/CMakeLists.txt
index bc0b78bec51..ee77bb7a42b 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -231,6 +231,7 @@ set(SRC
        BLI_small_map.hpp
        BLI_small_set.hpp
        BLI_small_set_vector.hpp
+       BLI_small_stack.hpp
 )
 
 if(WITH_MEM_VALGRIND)
diff --git a/tests/gtests/blenlib/BLI_small_stack_test.cc 
b/tests/gtests/blenlib/BLI_small_stack_test.cc
new file mode 100644
index 00000000000..9f1b1e08ded
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_small_stack_test.cc
@@ -0,0 +1,41 @@
+#include "testing/testing.h"
+#include "BLI_small_stack.hpp"
+
+using IntStack = BLI::SmallStack<int>;
+
+TEST(small_stack, DefaultConstructor)
+{
+       IntStack stack;
+       EXPECT_EQ(stack.size(), 0);
+       EXPECT_TRUE(stack.empty());
+}
+
+TEST(small_stack, Push)
+{
+       IntStack stack;
+       EXPECT_EQ(stack.size(), 0);
+       stack.push(3);
+       EXPECT_EQ(stack.size(), 1);
+       stack.push(5);
+       EXPECT_EQ(stack.size(), 2);
+}
+
+TEST(small_stack, Pop)
+{
+       IntStack stack;
+       stack.push(4);
+       stack.push(6);
+       EXPECT_EQ(stack.pop(), 6);
+       EXPECT_EQ(stack.pop(), 4);
+}
+
+TEST(small_stack, Peek)
+{
+       IntStack stack;
+       stack.push(3);
+       stack.push(4);
+       EXPECT_EQ(stack.peek(), 4);
+       EXPECT_EQ(stack.peek(), 4);
+       stack.pop();
+       EXPECT_EQ(stack.peek(), 3);
+}
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc 
b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 69861dd032c..1e781cb4ae4 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -155,4 +155,24 @@ TEST(small_vector, VectorOfVectors_Fill)
        EXPECT_EQ(vec[1][1], 5);
        EXPECT_EQ(vec[2][0], 4);
        EXPECT_EQ(vec[2][1], 5);
+}
+
+TEST(small_vector, RemoveLast)
+{
+       IntVector vec = {5, 6};
+       EXPECT_EQ(vec.size(), 2);
+       vec.remove_last();
+       EXPECT_EQ(vec.size(), 1);
+       vec.remove_last();
+       EXPECT_EQ(vec.size(), 0);
+}
+
+TEST(small_vector, Empty)
+{
+       IntVector vec;
+       EXPECT_TRUE(vec.empty());
+       vec.append(1);
+       EXPECT_FALSE(vec.empty());
+       vec.remove_last();
+       EXPECT_TRUE(vec.empty());
 }
\ No newline at end of file
diff --git a/tests/gtests/blenlib/CMakeLists.txt 
b/tests/gtests/blenlib/CMakeLists.txt
index 40ed7658d5e..502fc137024 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -58,6 +58,7 @@ BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")
 BLENDER_TEST(BLI_shared "bf_blenlib")
 BLENDER_TEST(BLI_small_vector "bf_blenlib")
 BLENDER_TEST(BLI_small_set_vector "bf_blenlib")
+BLENDER_TEST(BLI_small_stack "bf_blenlib")
 BLENDER_TEST(BLI_small_map "bf_blenlib")
 BLENDER_TEST(BLI_stack "bf_blenlib")
 BLENDER_TEST(BLI_string "bf_blenlib")

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to