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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#pragma once
#include <set>
#include <unordered_set>
#include "StarFlatHashSet.hpp"
#include "StarList.hpp"
namespace Star {
STAR_EXCEPTION(SetException, StarException);
template <typename BaseSet>
class SetMixin : public BaseSet {
public:
typedef BaseSet Base;
typedef typename Base::iterator iterator;
typedef typename Base::const_iterator const_iterator;
typedef typename Base::value_type value_type;
using Base::Base;
List<value_type> values() const;
bool contains(value_type const& v) const;
bool add(value_type const& v);
// Like add, but always adds new value, potentially replacing another equal
// (comparing equal, may not be actually equal) value. Returns whether an
// existing value was replaced.
bool replace(value_type v);
template <typename Container>
void addAll(Container const& s);
bool remove(value_type const& v);
template <typename Container>
void removeAll(Container const& s);
value_type first();
Maybe<value_type> maybeFirst();
value_type takeFirst();
Maybe<value_type> maybeTakeFirst();
value_type last();
Maybe<value_type> maybeLast();
value_type takeLast();
Maybe<value_type> maybeTakeLast();
bool hasIntersection(SetMixin const& s) const;
};
template <typename BaseSet>
std::ostream& operator<<(std::ostream& os, SetMixin<BaseSet> const& set);
template <typename Value, typename Compare = std::less<Value>, typename Allocator = std::allocator<Value>>
class Set : public SetMixin<std::set<Value, Compare, Allocator>> {
public:
typedef SetMixin<std::set<Value, Compare, Allocator>> Base;
typedef typename Base::iterator iterator;
typedef typename Base::const_iterator const_iterator;
typedef typename Base::value_type value_type;
template <typename Container>
static Set from(Container const& c);
using Base::Base;
// Returns set of elements that are in this set and the given set.
Set intersection(Set const& s) const;
Set intersection(Set const& s, std::function<bool(Value const&, Value const&)> compare) const;
// Returns elements in this set that are not in the given set
Set difference(Set const& s) const;
Set difference(Set const& s, std::function<bool(Value const&, Value const&)> compare) const;
// Returns elements in either this set or the given set
Set combination(Set const& s) const;
};
template <typename BaseSet>
class HashSetMixin : public SetMixin<BaseSet> {
public:
typedef SetMixin<BaseSet> Base;
typedef typename Base::iterator iterator;
typedef typename Base::const_iterator const_iterator;
typedef typename Base::value_type value_type;
template <typename Container>
static HashSetMixin from(Container const& c);
using Base::Base;
HashSetMixin intersection(HashSetMixin const& s) const;
HashSetMixin difference(HashSetMixin const& s) const;
HashSetMixin combination(HashSetMixin const& s) const;
};
template <typename Value, typename Hash = hash<Value>, typename Equals = std::equal_to<Value>, typename Allocator = std::allocator<Value>>
using HashSet = HashSetMixin<FlatHashSet<Value, Hash, Equals, Allocator>>;
template <typename Value, typename Hash = hash<Value>, typename Equals = std::equal_to<Value>, typename Allocator = std::allocator<Value>>
using StableHashSet = HashSetMixin<std::unordered_set<Value, Hash, Equals, Allocator>>;
template <typename BaseSet>
auto SetMixin<BaseSet>::values() const -> List<value_type> {
return List<value_type>(Base::begin(), Base::end());
}
template <typename BaseSet>
bool SetMixin<BaseSet>::contains(value_type const& v) const {
return Base::find(v) != Base::end();
}
template <typename BaseSet>
bool SetMixin<BaseSet>::add(value_type const& v) {
return Base::insert(v).second;
}
template <typename BaseSet>
bool SetMixin<BaseSet>::replace(value_type v) {
bool replaced = remove(v);
Base::insert(std::move(v));
return replaced;
}
template <typename BaseSet>
template <typename Container>
void SetMixin<BaseSet>::addAll(Container const& s) {
return Base::insert(s.begin(), s.end());
}
template <typename BaseSet>
bool SetMixin<BaseSet>::remove(value_type const& v) {
return Base::erase(v) != 0;
}
template <typename BaseSet>
template <typename Container>
void SetMixin<BaseSet>::removeAll(Container const& s) {
for (auto const& v : s)
remove(v);
}
template <typename BaseSet>
auto SetMixin<BaseSet>::first() -> value_type {
if (Base::empty())
throw SetException("first called on empty set");
return *Base::begin();
}
template <typename BaseSet>
auto SetMixin<BaseSet>::maybeFirst() -> Maybe<value_type> {
if (Base::empty())
return {};
return *Base::begin();
}
template <typename BaseSet>
auto SetMixin<BaseSet>::takeFirst() -> value_type {
if (Base::empty())
throw SetException("takeFirst called on empty set");
auto i = Base::begin();
value_type v = std::move(*i);
Base::erase(i);
return v;
}
template <typename BaseSet>
auto SetMixin<BaseSet>::maybeTakeFirst() -> Maybe<value_type> {
if (Base::empty())
return {};
auto i = Base::begin();
value_type v = std::move(*i);
Base::erase(i);
return std::move(v);
}
template <typename BaseSet>
auto SetMixin<BaseSet>::last() -> value_type {
if (Base::empty())
throw SetException("last called on empty set");
return *prev(Base::end());
}
template <typename BaseSet>
auto SetMixin<BaseSet>::maybeLast() -> Maybe<value_type> {
if (Base::empty())
return {};
return *prev(Base::end());
}
template <typename BaseSet>
auto SetMixin<BaseSet>::takeLast() -> value_type {
if (Base::empty())
throw SetException("takeLast called on empty set");
auto i = prev(Base::end());
value_type v = std::move(*i);
Base::erase(i);
return v;
}
template <typename BaseSet>
auto SetMixin<BaseSet>::maybeTakeLast() -> Maybe<value_type> {
if (Base::empty())
return {};
auto i = prev(Base::end());
value_type v = std::move(*i);
Base::erase(i);
return std::move(v);
}
template <typename BaseSet>
bool SetMixin<BaseSet>::hasIntersection(SetMixin const& s) const {
for (auto const& v : s) {
if (contains(v)) {
return true;
}
}
return false;
}
template <typename BaseSet>
std::ostream& operator<<(std::ostream& os, SetMixin<BaseSet> const& set) {
os << "(";
for (auto i = set.begin(); i != set.end(); ++i) {
if (i != set.begin())
os << ", ";
os << *i;
}
os << ")";
return os;
}
template <typename Value, typename Compare, typename Allocator>
template <typename Container>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::from(Container const& c) {
return Set(c.begin(), c.end());
}
template <typename Value, typename Compare, typename Allocator>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::intersection(Set const& s) const {
Set res;
std::set_intersection(Base::begin(), Base::end(), s.begin(), s.end(), std::inserter(res, res.end()));
return res;
}
template <typename Value, typename Compare, typename Allocator>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::intersection(Set const& s, std::function<bool(Value const&, Value const&)> compare) const {
Set res;
std::set_intersection(Base::begin(), Base::end(), s.begin(), s.end(), std::inserter(res, res.end()), compare);
return res;
}
template <typename Value, typename Compare, typename Allocator>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::difference(Set const& s) const {
Set res;
std::set_difference(Base::begin(), Base::end(), s.begin(), s.end(), std::inserter(res, res.end()));
return res;
}
template <typename Value, typename Compare, typename Allocator>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::difference(Set const& s, std::function<bool(Value const&, Value const&)> compare) const {
Set res;
std::set_difference(Base::begin(), Base::end(), s.begin(), s.end(), std::inserter(res, res.end()), compare);
return res;
}
template <typename Value, typename Compare, typename Allocator>
Set<Value, Compare, Allocator> Set<Value, Compare, Allocator>::combination(Set const& s) const {
Set ret(*this);
ret.addAll(s);
return ret;
}
template <typename BaseMap>
template <typename Container>
HashSetMixin<BaseMap> HashSetMixin<BaseMap>::from(Container const& c) {
return HashSetMixin(c.begin(), c.end());
}
template <typename BaseMap>
HashSetMixin<BaseMap> HashSetMixin<BaseMap>::intersection(HashSetMixin const& s) const {
// Can't use std::set_intersection, since not sorted, naive version is fine.
HashSetMixin ret;
for (auto const& e : s) {
if (contains(e))
ret.add(e);
}
return ret;
}
template <typename BaseMap>
HashSetMixin<BaseMap> HashSetMixin<BaseMap>::difference(HashSetMixin const& s) const {
// Can't use std::set_difference, since not sorted, naive version is fine.
HashSetMixin ret;
for (auto const& e : *this) {
if (!s.contains(e))
ret.add(e);
}
return ret;
}
template <typename BaseMap>
HashSetMixin<BaseMap> HashSetMixin<BaseMap>::combination(HashSetMixin const& s) const {
HashSetMixin ret(*this);
ret.addAll(s);
return ret;
}
}
|