summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvcore/Hash.h
diff options
context:
space:
mode:
authorHein-Pieter van Braam <hp@tmm.cx>2017-12-08 15:05:47 +0100
committerHein-Pieter van Braam <hp@tmm.cx>2017-12-08 15:47:15 +0100
commitbf05309af734431c3b3cf869a63ed477439a6739 (patch)
tree72c1c939f9035c711f50ec94b0270ea60e0bb4e4 /thirdparty/thekla_atlas/nvcore/Hash.h
parentb3b4727dff009dda0a65b8a013ec04d52a54b367 (diff)
Import thekla_atlas
As requested by reduz, an import of thekla_atlas into thirdparty/
Diffstat (limited to 'thirdparty/thekla_atlas/nvcore/Hash.h')
-rw-r--r--thirdparty/thekla_atlas/nvcore/Hash.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/thirdparty/thekla_atlas/nvcore/Hash.h b/thirdparty/thekla_atlas/nvcore/Hash.h
new file mode 100644
index 0000000000..a8b0b2c63b
--- /dev/null
+++ b/thirdparty/thekla_atlas/nvcore/Hash.h
@@ -0,0 +1,83 @@
+// This code is in the public domain -- Ignacio Castaņo <castano@gmail.com>
+
+#pragma once
+#ifndef NV_CORE_HASH_H
+#define NV_CORE_HASH_H
+
+#include "nvcore.h"
+
+namespace nv
+{
+ inline uint sdbmHash(const void * data_in, uint size, uint h = 5381)
+ {
+ const uint8 * data = (const uint8 *) data_in;
+ uint i = 0;
+ while (i < size) {
+ h = (h << 16) + (h << 6) - h + (uint) data[i++];
+ }
+ return h;
+ }
+
+ // Note that this hash does not handle NaN properly.
+ inline uint sdbmFloatHash(const float * f, uint count, uint h = 5381)
+ {
+ for (uint i = 0; i < count; i++) {
+ //nvDebugCheck(nv::isFinite(*f));
+ union { float f; uint32 i; } x = { f[i] };
+ if (x.i == 0x80000000) x.i = 0;
+ h = sdbmHash(&x, 4, h);
+ }
+ return h;
+ }
+
+
+ template <typename T>
+ inline uint hash(const T & t, uint h = 5381)
+ {
+ return sdbmHash(&t, sizeof(T), h);
+ }
+
+ template <>
+ inline uint hash(const float & f, uint h)
+ {
+ return sdbmFloatHash(&f, 1, h);
+ }
+
+
+ // Functors for hash table:
+ template <typename Key> struct Hash
+ {
+ uint operator()(const Key & k) const {
+ return hash(k);
+ }
+ };
+
+ template <typename Key> struct Equal
+ {
+ bool operator()(const Key & k0, const Key & k1) const {
+ return k0 == k1;
+ }
+ };
+
+
+ // @@ Move to Utils.h?
+ template <typename T1, typename T2>
+ struct Pair {
+ T1 first;
+ T2 second;
+ };
+
+ template <typename T1, typename T2>
+ bool operator==(const Pair<T1,T2> & p0, const Pair<T1,T2> & p1) {
+ return p0.first == p1.first && p0.second == p1.second;
+ }
+
+ template <typename T1, typename T2>
+ uint hash(const Pair<T1,T2> & p, uint h = 5381) {
+ return hash(p.second, hash(p.first));
+ }
+
+
+} // nv namespace
+
+#endif // NV_CORE_HASH_H