cimod
C++ library for a binary (and polynomial) quadratic model.
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1
// Copyright 2020-2025 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 <cstdint>
18
#include <iostream>
19
#include <utility>
20
#include <vector>
21
22
template
<
typename
T>
23
inline
void
hash_combine
( std::size_t&
seed
,
const
T
&
val
) {
24
std::hash<T>
hasher
;
25
seed
^=
hasher
(
val
) + 0x9e3779b9 + (
seed
<< 6 ) + (
seed
>> 2 );
26
}
27
28
template
<
class
...
TupleArgs
>
29
struct
std::hash<std::
tuple
<TupleArgs...>> {
30
private
:
31
// this is a termination condition
32
// N == sizeof...(TupleTypes)
33
//
34
template
<
size_t
Idx
,
typename
...
TupleTypes
>
35
inline
typename
std::enable_if<
Idx
==
sizeof
...(
TupleTypes
),
void
>
::type
36
hash_combine_tup
(
size_t
&
/*seed*/
,
const
std::tuple<TupleTypes...>&
/*tup*/
)
const
{
37
}
38
39
// this is the computation function
40
// continues till condition N < sizeof...(TupleTypes) holds
41
//
42
template
<
size_t
Idx
,
typename
...
TupleTypes
>
43
inline
typename
std::enable_if <
Idx
<
sizeof
...(
TupleTypes
),
void
>
::type
44
hash_combine_tup(
size_t
&
seed
,
const
std::tuple<TupleTypes...>&
tup
)
const
{
45
hash_combine
(
seed
, std::get<Idx>(
tup
) );
46
47
// on to next element
48
hash_combine_tup<Idx + 1>
(
seed
,
tup
);
49
}
50
51
public
:
52
size_t
operator()
(
const
std::tuple<TupleArgs...>&
tupleValue
)
const
{
53
size_t
seed
= 0;
54
// begin with the first iteration
55
hash_combine_tup<0>
(
seed
,
tupleValue
);
56
return
seed
;
57
}
58
};
59
60
namespace
cimod
{
65
struct
pair_hash
{
66
template
<
class
T1,
class
T2>
67
std::size_t
operator()
(
const
std::pair<T1, T2>&
p
)
const
{
68
std::size_t
lhs
= std::hash<T1>()(
p
.first ),
rhs
= std::hash<T2>()(
p
.second );
69
return
lhs
^ (
rhs
+ 0x9e3779b9 + (
lhs
<< 6 ) + (
lhs
>> 2 ) );
70
}
71
};
72
73
struct
vector_hash
{
74
75
template
<
class
T>
76
std::size_t
operator()
(
const
std::vector<T>&
V
)
const
{
77
std::size_t
hash
=
V
.size();
78
for
(
auto
&
i
:
V
) {
79
hash
^= std::hash<T>()(
i
) + 0x9e3779b9 + (
hash
<< 6 ) + (
hash
>> 2 );
80
}
81
return
hash
;
82
}
83
};
84
85
}
// namespace cimod
hash_combine
void hash_combine(std::size_t &seed, const T &val)
Definition
hash.hpp:23
declare_BQM
void declare_BQM(py::module &m, const std::string &name)
Definition
main.hpp:41
cimod
Definition
binary_polynomial_model.hpp:139
cimod::pair_hash
Hash function for std::unordered_map.
Definition
hash.hpp:65
cimod::pair_hash::operator()
std::size_t operator()(const std::pair< T1, T2 > &p) const
Definition
hash.hpp:67
cimod::vector_hash
Definition
hash.hpp:73
cimod::vector_hash::operator()
std::size_t operator()(const std::vector< T > &V) const
Definition
hash.hpp:76
std::hash< std::tuple< TupleArgs... > >::hash_combine_tup
std::enable_if< Idx==sizeof...(TupleTypes), void >::type hash_combine_tup(size_t &, const std::tuple< TupleTypes... > &) const
Definition
hash.hpp:36
std::hash< std::tuple< TupleArgs... > >::operator()
size_t operator()(const std::tuple< TupleArgs... > &tupleValue) const
Definition
hash.hpp:52
include
cimod
hash.hpp
Generated by
1.9.8