Lagrange
hash.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <algorithm>
15#include <functional>
16#include <type_traits>
17#include <utility>
18
19namespace lagrange {
20
23
28template <class T>
29void hash_combine(size_t& seed, const T& v)
30{
31 std::hash<T> hasher;
32 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) +
33 (seed >> 2); // magic random number ensures spreading of hashes
34}
35
36namespace detail {
37
38template <typename U, typename V>
39size_t ordered_pair_hash_value(const U& u, const V& v)
40{
41 size_t h = std::hash<U>{}(u);
42 hash_combine(h, v);
43 return h;
44}
45
46} // namespace detail
47
52template <typename T, typename Enable = void>
54{
55 size_t operator()(const T& k) const { return detail::ordered_pair_hash_value(k[0], k[1]); }
56};
57
58template <typename U, typename V>
59struct OrderedPairHash<std::pair<U, V>>
60{
61 size_t operator()(const std::pair<U, V>& k) const
62 {
63 return detail::ordered_pair_hash_value(k.first, k.second);
64 }
65};
66
68
69} // namespace lagrange
void hash_combine(size_t &seed, const T &v)
Hash an object v and combine it with an existing hash value seed.
Definition: hash.h:29
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Compute an order-dependent hash of a pair of values.
Definition: hash.h:54