140 lines · cpp
1//===----------------------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9// <unordered_set>10 11// template <class Key, class T, class Hash, class Pred, class Alloc>12// bool13// operator==(const unordered_set<Key, T, Hash, Pred, Alloc>& x,14// const unordered_set<Key, T, Hash, Pred, Alloc>& y);15//16// template <class Key, class T, class Hash, class Pred, class Alloc>17// bool18// operator!=(const unordered_set<Key, T, Hash, Pred, Alloc>& x,19// const unordered_set<Key, T, Hash, Pred, Alloc>& y);20 21// Implements paper: http://wg21.link/p080922 23#include <unordered_set>24#include <cstddef>25#include <limits>26#include <cassert>27#include <functional>28 29template <class T>30std::size_t hash_identity(T val) {31 return val;32}33template <class T>34std::size_t hash_neg(T val) {35 return std::numeric_limits<T>::max() - val;36}37template <class T>38std::size_t hash_scale(T val) {39 return static_cast<std::size_t>(val << 1);40}41template <class T>42std::size_t hash_even(T val) {43 return val & 1 ? 1 : 0;44}45template <class T>46std::size_t hash_same(T /*val*/) {47 return 1;48}49 50template <class T>51std::size_t hash_identity(T* val) {52 return *val;53}54template <class T>55std::size_t hash_neg(T* val) {56 return std::numeric_limits<T>::max() - *val;57}58template <class T>59std::size_t hash_scale(T* val) {60 return static_cast<std::size_t>(*val << 1);61}62template <class T>63std::size_t hash_even(T* val) {64 return *val & 1 ? 1 : 0;65}66 67template <class T, std::size_t N>68void test(T (&vals)[N]) {69 using Hash = std::size_t (*)(T);70 using C = std::unordered_set<T, Hash, std::equal_to<T> >;71 72 C c1(std::begin(vals), std::end(vals), 0, hash_identity);73 C c2(std::begin(vals), std::end(vals), 0, hash_neg);74 C c3(std::begin(vals), std::end(vals), 0, hash_scale);75 C c4(std::begin(vals), std::end(vals), 0, hash_even);76 C c5(std::begin(vals), std::end(vals), 0, hash_same);77 78 assert(c1 == c1);79 assert(c1 == c2);80 assert(c1 == c3);81 assert(c1 == c4);82 assert(c1 == c5);83 84 assert(c2 == c1);85 assert(c2 == c2);86 assert(c2 == c3);87 assert(c2 == c4);88 assert(c2 == c5);89 90 assert(c3 == c1);91 assert(c3 == c2);92 assert(c3 == c3);93 assert(c3 == c4);94 assert(c3 == c5);95 96 assert(c4 == c1);97 assert(c4 == c2);98 assert(c4 == c3);99 assert(c4 == c4);100 assert(c4 == c5);101 102 assert(c5 == c1);103 assert(c5 == c2);104 assert(c5 == c3);105 assert(c5 == c4);106 assert(c5 == c5);107}108 109int main(int, char**) {110 {111 std::size_t vals[] = {112 // clang-format off113 1,114 2, 2,115 3, 3, 3,116 4, 4, 4, 4,117 5, 5, 5, 5, 5,118 6, 6, 6, 6, 6, 6,119 7, 7, 7, 7, 7, 7, 7,120 8, 8, 8, 8, 8, 8, 8, 8,121 9, 9, 9, 9, 9, 9, 9, 9, 9,122 10123 // clang-format on124 };125 test(vals);126 }127 128 {129 bool vals[] = {true, false};130 test(vals);131 }132 133 {134 char* vals[] = {(char*)("a"), (char*)("b"), (char*)("cde")};135 test(vals);136 }137 138 return 0;139}140