Newer
Older

Christian Godenschwager
committed
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file AllSet.h
//! \ingroup core
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//! \author Christian Feichtinger
//
//======================================================================================================================
#pragma once
#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
namespace walberla{
template< typename T > class AllSet;
template< typename T > inline AllSet<T> setIntersection( const AllSet<T>& a, const AllSet<T>& b );
template< typename T > inline AllSet<T> setUnion ( const AllSet<T>& a, const AllSet<T>& b );
template< typename T > inline AllSet<T> setDifference ( const AllSet<T>& a, const AllSet<T>& b );
template< typename T > inline bool setIsEqual( const AllSet<T>& a, const AllSet<T>& b );
//**********************************************************************************************************************
/*!
* \brief A class for managing sets that supports the ability to define a set that represents all possible elements
*
* What is special about this set is the possibility to define a set that contains all possible elements:
*
\code
AllSet<int> universe = AllSet<int>::all(); // "universe" contains all integers
bool contains = universe.contains( [any integer] ); // -> true
universe -= 42; // "universe" contains all integers except 42
contains = universe.contains( 42 ); // -> false
AllSet<int> set = AllSet<int>(5) + AllSet<int>(23); // a set that consists of two integers: 5 and 23
contains = universe.contains( set ); // -> true
set += 42;
contains = universe.contains( set ); // -> false
bool intersects = universe.intersects( set ); // -> true
\endcode
*
* If the equality of two sets must be tested, the operators "==" and "!=" and the member function "isEqual" can be
* used. If two sets must be compared in terms of size, the operators "<", ">", "<=", and ">=" and the member function
* "equalSize" can be used:
*
\code
AllSet<int> aAll = AllSet<int>::all() - 42;
AllSet<int> bAll = AllSet<int>::all();
bool boolean = ( aAll == bAll ); // -> false
boolean = aAll.equalSize( bAll ); // -> false
boolean = ( aAll < bAll ); // -> true
bAll -= 23;
boolean = ( aAll == bAll ); // -> false
boolean = aAll.equalSize( bAll ); // -> true
boolean = ( aAll < bAll ); // -> false
aAll -= 23;
bAll -= 42;
boolean = ( aAll == bAll ); // -> true
boolean = aAll.equalSize( bAll ); // -> true
\endcode
*/
//**********************************************************************************************************************
template< typename T >
class AllSet {
public:
friend inline AllSet<T> operator&( const AllSet& a, const AllSet& b ) { return setIntersection(a,b); } ///< intersection
friend inline AllSet<T> operator+( const AllSet& a, const AllSet& b ) { return setUnion (a,b); } ///< union
friend inline AllSet<T> operator-( const AllSet& a, const AllSet& b ) { return setDifference (a,b); } ///< difference / relative complement
friend inline bool operator==( const AllSet& a, const AllSet& b ) { return setIsEqual(a,b); } ///< compares the content of two sets
friend inline bool operator!=( const AllSet& a, const AllSet& b ) { return !setIsEqual(a,b); } ///< compares the content of two sets
typedef typename std::set<T>::const_iterator ConstIter;
inline explicit AllSet( const bool _all = false ) : all_( _all ) {}
inline AllSet( const T& element ) : all_( false ) { set_.insert( element ); }
inline static const AllSet<T>& all() { static AllSet set( true ); return set; }
inline static const AllSet<T>& emptySet() { static AllSet set( false ); return set; }
void invert() { all_ = !all_; }
AllSet<T> getComplement() const { AllSet<T> set( *this ); set.all_ = !all_; return set; }
void insert( const T& element ) { if( isAll() ) set_.erase( element ); else set_.insert( element ); }
void clear() { all_ = false; set_.clear(); }
const AllSet<T>& operator&=( const AllSet<T>& set ); ///< intersection
const AllSet<T>& operator+=( const AllSet<T>& set ); ///< union
const AllSet<T>& operator-=( const AllSet<T>& set ); ///< difference / relative complement
bool operator< ( const AllSet<T>& set ) const; ///< compares the size (not the content!) of two sets
bool operator> ( const AllSet<T>& set ) const; ///< compares the size (not the content!) of two sets
inline bool operator<=( const AllSet<T>& set ) const { return !(operator>( set )); } ///< compares the size (not the content!) of two sets
inline bool operator>=( const AllSet<T>& set ) const { return !(operator<( set )); } ///< compares the size (not the content!) of two sets
inline bool equalSize ( const AllSet<T>& set ) const; ///< compares the size (not the content!) of two sets
bool intersects( const AllSet<T>& set ) const;
bool contains ( const AllSet<T>& set ) const;
inline bool contains ( const T& element ) const;
bool isEqual ( const AllSet<T>& set ) const { return all_ == set.all_ && set_ == set.set_; }
inline bool isUniverse() const { return all_ && set_.empty(); }
inline bool isEmpty() const { return !all_ && set_.empty(); }
inline bool isAll() const { return all_; }
inline bool isCountable() const { return !all_; }
void toStream( std::ostream& os ) const;
inline std::string toString() const;
inline ConstIter begin() const { return set_.begin(); }
inline ConstIter end() const { return set_.end(); }
private:
bool all_; ///< true if the set represents a set of all possible elements (= "the universe" / the complement of the empty set)
std::set<T> set_;
}; // class AllSet
//**********************************************************************************************************************
/*!
* \brief Calculates the intersection of "this" and "set", only the resulting set is kept.
*/
//**********************************************************************************************************************
template< typename T >
const AllSet<T>& AllSet<T>::operator&=( const AllSet<T>& set ) { // intersection
if( isAll() ) {
if( set.isAll() ) {
set_.insert( set.begin(), set.end() );
}
else {
std::set<T> result;
std::set_difference( set.begin(), set.end(), begin(), end(), std::inserter( result, result.end() ) );
set_ = result;
all_ = false;
}
}
else {
if( set.isAll() ) {
std::set<T> result;
std::set_difference( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
else {
std::set<T> result;
std::set_intersection( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
}
return *this;
}
//**********************************************************************************************************************
/*!
* \brief Calculates the union of "this" and "set", only the resulting set is kept.
*/
//**********************************************************************************************************************
template< typename T >
const AllSet<T>& AllSet<T>::operator+=( const AllSet<T>& set ) { // union
if( isAll() ) {
if( set.isAll() ) {
std::set<T> result;
std::set_intersection( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
else {
std::set<T> result;
std::set_difference( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
}
else {
if( set.isAll() ) {
std::set<T> result;
std::set_difference( set.begin(), set.end(), begin(), end(), std::inserter( result, result.end() ) );
set_ = result;
all_ = true;
}
else {
set_.insert( set.begin(), set.end() );
}
}
return *this;
}
//**********************************************************************************************************************
/*!
* \brief Calculates the difference of "this" and "set", only the resulting set (result = this - set) is kept.
*/
//**********************************************************************************************************************
template< typename T >
const AllSet<T>& AllSet<T>::operator-=( const AllSet<T>& set ) { // difference / relative complement
if( isAll() ) {
if( set.isAll() ) {
std::set<T> result;
std::set_difference( set.begin(), set.end(), begin(), end(), std::inserter( result, result.end() ) );
set_ = result;
all_ = false;
}
else {
set_.insert( set.begin(), set.end() );
}
}
else {
if( set.isAll() ) {
std::set<T> result;
std::set_intersection( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
else {
std::set<T> result;
std::set_difference( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
}
}
return *this;
}
//**********************************************************************************************************************
/*!
* \brief Returns true only if there are less elements in "this" set than in the set "set".
*/
//**********************************************************************************************************************
template< typename T >
bool AllSet<T>::operator<( const AllSet<T>& set ) const {
if( isAll() ) {
if( set.isAll() )
return set_.size() > set.set_.size();
else
return false;
}
else if( set.isAll() )
return true;
return set_.size() < set.set_.size();
}
//**********************************************************************************************************************
/*!
* \brief Returns true only if there are more elements in "this" set than in the set "set".
*/
//**********************************************************************************************************************
template< typename T >
bool AllSet<T>::operator>( const AllSet<T>& set ) const {
if( isAll() ) {
if( set.isAll() )
return set_.size() < set.set_.size();
else
return true;
}
else if( set.isAll() )
return false;
return set_.size() > set.set_.size();
}
//**********************************************************************************************************************
/*!
* \brief Returns true only if there are as many elements in "this" set as there are elements in the set "set".
*/
//**********************************************************************************************************************
template< typename T >
inline bool AllSet<T>::equalSize( const AllSet<T>& set ) const {
return all_ == set.all_ && set_.size() == set.set_.size();
}
/// \cond internal
namespace allset {
template< class InputIterator1, class InputIterator2 >
bool setsIntersect( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 );
} // namespace allset
/// \endcond
//**********************************************************************************************************************
/*!
* \brief Returns true only if there is an intersection between "this" and "set".
*/
//**********************************************************************************************************************
template< typename T >
bool AllSet<T>::intersects( const AllSet<T>& set ) const {
if( isAll() ) {
if( set.isAll() ) {
return true;
}
else {
return !std::includes( begin(), end(), set.begin(), set.end() );
}
}
else if( set.isAll() ) {
return !std::includes( set.begin(), set.end(), begin(), end() );
}
return allset::setsIntersect( begin(), end(), set.begin(), set.end() );
}
//**********************************************************************************************************************
/*!
* \brief Returns true only if "set" is a strict subset of "this".
*/
//**********************************************************************************************************************
template< typename T >
bool AllSet<T>::contains( const AllSet<T>& set ) const {
if( isAll() ) {
if( set.isAll() ) {
return std::includes( set.begin(), set.end(), begin(), end() );
}
else {
return !allset::setsIntersect( begin(), end(), set.begin(), set.end() );
}
}
else if( set.isAll() ) {
return false;
}
return std::includes( begin(), end(), set.begin(), set.end() );
}
//**********************************************************************************************************************
/*!
* \brief Returns true only if the element "element" is included in this set.
*/
//**********************************************************************************************************************
template< typename T >
inline bool AllSet<T>::contains( const T& element ) const {
if( isAll() )
return set_.find( element ) == set_.end();
return set_.find( element ) != set_.end();
}
template< typename T >
void AllSet<T>::toStream( std::ostream& os ) const {
os << "{ ";
if( isAll() ) {
os << "ALL ";
if( !(set_.empty()) ) os << "- ";
}
for( ConstIter it = begin(); it != end(); ++it ) {
ConstIter next = it;
os << (*it) << ( ( ++next == end() ) ? " " : ", " );
}
os << "}";
}
template< typename T >
inline std::string AllSet<T>::toString() const {
std::ostringstream oss;
toStream( oss );
return oss.str();
}
//////////////////////
// Global Functions //
//////////////////////
template< typename T >
inline AllSet<T> setIntersection( const AllSet<T>& a, const AllSet<T>& b ) {
AllSet<T> result(a);
result &= b;
return result;
}
template< typename T >
inline AllSet<T> setUnion( const AllSet<T>& a, const AllSet<T>& b ) {
AllSet<T> result(a);
result += b;
return result;
}
template< typename T >
inline AllSet<T> setDifference( const AllSet<T>& a, const AllSet<T>& b ) {
AllSet<T> result(a);
result -= b;
return result;
}
template< typename T >
inline bool setIsEqual( const AllSet<T>& a, const AllSet<T>& b ) {
return a.isEqual(b);
}
template< typename T >
inline std::ostream& operator<<( std::ostream& os, const AllSet<T>& set ) {
set.toStream( os );
return os;
}
/// \cond internal
namespace allset {
template< class InputIterator1, class InputIterator2 >
bool setsIntersect( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 ) {
while( first1 != last1 && first2 != last2) {
if( *first1 < *first2 ) ++first1;
else if( *first2 < *first1 ) ++first2;
else return true;
}
return false;
}
} // namespace allset
/// \endcond
} // namespace walberla