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
//======================================================================================================================
//
// 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 Set.h
//! \ingroup core
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================
#pragma once
#include "core/mpi/BufferDataTypeExtensions.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <sstream>
namespace walberla {
template< typename T > class Set;
template< typename T > inline Set<T> setIntersection( const Set<T>& a, const Set<T>& b );
template< typename T > inline Set<T> setUnion ( const Set<T>& a, const Set<T>& b );
template< typename T > inline Set<T> setDifference ( const Set<T>& a, const Set<T>& b );
template< typename T > inline bool setIsEqual( const Set<T>& a, const Set<T>& b );
/// \cond internal
namespace set {
template< class InputIterator1, class InputIterator2 >
bool setsIntersect( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 );
} // namespace set
/// \endcond
//**********************************************************************************************************************
/*!
* \brief Wrapper class for managing sets that store elements of type T
*
* 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
Set<int> A = Set<int>(1) + Set<int>(2) + Set<int>(3);
Set<int> B = Set<int>(1) + Set<int>(2);
Set<int> C = Set<int>(1) + Set<int>(2) + Set<int>(4);
bool boolean = ( A == B ); // -> false
boolean = ( A != B ); // -> true
boolean = ( A > B ); // -> true
boolean = ( A >= B ); // -> true
boolean = ( A < B ); // -> false
boolean = A.equalSize(B); // -> false
boolean = ( A == C ); // -> false
boolean = A.equalSize(C); // -> true
B.insert(3);
boolean = ( A == B ); // -> true
boolean = A.equalSize(B); // -> true
boolean = ( A > B ); // -> false
boolean = ( A >= B ); // -> true
boolean = ( A <= B ); // -> true
\endcode
*/
//**********************************************************************************************************************
template< typename T >
class Set {
public:
typedef typename std::set<T>::value_type value_type;
typedef typename std::set<T>::const_iterator const_iterator;
typedef typename std::set<T>::iterator iterator;
friend inline Set<T> operator&( const Set& a, const Set& b ) { return setIntersection(a,b); } ///< intersection
friend inline Set<T> operator+( const Set& a, const Set& b ) { return setUnion (a,b); } ///< union
friend inline Set<T> operator-( const Set& a, const Set& b ) { return setDifference (a,b); } ///< difference / relative complement
friend inline bool operator==( const Set& a, const Set& b ) { return setIsEqual(a,b); } ///< compares the content of two sets
friend inline bool operator!=( const Set& a, const Set& b ) { return !setIsEqual(a,b); } ///< compares the content of two sets
inline Set() {}
inline Set( const T& element ) { set_.insert( element ); }
inline virtual ~Set() {}
static const Set<T>& emptySet() { static Set set; return set; }
inline std::pair<iterator,bool> insert( const T& element ) { return set_.insert( element ); }
inline iterator insert( iterator position, const T& element ) { return set_.insert( position, element ); }
template <class InputIterator>
inline void insert( InputIterator first, InputIterator last ) { set_.insert( first, last); }
inline void clear() { set_.clear(); } ///< removes all elements from this set
inline const Set<T>& operator&=( const Set<T>& set ); ///< intersection
inline const Set<T>& operator+=( const Set<T>& set ); ///< union
inline const Set<T>& operator-=( const Set<T>& set ); ///< difference / relative complement
inline bool operator< ( const Set<T>& set ) const { return set_.size() < set.set_.size(); } ///< compares the size (not the content!) of two sets
inline bool operator> ( const Set<T>& set ) const { return set_.size() > set.set_.size(); } ///< compares the size (not the content!) of two sets
inline bool operator<=( const Set<T>& set ) const { return !(operator>( set )); } ///< compares the size (not the content!) of two sets
inline bool operator>=( const Set<T>& set ) const { return !(operator<( set )); } ///< compares the size (not the content!) of two sets
inline bool equalSize ( const Set<T>& set ) const { return set_.size() == set.set_.size(); } ///< compares the size (not the content!) of two sets
bool intersects( const Set<T>& set ) const { return set::setsIntersect( begin(), end(), set.begin(), set.end() ); } ///< true if both sets intersect
bool contains ( const Set<T>& set ) const { return std::includes( begin(), end(), set.begin(), set.end() ); } ///< true if "set" is completely contained within this set
bool contains ( const T& element ) const { return set_.find( element ) != set_.end(); } ///< true if "element" is contained within this set
bool isEqual ( const Set<T>& set ) const { return set_ == set.set_; } ///< true if both sets contain the same elements
inline bool empty() const { return set_.empty(); } ///< true if this set is empty
inline bool isEmpty() const { return empty(); } ///< true if this set is empty
inline size_t size() const { return set_.size(); }
inline void swap( Set<T>& set ) { set_.swap( set.set_ ); }
void toStream( std::ostream& os ) const;
inline std::string toString() const;
inline const_iterator begin() const { return set_.begin(); }
inline iterator begin() { return set_.begin(); }
inline const_iterator end() const { return set_.end(); }
inline iterator end() { return set_.end(); }
inline const std::set<T> & get() const { return set_; }
inline std::set<T> & get() { return set_; }
private:
std::set<T> set_;
}; // class Set
//**********************************************************************************************************************
/*!
* \brief Calculates the intersection of "this" and "set", only the resulting set is kept.
*/
//**********************************************************************************************************************
template< typename T >
inline const Set<T>& Set<T>::operator&=( const Set<T>& set ) { // intersection
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 >
inline const Set<T>& Set<T>::operator+=( const Set<T>& set ) { // union
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 >
inline const Set<T>& Set<T>::operator-=( const Set<T>& set ) { // difference / relative complement
std::set<T> result;
std::set_difference( begin(), end(), set.begin(), set.end(), std::inserter( result, result.end() ) );
set_ = result;
return *this;
}
template< typename T >
void Set<T>::toStream( std::ostream& os ) const {
os << "{ ";
for( const_iterator it = begin(); it != end(); ++it ) {
const_iterator next = it;
os << (*it) << ( ( ++next == end() ) ? " " : ", " );
}
os << "}";
}
template< typename T >
inline std::string Set<T>::toString() const {
std::ostringstream oss;
toStream( oss );
return oss.str();
}
//////////////////////
// Global Functions //
//////////////////////
template< typename T >
inline Set<T> setIntersection( const Set<T>& a, const Set<T>& b ) {
Set<T> result(a);
result &= b;
return result;
}
template< typename T >
inline Set<T> setUnion( const Set<T>& a, const Set<T>& b ) {
Set<T> result(a);
result += b;
return result;
}
template< typename T >
inline Set<T> setDifference( const Set<T>& a, const Set<T>& b ) {
Set<T> result(a);
result -= b;
return result;
}
template< typename T >
inline bool setIsEqual( const Set<T>& a, const Set<T>& b ) {
return a.isEqual(b);
}
template< typename T >
inline std::ostream& operator<<( std::ostream& os, const Set<T>& set ) {
set.toStream( os );
return os;
}
/// \cond internal
namespace set {
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 set
/// \endcond
} // namespace walberla
//======================================================================================================================
//
// Send/Recv Buffer Serialization Specialization
//
//======================================================================================================================
namespace walberla {
namespace mpi {
template< typename T, // Element type of SendBuffer
typename G, // Growth policy of SendBuffer
typename E >
inline mpi::GenericSendBuffer<T,G> & operator<<( mpi::GenericSendBuffer<T,G> & buffer, const walberla::Set<E> & set )
{
buffer.addDebugMarker( "se" );
buffer << set.get();
return buffer;
}
template< typename T, // Element type of RecvBuffer
typename E >
inline mpi::GenericRecvBuffer<T>& operator>>( mpi::GenericRecvBuffer<T> & buffer, walberla::Set<E> & set )
{
buffer.readDebugMarker( "se" );
buffer >> set.get();
return buffer;
}
template< typename T >
struct BufferSizeTrait< walberla::Set<T> > { static const bool constantSize = false; };
}
}