-
Notifications
You must be signed in to change notification settings - Fork 1
/
heterogeneous_unordered_lookup_demo.cpp
79 lines (58 loc) · 2.7 KB
/
heterogeneous_unordered_lookup_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Demos MSVC STL implementation for https://wg21.link/P0919R3 and patched by https://wg21.link/P1690R1
#include <iostream>
#include <string_view>
#include <string>
#include <unordered_map>
#include <unordered_set>
using namespace std::literals;
struct transparent_string_hasher {
using is_transparent = void; // For the lookup overloads to work, 'Hash::is_transparent' must be valid and denote a type
size_t operator()(const std::string_view target) const noexcept {
return std::hash<std::string_view>{}(target);
}
};
// Alternatively, based on a refinement, https://wg21.link/P1690R1, std::equal_to<> could be used for 'Pred' template parameter
struct transparent_string_equal {
using is_transparent = void; // For the lookup overloads to work, 'Pred::is_transparent' must be valid and denote a type
bool operator()(const std::string_view lhs, const std::string_view rhs) const noexcept {
return lhs == rhs;
}
};
void unordered_map_demo()
{
std::cout << "std::unordered_map:\n";
std::unordered_map<std::string, int, transparent_string_hasher, transparent_string_equal> demo_unordered_map = {
{"apples", 1},
{"oranges", 2},
{"bananas", 3} };
std::string key = "apples";
std::cout << "Key \"" << key << "\" found: " << demo_unordered_map.contains(key) << std::endl;
std::string_view k_view = "vegetables";
std::cout << "Key \"" << k_view << "\"sv found: " << demo_unordered_map.contains(k_view) << std::endl;
std::cout << "Key \"bananas\"sv found: " << demo_unordered_map.contains("bananas"sv) << std::endl;
std::cout << "Key \"oranges\" found: " << (demo_unordered_map.find("oranges") != demo_unordered_map.end()) << std::endl;
}
void unordered_set_demo()
{
std::cout << "std::unordered_set:\n";
std::unordered_set<std::string, transparent_string_hasher, std::equal_to<>> demo_unordered_set = {
"apples",
"oranges",
"bananas" };
std::string key = "apples";
std::cout << "Key \"" << key << "\" found: " << demo_unordered_set.contains(key) << std::endl;
std::string_view k_view = "vegetables";
std::cout << "Key \"" << k_view << "\"sv found: " << demo_unordered_set.contains(k_view) << std::endl;
std::cout << "Key \"bananas\"sv found: " << demo_unordered_set.contains("bananas"sv) << std::endl;
std::cout << "Key \"oranges\" found: " << (demo_unordered_set.find("oranges") != demo_unordered_set.end()) << std::endl;
}
void heterogeneous_unordered_lookup_demo()
{
// Set boolalpha format flag so bool values are represented by their textual representation: true or false
std::boolalpha(std::cout);
std::cout << "\nheterogeneous unordered lookup demo:\n";
unordered_map_demo();
unordered_set_demo();
}