-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.h
136 lines (118 loc) · 2.88 KB
/
lib.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2020 Felipe O. Carvalho
#pragma once
#include <string>
#include <utility>
#include <vector>
// STL helpers
template <typename MapType>
typename MapType::mapped_type *lookup(MapType &m, const typename MapType::key_type &k) {
auto it = m.find(k);
if (it != m.end()) {
return &it->second;
}
return nullptr;
}
template <typename MapType>
const typename MapType::mapped_type *lookup(const MapType &m, const typename MapType::key_type &k) {
auto it = m.find(k);
if (it != m.end()) {
return &it->second;
}
return nullptr;
}
template <typename Container>
bool contains(const Container &s, const typename Container::value_type &v) {
return s.find(v) != s.end();
}
template <typename Container>
bool linearContains(const Container &s, const typename Container::value_type &v) {
return std::find(s.begin(), s.end(), v) != s.end();
}
template <typename T>
void hash_combine(std::size_t &seed, const T &val) {
seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
// Hashers
namespace std {
template <typename A, typename B>
struct hash<std::pair<A, B>> {
size_t operator()(const std::pair<A, B> &k) const {
size_t h = 0;
hash_combine(h, k.first);
hash_combine(h, k.second);
return h;
}
};
template <>
struct hash<vector<string>> {
size_t operator()(const vector<string> &v) const {
size_t h = 0;
for (const auto &s : v) {
hash_combine(h, s);
}
return h;
}
};
template <typename T>
struct hash<unordered_set<T>> {
size_t operator()(const unordered_set<T> &v) const {
hash<T> elem_hasher;
size_t h = 0;
for (const auto &elem : v) {
h ^= elem_hasher(elem);
}
return h;
}
};
template <typename K, typename V>
struct hash<unordered_map<K, V>> {
size_t operator()(const unordered_map<K, V> &v) const {
size_t ret = 0;
for (const auto & [ key, value ] : v) {
size_t h = 0;
hash_combine(h, key);
hash_combine(h, value);
ret ^= h;
}
return ret;
}
};
} // namespace std
template <typename T>
struct ValuePrinter {
void print(const T &) { assert(false && "ValuePrinter not implemented"); }
};
template <>
struct ValuePrinter<std::string> {
void print(const std::string &value) { printf("'%s'", value.c_str()); }
};
template <typename T>
struct ValuePrinter<std::optional<T>> {
void print(const std::optional<T> &value) {
if (value.has_value()) {
ValuePrinter<T> printer;
printf("Some(");
printer.print(*value);
putchar(')');
} else {
printf("None");
}
}
};
template <typename T>
struct ValuePrinter<std::unordered_set<T>> {
void print(const std::unordered_set<T> &value) {
ValuePrinter<T> printer;
putchar('{');
bool first = true;
for (auto &elem : value) {
if (!first) {
printf(", ");
} else {
first = false;
}
printer.print(elem);
}
putchar('}');
}
};