Commit: b166e18d56e242d3da81616ceb28c4477483f7b4
Author: Jacques Lucke
Date:   Sun Feb 17 15:49:43 2019 +0100
Branches: functions
https://developer.blender.org/rBb166e18d56e242d3da81616ceb28c4477483f7b4

small vector - remove and reorder

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

M       source/blender/blenlib/BLI_small_vector.hpp
M       tests/gtests/blenlib/BLI_small_vector_test.cc

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

diff --git a/source/blender/blenlib/BLI_small_vector.hpp 
b/source/blender/blenlib/BLI_small_vector.hpp
index ca5cb47b6d7..8ed42027e4a 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -119,7 +119,21 @@ namespace BLI {
                void remove_last()
                {
                        BLI_assert(!this->empty());
-                       (this->m_elements + m_size - 1)->~T();
+                       this->destruct_element(m_size - 1);
+                       m_size--;
+               }
+
+               void remove_and_reorder(uint index)
+               {
+                       BLI_assert(this->is_index_in_range(index));
+                       if (index < m_size - 1) {
+                               /* Move last element to index. */
+                               std::copy(
+                                       std::make_move_iterator(this->end() - 
1),
+                                       std::make_move_iterator(this->end()),
+                                       this->element_ptr(index));
+                       }
+                       this->destruct_element(m_size - 1);
                        m_size--;
                }
 
@@ -135,7 +149,7 @@ namespace BLI {
 
                T &operator[](const int index) const
                {
-                       BLI_assert(index >= 0 && index < this->size());
+                       BLI_assert(this->is_index_in_range(index));
                        return m_elements[index];
                }
 
@@ -168,6 +182,16 @@ namespace BLI {
                        return m_elements == this->small_buffer();
                }
 
+               bool is_index_in_range(uint index) const
+               {
+                       return index >= 0 && index < this->size();
+               }
+
+               T *element_ptr(uint index) const
+               {
+                       return m_elements + index;
+               }
+
                inline void ensure_space_for_one()
                {
                        if (m_size >= m_capacity) {
@@ -245,10 +269,14 @@ namespace BLI {
                void destruct_elements_but_keep_memory()
                {
                        for (uint i = 0; i < m_size; i++) {
-                               (m_elements + i)->~T();
+                               this->destruct_element(i);
                        }
                }
 
+               void destruct_element(uint index)
+               {
+                       this->element_ptr(index)->~T();
+               }
        };
 
 } /* namespace BLI */
\ No newline at end of file
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc 
b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 1e781cb4ae4..2311552e080 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -175,4 +175,20 @@ TEST(small_vector, Empty)
        EXPECT_FALSE(vec.empty());
        vec.remove_last();
        EXPECT_TRUE(vec.empty());
+}
+
+TEST(small_vector, RemoveReorder)
+{
+       IntVector vec = {4, 5, 6, 7};
+       vec.remove_and_reorder(1);
+       EXPECT_EQ(vec[0], 4);
+       EXPECT_EQ(vec[1], 7);
+       EXPECT_EQ(vec[2], 6);
+       vec.remove_and_reorder(2);
+       EXPECT_EQ(vec[0], 4);
+       EXPECT_EQ(vec[1], 7);
+       vec.remove_and_reorder(0);
+       EXPECT_EQ(vec[0], 7);
+       vec.remove_and_reorder(0);
+       EXPECT_TRUE(vec.empty());
 }
\ No newline at end of file

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

Reply via email to