YSTest: hash.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

hash.hpp
浏览该文件的文档.
1 /*
2 © 2015 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_hash_hpp_
29 #define YB_INC_ystdex_hash_hpp_ 1
30
31 #include "tuple.hpp" // for ../ydef.h, std::tuple, index_sequence, std::get,
32 // std::declval, index_sequence_for, std::pair;
33 #include <functional> // for std::hash;
34 #include <numeric> // for std::accumulate;
35
36 namespace ystdex
37 {
38
51 template<typename _type>
52 inline void
53 hash_combine(size_t& seed, const _type& val)
54 ynoexcept_spec(std::hash<_type>()(val))
55 {
56 seed ^= std::hash<_type>()(val) + 0x9E3779B9 + (seed << 6) + (seed >> 2);
57 }
58
65 template<typename _type>
66 yconstfn size_t
67 hash_combine_seq(size_t seed, const _type& val)
68 ynoexcept_spec(std::hash<_type>()(val))
69 {
70 return ystdex::hash_combine(seed, val), seed;
71 }
72 template<typename _type, typename... _tParams>
73 yconstfn size_t
74 hash_combine_seq(size_t seed, const _type& val, const _tParams&... args)
75 ynoexcept_spec(std::hash<_type>()(val))
76 {
77 return
78 ystdex::hash_combine_seq(ystdex::hash_combine_seq(seed, val), args...);
79 }
81
87 template<typename _tIn>
89 inline size_t
90 hash_range(size_t seed, _tIn first, _tIn last)
91 {
92 return std::accumulate(first, last, seed,
93 [](size_t s, decltype(*first) val){
94 hash_combine(s, val);
95 return s;
96 });
97 }
98 template<typename _tIn>
99 inline size_t
100 hash_range(_tIn first, _tIn last)
101 {
102 return ystdex::hash_range(0, first, last);
103 }
105
106
107 namespace details
108 {
109
111
112 template<class, class>
113 struct combined_hash_tuple;
114
115 template<typename _type, size_t... _vSeq>
116 struct combined_hash_tuple<_type, index_sequence<_vSeq...>>
117 {
118 static yconstfn size_t
119 call(const _type& tp)
120 ynoexcept_spec(ystdex::hash_combine_seq(0, std::get<_vSeq>(tp)...))
121 {
122 return ystdex::hash_combine_seq(0, std::get<_vSeq>(tp)...);
123 }
124 };
126
127 } // namespace details;
128
134 template<typename...>
135 struct combined_hash;
136
137 template<typename _type>
138 struct combined_hash<_type> : std::hash<_type>
139 {};
140
141 template<typename... _types>
142 struct combined_hash<std::tuple<_types...>>
143 {
144 using type = std::tuple<_types...>;
145
147 yconstfn size_t
148 operator()(const type& tp) const ynoexcept_spec(
149 ystdex::hash_combine_seq(0, std::declval<const _types&>()...))
150 {
151 return details::combined_hash_tuple<type,
152 index_sequence_for<_types...>>::call(tp);
153 }
154 };
155
156 template<typename _type1, typename _type2>
157 struct combined_hash<std::pair<_type1, _type2>>
158 : combined_hash<std::tuple<_type1, _type2>>
159 {};
161
162
163 } // namespace ystdex;
164
165 #endif
166
ystdex::combined_hash< std::tuple< _types... > >::type
std::tuple< _types... > type
Definition: hash.hpp:144
ystdex::hash_combine
void hash_combine(size_t &seed, const _type &val)
重复计算散列。
Definition: hash.hpp:53
ystdex::cpp2014::index_sequence_for
make_index_sequence< sizeof...(_types)> index_sequence_for
Definition: integer_sequence.hpp:306
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::combined_hash< _type >
Definition: hash.hpp:138
ystdex::cpp2014::index_sequence
integer_sequence< size_t, _vSeq... > index_sequence
Definition: integer_sequence.hpp:63
ystdex::details::combined_hash_tuple
Definition: hash.hpp:113
tuple.hpp
元组类型和操作。
ynoexcept_spec
#define ynoexcept_spec(...)
表达式 noexcept 异常规范。
Definition: ydef.h:794
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
ystdex::details::combined_hash_tuple< _type, index_sequence< _vSeq... > >::call
static size_t call(const _type &tp)
Definition: hash.hpp:119
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
std::hash< _type >
ystdex::combined_hash
使用 std::hash 和 ystdex::hash_combine_seq 实现的对特定类型的散列。
Definition: hash.hpp:135
ystdex::combined_hash< std::tuple< _types... > >::operator()
size_t operator()(const type &tp) const
Definition: hash.hpp:148
std
Definition: memory.hpp:1183
ystdex::hash_combine_seq
size_t hash_combine_seq(size_t seed, const _type &val)
重复计算序列散列。
Definition: hash.hpp:67
ystdex::hash_range
size_t hash_range(size_t seed, _tIn first, _tIn last)
重复对范围计算散列。
Definition: hash.hpp:90