[Bf-blender-cvs] [3d18bd5c516] master: BLI: define default hash function for const types

Jacques Lucke noreply at git.blender.org
Thu Jun 11 16:16:59 CEST 2020


Commit: 3d18bd5c5160b7ee8543558053cad9126804dbd6
Author: Jacques Lucke
Date:   Thu Jun 11 16:16:44 2020 +0200
Branches: master
https://developer.blender.org/rB3d18bd5c5160b7ee8543558053cad9126804dbd6

BLI: define default hash function for const types

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

M	source/blender/blenlib/BLI_hash.hh
M	tests/gtests/blenlib/BLI_map_test.cc

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

diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh
index 57d5f7f9d8a..de81abd5d7a 100644
--- a/source/blender/blenlib/BLI_hash.hh
+++ b/source/blender/blenlib/BLI_hash.hh
@@ -97,6 +97,16 @@ template<typename T> struct DefaultHash {
   }
 };
 
+/**
+ * Use the same hash function for const and non const variants of a type.
+ */
+template<typename T> struct DefaultHash<const T> {
+  uint32_t operator()(const T &value) const
+  {
+    return DefaultHash<T>{}(value);
+  }
+};
+
 #define TRIVIAL_DEFAULT_INT_HASH(TYPE) \
   template<> struct DefaultHash<TYPE> { \
     uint32_t operator()(TYPE value) const \
diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc
index 623055d5032..96e9879abe7 100644
--- a/tests/gtests/blenlib/BLI_map_test.cc
+++ b/tests/gtests/blenlib/BLI_map_test.cc
@@ -449,6 +449,15 @@ TEST(map, PointerKeys)
   EXPECT_TRUE(map.is_empty());
 }
 
+TEST(map, ConstKeysAndValues)
+{
+  Map<const std::string, const std::string> map;
+  map.reserve(10);
+  map.add("45", "643");
+  EXPECT_TRUE(map.contains("45"));
+  EXPECT_FALSE(map.contains("54"));
+}
+
 /**
  * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot.
  */



More information about the Bf-blender-cvs mailing list