-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSet.h
More file actions
301 lines (249 loc) · 7.3 KB
/
Set.h
File metadata and controls
301 lines (249 loc) · 7.3 KB
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
#pragma once
#include "List.h"
#include <algorithm>
#include <set>
namespace pure {
namespace set {
/*
* Set theory!
*
* A small set of operator overloads to simplify working with sets, or
* generically, sequences.
*
* S(x,y,z...) -- create a set from x, y, z...
* !s -- s is null.
* +s -- the length of s (Or: |s|.)
* x > s | s < x -- s contains x
* s >> x -- s without x
* s << x -- s with x
* xs | ys -- The union of xs and ys.
* xs / ys -- The difference of xs and ys.
* xs % ys -- The intersection of xs and ys.
* xs * xs -- The Cartesian product of xs and xs.
*
* The source has been split into two sections: generic, which implements
* versions of these functions that should work on all containers, and ordered,
* which takes advantage of ordering for optimization. The common namespace
* implements functions that are the same in both.
*
* To use this, fully import the namespace with one of the fallowing:
* using namespace pure::set; // for generic
* using namespace pure::set::ordered;
*/
namespace common {
/* Create a set. */
template< class X, class ...Y >
std::set<Decay<X>> S( X&& x, Y&& ...y ) {
return std::set<Decay<X>>({ std::forward<X>(x),
std::forward<Y>(y)... });
}
/* s is null */
template< class S >
bool operator ! ( const S& s ) {
return pure::list::null(s);
}
/* |s| -- the magnitude of s. (The closest thing we have to an abs sign.) */
template< class S >
unsigned long long operator + ( const S& s ) {
return pure::list::length( s );
}
/* The Cartesian product of xs and ys. */
template< class XS, class YS,
class X = list::SeqVal<XS>, class Y = list::SeqVal<YS>,
class P = std::pair<X,Y>,
class R = list::Remap<XS,P> >
R operator * ( const XS& xs, const YS& ys ) {
return list::map (
[]( const X& x, const Y& y ) { return P{x,y}; },
xs, ys
);
}
} // namespace common
namespace generic {
using namespace common;
// The fallowing do not appear in set::ordered.
/* ys appended to xs */
template< class XS, class YS >
Decay<XS> operator + ( XS&& xs, YS&& ys ) {
return list::append( std::forward<XS>(xs), std::forward<YS>(ys) );
}
/* reverse s */
template< class S >
auto operator - ( S&& s ) -> decltype( list::reverse_wrap(std::declval<S>()) )
{
return list::reverse_wrap( std::forward<S>(s) );
}
// These operations are guaranteed to work even if the arguments are unsorted
// or contain duplicates.
/* x is an element of s */
template< class X, class S >
bool operator < ( const X& x, const S& s ) {
return pure::list::elem( x, s );
}
/* s contains x */
template< class S, class X >
bool operator > ( const S& s, const X& x ) {
return pure::list::elem( x, s );
}
/* xs is a subset of ys */
template< class XS, class YS >
bool operator <= ( XS xs, const YS& ys ) {
for( const auto& y : ys ) {
xs <<= y;
// If everything in xs has been found in ys, xs is a subset of ys.
if( ! xs )
return true;
}
return false;
}
/* s without x */
template< class S, class X >
Decay<S> operator >> ( S&& s, const X& x ) {
return pure::list::erase( x, std::forward<S>(s) );
}
/* (reference version) */
template< class S, class X >
S& operator >>= ( S& s, const X& x ) {
s = std::move(s) >> x;
return s;
}
/* s appended with x */
template< class S, class X >
Decay<S> operator << ( S&& s, X&& x ) {
return pure::list::cons( std::forward<S>(s), std::forward<X>(x) );
}
/* (reference version) */
template< class S, class X >
S& operator <<= ( S& s, X&& x ) {
s = std::move(s) << std::forward<X>(x);
return s;
}
/* The union of xs and ys (with no duplicates). */
template< class XS, class YS >
XS operator | ( XS xs, YS&& ys ) {
for( auto y : std::forward<YS>(ys) )
if( not (y<xs) )
xs <<= y;
return xs;
}
/* (reference version) */
template< class XS, class YS >
XS& operator |= ( XS& xs, YS&& ys ) {
xs = std::move(xs) | std::forward<YS>(ys);
return xs;
}
/* Every x from xs such that there is no y from ys where x = y. */
template< class XS, class YS >
XS operator / ( XS xs, const YS& ys ) {
for( const auto& y : ys )
xs >>= y;
return xs;
}
/* (reference version) */
template< class XS, class YS >
XS& operator /= ( XS& xs, const YS& ys ) {
xs = std::move(xs) / ys;
return xs;
}
/* The intersection of xs and ys. (Or: The remainder of xs/ys.) */
template< class XS, class YS >
XS operator % ( const XS& xs, const YS& ys ) {
XS r;
for( const auto& y : ys )
if( y < xs )
r <<= y;
return r;
}
/* (reference version) */
template< class XS, class YS >
XS& operator %= ( XS& xs, YS&& ys ) {
xs = std::move(xs) % std::forward<YS>(ys);
return xs;
}
} // namespace generic
namespace ordered {
using namespace common;
// These versions take advantage of knowing the container is ordered.
/* x is an element of s */
template< class X, class S >
bool operator < ( const X& x, const S& s ) {
return std::binary_search( begin(s), end(s), x );
}
/* s contains x */
template< class S, class X >
bool operator > ( const S& s, const X& x ) {
return std::binary_search( begin(s), end(s), x );
}
/* xs is a subset of ys */
template< class XS, class YS >
bool operator <= ( const XS& xs, const YS& ys ) {
return std::includes( begin(ys), end(ys), begin(xs), end(xs) );
}
/* s without x */
template< class S, class X >
Decay<S> operator >> ( S&& s, const X& x ) {
return pure::list::erase( x, std::forward<S>(s) );
}
/* (reference version) */
template< class S, class X >
S& operator >>= ( S& s, const X& x ) {
s = std::move(s) >> x;
return s;
}
/* s appended with x */
template< class S, class X >
Decay<S> operator << ( S&& s, X&& x ) {
return pure::list::insert( std::forward<X>(x), std::forward<S>(s) );
}
/* (reference version) */
template< class S, class X >
S& operator <<= ( S& s, X&& x ) {
s = std::move(s) << std::forward<X>(x);
return s;
}
/* The union of xs and ys (with no duplicates). */
template< class XS, class YS >
XS operator | ( const XS& xs, const YS& ys ) {
XS r;
std::merge( begin(xs), end(xs), begin(ys), end(ys),
list::tailInserter(r) );
return r;
}
/* (reference version) */
template< class XS, class YS >
XS& operator |= ( XS& xs, YS&& ys ) {
xs = std::move(xs) | std::forward<YS>(ys);
return xs;
}
/* Every x from xs such that there is no y from ys where x = y. */
template< class XS, class YS >
XS operator / ( const XS& xs, const YS& ys ) {
XS r;
std::set_difference( begin(xs), end(xs), begin(ys), end(ys),
list::tailInserter(r) );
return r;
}
/* (reference version) */
template< class XS, class YS >
XS& operator /= ( XS& xs, const YS& ys ) {
xs = std::move(xs) / ys;
return xs;
}
/* The intersection of xs and ys. (Or: The remainder of xs/ys.) */
template< class XS, class YS >
XS operator % ( const XS& xs, const YS& ys ) {
XS r;
std::set_intersection( begin(xs), end(xs), begin(ys), end(ys),
list::tailInserter(r) );
return r;
}
/* (reference version) */
template< class XS, class YS >
XS& operator %= ( XS& xs, YS&& ys ) {
xs = std::move(xs) % std::forward<YS>(ys);
return xs;
}
} // namespace ordered
using namespace generic;
} // namespace set
}