Easy3D 2.5.3
hash.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_HASH_H
28#define EASY3D_CORE_HASH_H
29
30
31#include <cstdint>
32#include <functional>
33
34
35namespace easy3d
36{
37 // The hash function in Google Optimization Tools
38 // https://github.com/google/or-tools/blob/stable/ortools/base/hash.h
39
40#if 0
50 template<typename T>
51 inline void hash_combine(uint64_t &seed, T const& value) {
52 static std::hash<T> hasher;
53 seed ^= hasher(value) + 0x9e3779b9 + (seed<<6) + (seed>>2);
54 }
55
56#else
63 template<typename T>
64 inline void hash_combine(uint64_t &seed, T const& value) {
65 static std::hash<T> hasher;
66 uint64_t a = (hasher(value) ^ seed) * 0x9ddfea08eb382d69ULL;
67 a ^= (a >> 47);
68 uint64_t b = (seed ^ a) * 0x9ddfea08eb382d69ULL;
69 b ^= (b >> 47);
70 seed = b * 0x9ddfea08eb382d69ULL;
71 }
72#endif
73
75 template <typename FT>
76 uint64_t hash(const Vec<2, FT>& value) {
77 uint64_t seed(0);
78 hash_combine(seed, value.x);
79 hash_combine(seed, value.y);
80 return seed;
81 }
82
84 template <typename FT>
85 uint64_t hash(const Vec<3, FT>& value) {
86 uint64_t seed(0);
87 hash_combine(seed, value.x);
88 hash_combine(seed, value.y);
89 hash_combine(seed, value.z);
90 return seed;
91 }
92
93
95 template <int DIM, typename FT> inline
96 uint64_t hash(const Vec<DIM, FT>& value) {
97 uint64_t seed(0);
98 for (int i=0; i<DIM; ++i)
99 hash_combine(seed, value[i]);
100 return seed;
101 }
102
103
105 template<typename Iterator>
106 inline uint64_t hash(Iterator first, Iterator last) {
107 uint64_t seed(0);
108 for (; first != last; ++first) {
109 hash_combine(seed, *first);
110 }
111 return seed;
112 }
113
114
116 template<typename Iterator>
117 inline void hash(uint64_t &seed, Iterator first, Iterator last) {
118 for (; first != last; ++first) {
119 hash_combine(seed, *first);
120 }
121 }
122
123} // namespace easy3d
124
125#endif // EASY3D_CORE_HASH_H
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
Definition: collider.cpp:182
uint64_t hash(const Vec< 2, FT > &value)
Computes the hash value of a 2D vector.
Definition: hash.h:76
void hash_combine(uint64_t &seed, T const &value)
std::size_t has 64 bits on most systems, but 32 bits on 32-bit Windows. To make the same code robustl...
Definition: hash.h:64