1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // template <class Key, class T, class Hash, class Pred, class Alloc>
13 // operator==(const unordered_map<Key, T, Hash, Pred, Alloc>& x,
14 // const unordered_map<Key, T, Hash, Pred, Alloc>& y);
16 // template <class Key, class T, class Hash, class Pred, class Alloc>
18 // operator!=(const unordered_map<Key, T, Hash, Pred, Alloc>& x,
19 // const unordered_map<Key, T, Hash, Pred, Alloc>& y);
21 // Implements paper: http://wg21.link/p0809
23 #include <unordered_map>
30 std::size_t hash_identity(T val
) {
34 std::size_t hash_neg(T val
) {
35 return std::numeric_limits
<T
>::max() - val
;
38 std::size_t hash_scale(T val
) {
39 return static_cast<std::size_t>(val
<< 1);
42 std::size_t hash_even(T val
) {
43 return val
& 1 ? 1 : 0;
46 std::size_t hash_same(T
/*val*/) {
51 std::size_t hash_identity(T
* val
) {
55 std::size_t hash_neg(T
* val
) {
56 return std::numeric_limits
<T
>::max() - *val
;
59 std::size_t hash_scale(T
* val
) {
60 return static_cast<std::size_t>(*val
<< 1);
63 std::size_t hash_even(T
* val
) {
64 return *val
& 1 ? 1 : 0;
67 template <class Map
, class Ittr
>
68 void populate(Map
& m
, Ittr start
, Ittr end
) {
69 for (auto *p1
= start
, *p2
= end
- 1; p1
!= end
; ++p1
, --p2
) {
70 m
.insert(std::make_pair(*p1
, *p2
));
74 template <class T
, std::size_t N
>
75 void test(T (&vals
)[N
]) {
76 using Hash
= std::size_t (*)(T
);
77 using C
= std::unordered_map
<T
, T
, Hash
, std::equal_to
<T
> >;
79 C
c1(0, hash_identity
);
85 populate(c1
, std::begin(vals
), std::end(vals
));
86 populate(c2
, std::begin(vals
), std::end(vals
));
87 populate(c3
, std::begin(vals
), std::end(vals
));
88 populate(c4
, std::begin(vals
), std::end(vals
));
89 populate(c5
, std::begin(vals
), std::end(vals
));
122 int main(int, char**) {
124 std::size_t vals
[] = {
133 8, 8, 8, 8, 8, 8, 8, 8,
134 9, 9, 9, 9, 9, 9, 9, 9, 9,
142 bool vals
[] = {true, false};
147 char* vals
[] = {(char*)("a"), (char*)("b"), (char*)("cde")};