From a8ff8b64dc4d4bea9ec620920639440496dfcba0 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 8 Jul 2020 14:40:34 +0200 Subject: BLI: add comparison operators and hash functions for float3, etc. --- source/blender/blenlib/BLI_color.hh | 29 +++++++++++++++++++++++++++++ source/blender/blenlib/BLI_float2.hh | 10 ++++++++++ source/blender/blenlib/BLI_float3.hh | 18 ++++++++++++++++++ source/blender/blenlib/BLI_hash.hh | 7 +++++++ 4 files changed, 64 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_color.hh b/source/blender/blenlib/BLI_color.hh index 37f74edcf4c..1215affe837 100644 --- a/source/blender/blenlib/BLI_color.hh +++ b/source/blender/blenlib/BLI_color.hh @@ -51,6 +51,25 @@ struct Color4f { stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; return stream; } + + friend bool operator==(const Color4f &a, const Color4f &b) + { + return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; + } + + friend bool operator!=(const Color4f &a, const Color4f &b) + { + return !(a == b); + } + + uint32_t hash() const + { + uint32_t x1 = *(uint32_t *)&r; + uint32_t x2 = *(uint32_t *)&g; + uint32_t x3 = *(uint32_t *)&b; + uint32_t x4 = *(uint32_t *)&a; + return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319); + } }; struct Color4b { @@ -89,6 +108,16 @@ struct Color4b { stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; return stream; } + + friend bool operator==(const Color4b &a, const Color4b &b) + { + return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; + } + + friend bool operator!=(const Color4b &a, const Color4b &b) + { + return !(a == b); + } }; } // namespace blender diff --git a/source/blender/blenlib/BLI_float2.hh b/source/blender/blenlib/BLI_float2.hh index 94da5d18ad2..7bf928e7db5 100644 --- a/source/blender/blenlib/BLI_float2.hh +++ b/source/blender/blenlib/BLI_float2.hh @@ -79,6 +79,16 @@ struct float2 { stream << "(" << v.x << ", " << v.y << ")"; return stream; } + + friend bool operator==(const float2 &a, const float2 &b) + { + return a.x == b.x && a.y == b.y; + } + + friend bool operator!=(const float2 &a, const float2 &b) + { + return !(a == b); + } }; } // namespace blender diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh index 0edee600ef6..a36cedad41d 100644 --- a/source/blender/blenlib/BLI_float3.hh +++ b/source/blender/blenlib/BLI_float3.hh @@ -128,6 +128,16 @@ struct float3 { return stream; } + friend bool operator==(const float3 &a, const float3 &b) + { + return a.x == b.x && a.y == b.y && a.z == b.z; + } + + friend bool operator!=(const float3 &a, const float3 &b) + { + return !(a == b); + } + float normalize_and_get_length() { return normalize_v3(*this); @@ -178,6 +188,14 @@ struct float3 { z = -z; } + uint32_t hash() const + { + uint32_t x1 = *(uint32_t *)&x; + uint32_t x2 = *(uint32_t *)&y; + uint32_t x3 = *(uint32_t *)&z; + return (x1 * 435109) ^ (x2 * 380867) ^ (x3 * 1059217); + } + static float dot(const float3 &a, const float3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh index 49e619ff1bc..5cd4ce3c1a9 100644 --- a/source/blender/blenlib/BLI_hash.hh +++ b/source/blender/blenlib/BLI_hash.hh @@ -154,6 +154,13 @@ template<> struct DefaultHash { } }; +template<> struct DefaultHash { + uint32_t operator()(bool value) const + { + return (uint32_t)(value != false) * 1298191; + } +}; + inline uint32_t hash_string(StringRef str) { uint32_t hash = 5381; -- cgit v1.2.3