42
42
namespace SundanceUtils
44
using namespace Teuchos;
47
* Extension of STL set, adding some nicer syntax
48
* and an iostream insertion operator.
50
template<class Key, class Compare = std::less<Key> >
51
class Set : public std::set<Key, Compare>
55
Set() : std::set<Key, Compare>() {;}
57
/** Test whether the specified key is present in the set */
58
bool contains(const Key& key) const {return this->find(key) != this->end();}
60
/** Put a new entry in the set */
61
void put(const Key& key) {insert(key);}
63
/** Write into an array */
64
Array<Key> elements() const ;
67
void elements(Array<Key>& keys) const ;
70
void merge(const Set<Key, Compare>& other);
73
Set<Key, Compare> intersection(const Set<Key, Compare>& other) const ;
76
Set<Key, Compare> setUnion(const Set<Key, Compare>& other) const ;
79
Set<Key, Compare> setDifference(const Set<Key, Compare>& other) const ;
82
std::ostream& toStream(std::ostream& os) const ;
85
string toString() const ;
89
template<class Key, class Compare> inline
90
Array<Key> Set<Key, Compare>::elements() const
94
typename Set<Key, Compare>::const_iterator iter;
96
for (iter=this->begin(); iter != this->end(); iter++)
104
template<class Key, class Compare> inline
105
void Set<Key, Compare>::elements(Array<Key>& rtn) const
108
typename Set<Key, Compare>::const_iterator iter;
110
for (iter=this->begin(); iter != this->end(); iter++)
116
template<class Key, class Compare> inline
117
void Set<Key, Compare>::merge(const Set<Key, Compare>& other)
119
typename Set<Key, Compare>::const_iterator iter;
121
for (iter=other.begin(); iter != other.end(); iter++)
127
template<class Key, class Compare> inline
128
Set<Key, Compare> Set<Key, Compare>::intersection(const Set<Key, Compare>& other) const
130
Set<Key, Compare> rtn;
132
set_intersection(this->begin(), this->end(),
133
other.begin(), other.end(),
134
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
138
template<class Key, class Compare> inline
139
Set<Key, Compare> Set<Key, Compare>::setUnion(const Set<Key, Compare>& other) const
141
Set<Key, Compare> rtn;
143
set_union(this->begin(), this->end(),
144
other.begin(), other.end(),
145
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
149
template<class Key, class Compare> inline
150
Set<Key, Compare> Set<Key, Compare>::setDifference(const Set<Key, Compare>& other) const
152
Set<Key, Compare> rtn;
154
set_difference(this->begin(), this->end(),
155
other.begin(), other.end(),
156
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
160
template<class Key, class Compare> inline
161
std::ostream& Set<Key, Compare>::toStream(std::ostream& os) const
163
typename Set<Key, Compare>::const_iterator iter;
167
for (iter=this->begin(); iter != this->end(); iter++, k++)
170
if (k<(this->size()-1)) os << ", ";
177
template<class Key, class Compare> inline
178
string Set<Key, Compare>::toString() const
180
std::ostringstream os;
185
/** \relates Set Creates a set */
186
template<class Key> inline
187
Set<Key> makeSet(const Key& k)
194
/** \relates Set Creates a set */
195
template<class Key> inline
196
Set<Key> makeSet(const Key& k1, const Key& k2)
198
Set<Key> rtn = makeSet<Key>(k1);
203
/** \relates Set Creates a set */
204
template<class Key> inline
205
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3)
207
Set<Key> rtn = makeSet<Key>(k1, k2);
212
/** \relates Set Creates a set */
213
template<class Key> inline
214
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4)
216
Set<Key> rtn = makeSet<Key>(k1, k2, k3);
221
/** \relates Set Creates a set */
222
template<class Key> inline
223
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
226
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4);
231
/** \relates Set Creates a set */
232
template<class Key> inline
233
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
234
const Key& k5, const Key& k6)
236
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5);
241
/** \relates Set Creates a set */
242
template<class Key> inline
243
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
244
const Key& k5, const Key& k6, const Key& k7)
246
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6);
251
/** \relates Set Creates a set */
252
template<class Key> inline
253
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
254
const Key& k5, const Key& k6, const Key& k7, const Key& k8)
256
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6, k7);
44
using namespace Teuchos;
47
* Extension of STL set, adding some nicer syntax
48
* and an iostream insertion operator.
50
template<class Key, class Compare = std::less<Key> >
55
typedef typename std::set<Key, Compare>::iterator iterator;
56
typedef typename std::set<Key, Compare>::const_iterator const_iterator;
57
typedef typename std::set<Key, Compare>::reverse_iterator reverse_iterator;
58
typedef typename std::set<Key, Compare>::const_reverse_iterator const_reverse_iterator;
60
typedef typename std::set<Key, Compare>::size_type size_type;
61
typedef typename std::set<Key, Compare>::pointer pointer;
62
typedef typename std::set<Key, Compare>::const_pointer const_pointer;
63
typedef typename std::set<Key, Compare>::const_reference const_reference;
64
typedef typename std::set<Key, Compare>::reference reference;
65
typedef typename std::set<Key, Compare>::difference_type difference_type;
71
Set(const std::set<Key>& s) : set_(s) {;}
74
iterator begin() {return set_.begin();}
77
const_iterator begin() const {return set_.begin();}
80
iterator end() {return set_.end();}
83
const_iterator end() const {return set_.end();}
86
reverse_iterator rbegin() {return set_.rbegin();}
89
const_reverse_iterator rbegin() const {return set_.rbegin();}
92
reverse_iterator rend() {return set_.rend();}
95
const_reverse_iterator rend() const {return set_.rend();}
98
std::pair<iterator, bool> insert(const Key& x) {return set_.insert(x);}
101
iterator insert(iterator pos, const Key& x) {return set_.insert(pos,x);}
104
void erase(iterator pos) {set_.erase(pos);}
107
void erase(const Key& x) {set_.erase(x);}
110
void erase(iterator first, iterator last) {set_.erase(first, last);}
113
void clear() {set_.clear();}
116
iterator find(const Key& x) {return set_.find(x);}
119
const_iterator find(const Key& x) const {return set_.find(x);}
122
iterator lower_bound(const Key& x) {return set_.lower_bound(x);}
125
const_iterator lower_bound(const Key& x) const {return set_.lower_bound(x);}
128
iterator upper_bound(const Key& x) {return set_.upper_bound(x);}
131
const_iterator upper_bound(const Key& x) const {return set_.upper_bound(x);}
134
std::pair<iterator,iterator> equal_range(const Key& x)
135
{return set_.equal_range(x);}
138
std::pair<const_iterator,const_iterator> equal_range(const Key& x) const
139
{return set_.equal_range(x);}
142
size_type size() const {return set_.size();}
145
size_type max_size() const {return set_.max_size();}
148
bool empty() const {return set_.empty();}
151
const std::set<Key, Compare>& set() const {return set_;}
154
std::set<Key, Compare>& set() {return set_;}
156
/** Test whether the specified key is present in the set */
157
bool contains(const Key& key) const {return this->find(key) != this->end();}
159
/** Put a new entry in the set */
160
void put(const Key& key) {insert(key);}
162
/** Write into an array */
163
Array<Key> elements() const ;
166
void elements(Array<Key>& keys) const ;
169
void merge(const Set<Key, Compare>& other);
172
Set<Key, Compare> intersection(const Set<Key, Compare>& other) const ;
175
Set<Key, Compare> setUnion(const Set<Key, Compare>& other) const ;
178
Set<Key, Compare> setDifference(const Set<Key, Compare>& other) const ;
181
std::ostream& toStream(std::ostream& os) const ;
184
string toString() const ;
187
std::set<Key, Compare> set_;
191
template<class Key, class Compare> inline
192
Array<Key> Set<Key, Compare>::elements() const
196
typename Set<Key, Compare>::const_iterator iter;
198
for (iter=this->begin(); iter != this->end(); iter++)
206
template<class Key, class Compare> inline
207
void Set<Key, Compare>::elements(Array<Key>& rtn) const
210
typename Set<Key, Compare>::const_iterator iter;
212
for (iter=this->begin(); iter != this->end(); iter++)
218
template<class Key, class Compare> inline
219
void Set<Key, Compare>::merge(const Set<Key, Compare>& other)
221
typename Set<Key, Compare>::const_iterator iter;
223
for (iter=other.begin(); iter != other.end(); iter++)
229
template<class Key, class Compare> inline
230
Set<Key, Compare> Set<Key, Compare>::intersection(const Set<Key, Compare>& other) const
232
Set<Key, Compare> rtn;
234
set_intersection(this->begin(), this->end(),
235
other.begin(), other.end(),
236
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
240
template<class Key, class Compare> inline
241
Set<Key, Compare> Set<Key, Compare>::setUnion(const Set<Key, Compare>& other) const
243
Set<Key, Compare> rtn;
245
set_union(this->begin(), this->end(),
246
other.begin(), other.end(),
247
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
251
template<class Key, class Compare> inline
252
Set<Key, Compare> Set<Key, Compare>::setDifference(const Set<Key, Compare>& other) const
254
Set<Key, Compare> rtn;
256
set_difference(this->begin(), this->end(),
257
other.begin(), other.end(),
258
std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
262
template<class Key, class Compare> inline
263
std::ostream& Set<Key, Compare>::toStream(std::ostream& os) const
265
typename Set<Key, Compare>::const_iterator iter;
269
for (iter=this->begin(); iter != this->end(); iter++, k++)
272
if (k<(this->size()-1)) os << ", ";
279
template<class Key, class Compare> inline
280
string Set<Key, Compare>::toString() const
282
std::ostringstream os;
287
/** \relates Set Creates a set */
288
template<class Key> inline
289
Set<Key> makeSet(const Key& k)
296
/** \relates Set Creates a set */
297
template<class Key> inline
298
Set<Key> makeSet(const Key& k1, const Key& k2)
300
Set<Key> rtn = makeSet<Key>(k1);
305
/** \relates Set Creates a set */
306
template<class Key> inline
307
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3)
309
Set<Key> rtn = makeSet<Key>(k1, k2);
314
/** \relates Set Creates a set */
315
template<class Key> inline
316
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4)
318
Set<Key> rtn = makeSet<Key>(k1, k2, k3);
323
/** \relates Set Creates a set */
324
template<class Key> inline
325
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
328
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4);
333
/** \relates Set Creates a set */
334
template<class Key> inline
335
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
336
const Key& k5, const Key& k6)
338
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5);
343
/** \relates Set Creates a set */
344
template<class Key> inline
345
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
346
const Key& k5, const Key& k6, const Key& k7)
348
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6);
353
/** \relates Set Creates a set */
354
template<class Key> inline
355
Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
356
const Key& k5, const Key& k6, const Key& k7, const Key& k8)
358
Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6, k7);
365
template <typename Key, typename Compare> bool operator==(
366
const Set<Key, Compare>& L,
367
const Set<Key, Compare>& R)
369
return L.set() == R.set();
373
template <typename Key, typename Compare> bool operator!=(
374
const Set<Key, Compare>& L,
375
const Set<Key, Compare>& R)
377
return L.set() != R.set();
381
template <typename Key, typename Compare> bool operator<=(
382
const Set<Key, Compare>& L,
383
const Set<Key, Compare>& R)
385
return L.set() <= R.set();
389
template <typename Key, typename Compare> bool operator<(
390
const Set<Key, Compare>& L,
391
const Set<Key, Compare>& R)
393
return L.set() < R.set();
398
template <typename Key, typename Compare> bool operator>(
399
const Set<Key, Compare>& L,
400
const Set<Key, Compare>& R)
402
return L.set() > R.set();
406
template <typename Key, typename Compare> bool operator>=(
407
const Set<Key, Compare>& L,
408
const Set<Key, Compare>& R)
410
return L.set() >= R.set();
265
/** \relates SundanceUtils::Set */
266
template<class Key, class Compare> inline
267
ostream& operator<<(ostream& os, const SundanceUtils::Set<Key, Compare>& m)
268
{return m.toStream(os);}
420
/** \relates SundanceUtils::Set */
421
template<class Key, class Compare> inline
422
ostream& operator<<(ostream& os, const SundanceUtils::Set<Key, Compare>& m)
423
{return m.toStream(os);}
271
426
#endif /* DOXYGEN_DEVELOPER_ONLY */