[Bf-blender-cvs] [c52a4316d0c] functions: update the hashing template

Jacques Lucke noreply at git.blender.org
Sun Aug 25 14:34:49 CEST 2019


Commit: c52a4316d0c1f59ff9e3c558a8465c5a857e0a20
Author: Jacques Lucke
Date:   Sun Aug 25 13:58:01 2019 +0200
Branches: functions
https://developer.blender.org/rBc52a4316d0c1f59ff9e3c558a8465c5a857e0a20

update the hashing template

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

M	source/blender/blenlib/BLI_hash.hpp
M	source/blender/blenlib/BLI_map.hpp
M	source/blender/blenlib/BLI_refcount.hpp
M	source/blender/blenlib/BLI_set.hpp
M	source/blender/blenlib/BLI_set_vector.hpp
M	source/blender/functions/core/data_graph.hpp
M	source/blender/functions/core/type.hpp
M	source/blender/functions/frontends/data_flow_nodes/mappings.hpp
M	source/blender/functions/functions/auto_vectorization.cpp

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

diff --git a/source/blender/blenlib/BLI_hash.hpp b/source/blender/blenlib/BLI_hash.hpp
index 8a36ff0be3c..e6fd5c3ad6f 100644
--- a/source/blender/blenlib/BLI_hash.hpp
+++ b/source/blender/blenlib/BLI_hash.hpp
@@ -1,16 +1,59 @@
 #pragma once
 
 #include <functional>
+#include <string>
+#include <utility>
 
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 namespace BLI {
 
-template<typename T> class MyHash {
- public:
-  uint32_t operator()(const T &value)
+template<typename T> struct DefaultHash {
+};
+
+#define TRIVIAL_DEFAULT_INT_HASH(TYPE) \
+  template<> struct DefaultHash<TYPE> { \
+    uint32_t operator()(TYPE value) const \
+    { \
+      return (uint32_t)value; \
+    } \
+  }
+
+TRIVIAL_DEFAULT_INT_HASH(int8_t);
+TRIVIAL_DEFAULT_INT_HASH(uint8_t);
+TRIVIAL_DEFAULT_INT_HASH(int16_t);
+TRIVIAL_DEFAULT_INT_HASH(uint16_t);
+TRIVIAL_DEFAULT_INT_HASH(int32_t);
+TRIVIAL_DEFAULT_INT_HASH(uint32_t);
+TRIVIAL_DEFAULT_INT_HASH(int64_t);
+
+template<> struct DefaultHash<std::string> {
+  uint32_t operator()(const std::string &value) const
+  {
+    uint32_t hash = 5381;
+    for (char c : value) {
+      hash = hash * 33 + c;
+    }
+    return hash;
+  }
+};
+
+template<typename T> struct DefaultHash<T *> {
+  uint32_t operator()(const T *value) const
+  {
+    uintptr_t ptr = POINTER_AS_UINT(value);
+    uint32_t hash = (uint32_t)(ptr >> 3);
+    return hash;
+  }
+};
+
+template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> {
+  uint32_t operator()(const std::pair<T1, T2> &value) const
   {
-    return (uint32_t)std::hash<T>{}(value);
+    uint32_t hash1 = DefaultHash<T1>{}(value.first);
+    uint32_t hash2 = DefaultHash<T2>{}(value.second);
+    return hash1 ^ (hash2 * 33);
   }
 };
 
diff --git a/source/blender/blenlib/BLI_map.hpp b/source/blender/blenlib/BLI_map.hpp
index 16130e2fc4b..02e62401ba4 100644
--- a/source/blender/blenlib/BLI_map.hpp
+++ b/source/blender/blenlib/BLI_map.hpp
@@ -34,7 +34,7 @@ namespace BLI {
 // clang-format off
 
 #define ITER_SLOTS_BEGIN(KEY, ARRAY, OPTIONAL_CONST, R_ITEM, R_OFFSET) \
-  uint32_t hash = MyHash<KeyT>{}(KEY); \
+  uint32_t hash = DefaultHash<KeyT>{}(KEY); \
   uint32_t perturb = hash; \
   while (true) { \
     uint32_t item_index = (hash & ARRAY.slot_mask()) >> OFFSET_SHIFT; \
diff --git a/source/blender/blenlib/BLI_refcount.hpp b/source/blender/blenlib/BLI_refcount.hpp
index 69c63de76e5..b970bbcbb63 100644
--- a/source/blender/blenlib/BLI_refcount.hpp
+++ b/source/blender/blenlib/BLI_refcount.hpp
@@ -33,6 +33,7 @@
 #include <atomic>
 #include <utility>
 #include "BLI_utildefines.h"
+#include "BLI_hash.hpp"
 
 namespace BLI {
 
@@ -231,16 +232,11 @@ template<typename T> class AutoRefCount {
   }
 };
 
-} /* namespace BLI */
-
-namespace std {
-template<typename T> struct hash<BLI::AutoRefCount<T>> {
-  typedef BLI::AutoRefCount<T> argument_type;
-  typedef size_t result_type;
-
-  result_type operator()(argument_type const &v) const noexcept
+template<typename T> struct DefaultHash<AutoRefCount<T>> {
+  uint32_t operator()(const AutoRefCount<T> &value) const
   {
-    return std::hash<T>{}(*v.ptr());
+    return DefaultHash<T>{}(value.ref());
   }
 };
-}  // namespace std
+
+} /* namespace BLI */
diff --git a/source/blender/blenlib/BLI_set.hpp b/source/blender/blenlib/BLI_set.hpp
index 2b76d1ed23c..df952fb573e 100644
--- a/source/blender/blenlib/BLI_set.hpp
+++ b/source/blender/blenlib/BLI_set.hpp
@@ -31,7 +31,7 @@ namespace BLI {
 // clang-format off
 
 #define ITER_SLOTS_BEGIN(VALUE, ARRAY, OPTIONAL_CONST, R_ITEM, R_OFFSET) \
-  uint32_t hash = MyHash<T>{}(VALUE); \
+  uint32_t hash = DefaultHash<T>{}(VALUE); \
   uint32_t perturb = hash; \
   while (true) { \
     uint32_t item_index = (hash & ARRAY.slot_mask()) >> OFFSET_SHIFT; \
diff --git a/source/blender/blenlib/BLI_set_vector.hpp b/source/blender/blenlib/BLI_set_vector.hpp
index a78efe0e676..88b0d23604f 100644
--- a/source/blender/blenlib/BLI_set_vector.hpp
+++ b/source/blender/blenlib/BLI_set_vector.hpp
@@ -29,7 +29,7 @@ namespace BLI {
 // clang-format off
 
 #define ITER_SLOTS_BEGIN(VALUE, ARRAY, OPTIONAL_CONST, R_SLOT) \
-  uint32_t hash = MyHash<T>{}(VALUE); \
+  uint32_t hash = DefaultHash<T>{}(VALUE); \
   uint32_t perturb = hash; \
   while (true) { \
     for (uint i = 0; i < 4; i++) {\
diff --git a/source/blender/functions/core/data_graph.hpp b/source/blender/functions/core/data_graph.hpp
index 6c06c4b0063..0fa196e5284 100644
--- a/source/blender/functions/core/data_graph.hpp
+++ b/source/blender/functions/core/data_graph.hpp
@@ -453,14 +453,11 @@ class DataGraph : public RefCounter {
 
 }  // namespace FN
 
-namespace std {
-template<> struct hash<FN::DataSocket> {
-  typedef FN::DataSocket argument_type;
-  typedef size_t result_type;
-
-  result_type operator()(argument_type const &v) const noexcept
+namespace BLI {
+template<> struct DefaultHash<FN::DataSocket> {
+  uint32_t operator()(FN::DataSocket value) const
   {
-    return v.id() + (int)v.is_input() * 12345;
+    return value.id() + (int)value.is_input() * 12345;
   }
 };
-}  // namespace std
+}  // namespace BLI
diff --git a/source/blender/functions/core/type.hpp b/source/blender/functions/core/type.hpp
index c0f84e14aa1..c03b5fd6006 100644
--- a/source/blender/functions/core/type.hpp
+++ b/source/blender/functions/core/type.hpp
@@ -154,17 +154,12 @@ inline Type *TypeExtension::owner() const
 
 } /* namespace FN */
 
-/* Make Type hashable using std::hash.
- ****************************************/
-
-namespace std {
-template<> struct hash<FN::Type> {
-  typedef FN::Type argument_type;
-  typedef size_t result_type;
-
-  result_type operator()(argument_type const &v) const noexcept
+namespace BLI {
+template<> struct DefaultHash<FN::Type> {
+  uint32_t operator()(const FN::Type &value) const noexcept
   {
-    return std::hash<void *>{}((void *)&v);
+    void *ptr = (void *)&value;
+    return DefaultHash<void *>{}(ptr);
   }
 };
-}  // namespace std
+}  // namespace BLI
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings.hpp b/source/blender/functions/frontends/data_flow_nodes/mappings.hpp
index c57d4715e3f..2bf5433265d 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings.hpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings.hpp
@@ -112,17 +112,3 @@ std::unique_ptr<LinkInserters> &MAPPING_link_inserters(void);
 
 }  // namespace DataFlowNodes
 }  // namespace FN
-
-namespace std {
-template<> struct hash<FN::DataFlowNodes::StringPair> {
-  typedef FN::DataFlowNodes::StringPair argument_type;
-  typedef size_t result_type;
-
-  result_type operator()(argument_type const &v) const noexcept
-  {
-    size_t h1 = std::hash<std::string>{}(v.first);
-    size_t h2 = std::hash<std::string>{}(v.second);
-    return h1 ^ h2;
-  }
-};
-}  // namespace std
diff --git a/source/blender/functions/functions/auto_vectorization.cpp b/source/blender/functions/functions/auto_vectorization.cpp
index 4352639f275..454f4dfc57e 100644
--- a/source/blender/functions/functions/auto_vectorization.cpp
+++ b/source/blender/functions/functions/auto_vectorization.cpp
@@ -516,15 +516,13 @@ SharedFunction to_vectorized_function__with_cache(
 }  // namespace Functions
 }  // namespace FN
 
-namespace std {
-template<> struct hash<FN::Functions::AutoVectorizationInput> {
-  typedef FN::Functions::AutoVectorizationInput argument_type;
-  typedef size_t result_type;
-
-  result_type operator()(argument_type const &v) const noexcept
+namespace BLI {
+template<> struct DefaultHash<FN::Functions::AutoVectorizationInput> {
+  uint32_t operator()(const FN::Functions::AutoVectorizationInput &value) const
   {
     /* TODO: take other struct fields into account. */
-    return BLI_ghashutil_ptrhash(v.m_original_fn.ptr());
+    void *ptr = (void *)value.m_original_fn.ptr();
+    return DefaultHash<void *>{}(ptr);
   }
 };
-}  // namespace std
+}  // namespace BLI



More information about the Bf-blender-cvs mailing list