Easy3D 2.6.1
Loading...
Searching...
No Matches
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#include <easy3d/core/vec.h>
35
36
37namespace easy3d
38{
49 template<typename T>
50 void hash_combine(uint64_t &seed, T const& value) {
51 static std::hash<T> hasher;
52 uint64_t a = (hasher(value) ^ seed) * 0x9ddfea08eb382d69ULL;
53 a ^= (a >> 47);
54 uint64_t b = (seed ^ a) * 0x9ddfea08eb382d69ULL;
55 b ^= (b >> 47);
56 seed = b * 0x9ddfea08eb382d69ULL;
57 }
58
65 template <typename FT>
66 uint64_t hash(const Vec<2, FT>& value) {
67 uint64_t seed(0);
68 hash_combine(seed, value.x);
69 hash_combine(seed, value.y);
70 return seed;
71 }
72
79 template <typename FT>
80 uint64_t hash(const Vec<3, FT>& value) {
81 uint64_t seed(0);
82 hash_combine(seed, value.x);
83 hash_combine(seed, value.y);
84 hash_combine(seed, value.z);
85 return seed;
86 }
87
95 template <int DIM, typename FT>
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
110 template<typename Iterator>
111 uint64_t hash(Iterator first, Iterator last) {
112 uint64_t seed(0);
113 for (; first != last; ++first) {
114 hash_combine(seed, *first);
115 }
116 return seed;
117 }
118
126 template<typename Iterator>
127 void hash(uint64_t &seed, Iterator first, Iterator last) {
128 for (; first != last; ++first) {
129 hash_combine(seed, *first);
130 }
131 }
132
133} // namespace easy3d
134
135#endif // EASY3D_CORE_HASH_H
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Definition collider.cpp:182
uint64_t hash(const Vec< 2, FT > &value)
Computes the hash value of a 2D vector.
Definition hash.h:66
void hash_combine(uint64_t &seed, T const &value)
Combines a hash value with a seed value.
Definition hash.h:50