[Bf-blender-cvs] [4d572be4e6d] functions: small set vector
Jacques Lucke
noreply at git.blender.org
Sun Feb 10 20:26:03 CET 2019
Commit: 4d572be4e6dbfd24d7b6f16269a41cd1f18a3708
Author: Jacques Lucke
Date: Tue Feb 5 13:56:45 2019 +0100
Branches: functions
https://developer.blender.org/rB4d572be4e6dbfd24d7b6f16269a41cd1f18a3708
small set vector
===================================================================
M source/blender/blenlib/BLI_small_set.hpp
A source/blender/blenlib/BLI_small_set_vector.hpp
M source/blender/blenlib/CMakeLists.txt
A tests/gtests/blenlib/BLI_small_set_vector_test.cc
M tests/gtests/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_small_set.hpp b/source/blender/blenlib/BLI_small_set.hpp
index b385b56db7f..af024f9fcd5 100644
--- a/source/blender/blenlib/BLI_small_set.hpp
+++ b/source/blender/blenlib/BLI_small_set.hpp
@@ -6,13 +6,20 @@ namespace BLI {
template<typename T, uint N = 4>
class SmallSet {
- private:
+ protected:
SmallVector<T> m_entries;
public:
SmallSet() = default;
- SmallSet(const SmallVector<T> values)
+ SmallSet(const std::initializer_list<T> &values)
+ {
+ for (T value : values) {
+ this->add(value);
+ }
+ }
+
+ SmallSet(const SmallVector<T> &values)
{
for (T value : values) {
this->add(value);
diff --git a/source/blender/blenlib/BLI_small_set_vector.hpp b/source/blender/blenlib/BLI_small_set_vector.hpp
new file mode 100644
index 00000000000..d3992915c84
--- /dev/null
+++ b/source/blender/blenlib/BLI_small_set_vector.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "BLI_small_set.hpp"
+
+namespace BLI {
+
+ template<typename T>
+ class SmallSetVector : public SmallSet<T> {
+ public:
+ SmallSetVector()
+ : SmallSet<T>() {}
+
+ SmallSetVector(const std::initializer_list<T> &values)
+ : SmallSet<T>(values) {}
+
+ int index(const T &value) const
+ {
+ for (uint i = 0; i < this->size(); i++) {
+ if (this->m_entries[i] == value) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ T operator[](const int index) const
+ {
+ BLI_assert(index >= 0 && index < this->size());
+ return this->m_entries[index];
+ }
+ };
+
+};
\ No newline at end of file
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index c9530e4736c..d41e0766728 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -228,6 +228,7 @@ set(SRC
BLI_small_vector.hpp
BLI_small_map.hpp
BLI_small_set.hpp
+ BLI_small_set_vector.hpp
)
if(WITH_MEM_VALGRIND)
diff --git a/tests/gtests/blenlib/BLI_small_set_vector_test.cc b/tests/gtests/blenlib/BLI_small_set_vector_test.cc
new file mode 100644
index 00000000000..81fc533e5ec
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_small_set_vector_test.cc
@@ -0,0 +1,61 @@
+#include "testing/testing.h"
+#include "BLI_small_set_vector.hpp"
+
+using IntSetVector = BLI::SmallSetVector<int>;
+
+TEST(small_set_vector, DefaultConstructor)
+{
+ IntSetVector set;
+ EXPECT_EQ(set.size(), 0);
+}
+
+TEST(small_set_vector, InitializerListConstructor_WithoutDuplicates)
+{
+ IntSetVector set = {1, 4, 5};
+ EXPECT_EQ(set.size(), 3);
+ EXPECT_EQ(set[0], 1);
+ EXPECT_EQ(set[1], 4);
+ EXPECT_EQ(set[2], 5);
+}
+
+TEST(small_set_vector, InitializerListConstructor_WithDuplicates)
+{
+ IntSetVector set = {1, 3, 3, 2, 1, 5};
+ EXPECT_EQ(set.size(), 4);
+ EXPECT_EQ(set[0], 1);
+ EXPECT_EQ(set[1], 3);
+ EXPECT_EQ(set[2], 2);
+ EXPECT_EQ(set[3], 5);
+}
+
+TEST(small_set_vector, AddNewIncreasesSize)
+{
+ IntSetVector set;
+ EXPECT_EQ(set.size(), 0);
+ set.add(5);
+ EXPECT_EQ(set.size(), 1);
+}
+
+TEST(small_set_vector, AddExistingDoesNotIncreaseSize)
+{
+ IntSetVector set;
+ EXPECT_EQ(set.size(), 0);
+ set.add(5);
+ EXPECT_EQ(set.size(), 1);
+ set.add(5);
+ EXPECT_EQ(set.size(), 1);
+}
+
+TEST(small_set_vector, IndexOfExisting)
+{
+ IntSetVector set = {3, 6, 4};
+ EXPECT_EQ(set.index(6), 1);
+ EXPECT_EQ(set.index(3), 0);
+ EXPECT_EQ(set.index(4), 2);
+}
+
+TEST(small_set_vector, IndexOfNotExisting)
+{
+ IntSetVector set = {3, 6, 4};
+ EXPECT_EQ(set.index(5), -1);
+}
\ No newline at end of file
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index f3b099e86de..d7097399dac 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -56,6 +56,7 @@ BLENDER_TEST(BLI_memiter "bf_blenlib")
BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")
BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")
BLENDER_TEST(BLI_small_vector "bf_blenlib")
+BLENDER_TEST(BLI_small_set_vector "bf_blenlib")
BLENDER_TEST(BLI_small_map "bf_blenlib")
BLENDER_TEST(BLI_stack "bf_blenlib")
BLENDER_TEST(BLI_string "bf_blenlib")
More information about the Bf-blender-cvs
mailing list