openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
pairhash.hpp
Go to the documentation of this file.
1// Copyright 2023 Jij Inc.
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstddef>
18#include <unordered_map>
19#include <utility>
20
22#include "./index_type.hpp"
23
24namespace openjij {
25namespace utility {
26
30struct PairHash {
31
39 template <class T1, class T2>
40 inline size_t operator()(const std::pair<T1, T2> &p) const {
41 size_t lhs = std::hash<T1>()(p.first);
42 size_t rhs = std::hash<T2>()(p.second);
43 return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
44 }
45};
46
47
49struct IndexHash {
50 template<class... Types>
51 std::size_t operator()(const std::variant<Types...> &v) const {
52 if (std::holds_alternative<std::int32_t>(v)) {
53 return std::hash<std::int32_t>()(std::get<std::int32_t>(v));
54 }
55 else if (std::holds_alternative<std::string>(v)) {
56 return std::hash<std::string>()(std::get<std::string>(v));
57 }
58 else if (std::holds_alternative<AnyTupleType>(v)) {
59 const auto &variant_vec = std::get<AnyTupleType>(v);
60 std::size_t hash = variant_vec.size();
61 for (const auto &i : variant_vec) {
62 if (std::holds_alternative<std::int32_t>(i)) {
63 hash ^= std::hash<std::int32_t>()(std::get<std::int32_t>(i)) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
64 }
65 else if (std::holds_alternative<std::string>(i)) {
66 hash ^= std::hash<std::string>()(std::get<std::string>(i)) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
67 }
68 else {
69 throw std::runtime_error("Invalid template parameters");
70 }
71 }
72 return hash;
73 }
74 else {
75 throw std::runtime_error("Invalid template parameters");
76 }
77 }
78};
79
82 std::size_t operator()(const std::vector<IndexType> &v) const {
83 std::size_t hash = v.size();
84 for (const auto &i : v) {
85 hash ^= IndexHash()(i) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
86 }
87 return hash;
88 }
89};
90
92struct VectorHash {
93 template<class T>
94 std::size_t operator()(const std::vector<T> &v) const {
95 std::size_t hash = v.size();
96 for (const auto &i : v) {
97 hash ^= std::hash<T>()(i) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
98 }
99 return hash;
100 }
101};
102
105 std::size_t operator()(const std::pair<IndexType, IndexType> &p) const {
106 std::size_t lhs = IndexHash()(p.first);
107 std::size_t rhs = IndexHash()(p.second);
108 return lhs^(rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
109 }
110};
111
112
113} // namespace utility
114} // namespace openjij
Definition algorithm.hpp:24
Hash struct of IndexType.
Definition pairhash.hpp:49
std::size_t operator()(const std::variant< Types... > &v) const
Definition pairhash.hpp:51
Hash struct of std::pair<IndexType>.
Definition pairhash.hpp:104
std::size_t operator()(const std::pair< IndexType, IndexType > &p) const
Definition pairhash.hpp:105
Hash struct of std::vector<AnyIndexType>.
Definition pairhash.hpp:81
std::size_t operator()(const std::vector< IndexType > &v) const
Definition pairhash.hpp:82
hash class for std::pair
Definition pairhash.hpp:30
size_t operator()(const std::pair< T1, T2 > &p) const
generate hash for std::pair
Definition pairhash.hpp:40
Hash struct of std::vector<T>.
Definition pairhash.hpp:92
std::size_t operator()(const std::vector< T > &v) const
Definition pairhash.hpp:94