00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef SUNDANCE_SET_H
00032 #define SUNDANCE_SET_H
00033
00034 #include "SundanceDefs.hpp"
00035 #include "Teuchos_Array.hpp"
00036 #include <set>
00037 #include <algorithm>
00038
00039
00040
00041 namespace Sundance
00042 {
00043 using namespace Teuchos;
00044
00045
00046
00047
00048
00049 template<class Key, class Compare = std::less<Key> >
00050 class Set
00051 {
00052 public:
00053
00054 typedef typename std::set<Key, Compare>::iterator iterator;
00055 typedef typename std::set<Key, Compare>::const_iterator const_iterator;
00056 typedef typename std::set<Key, Compare>::reverse_iterator reverse_iterator;
00057 typedef typename std::set<Key, Compare>::const_reverse_iterator const_reverse_iterator;
00058
00059 typedef typename std::set<Key, Compare>::size_type size_type;
00060 typedef typename std::set<Key, Compare>::pointer pointer;
00061 typedef typename std::set<Key, Compare>::const_pointer const_pointer;
00062 typedef typename std::set<Key, Compare>::const_reference const_reference;
00063 typedef typename std::set<Key, Compare>::reference reference;
00064 typedef typename std::set<Key, Compare>::difference_type difference_type;
00065
00066
00067 Set() : set_() {;}
00068
00069
00070 Set(const std::set<Key>& s) : set_(s) {;}
00071
00072
00073 iterator begin() {return set_.begin();}
00074
00075
00076 const_iterator begin() const {return set_.begin();}
00077
00078
00079 iterator end() {return set_.end();}
00080
00081
00082 const_iterator end() const {return set_.end();}
00083
00084
00085 reverse_iterator rbegin() {return set_.rbegin();}
00086
00087
00088 const_reverse_iterator rbegin() const {return set_.rbegin();}
00089
00090
00091 reverse_iterator rend() {return set_.rend();}
00092
00093
00094 const_reverse_iterator rend() const {return set_.rend();}
00095
00096
00097 std::pair<iterator, bool> insert(const Key& x) {return set_.insert(x);}
00098
00099
00100 iterator insert(iterator pos, const Key& x) {return set_.insert(pos,x);}
00101
00102
00103 void erase(iterator pos) {set_.erase(pos);}
00104
00105
00106 void erase(const Key& x) {set_.erase(x);}
00107
00108
00109 void erase(iterator first, iterator last) {set_.erase(first, last);}
00110
00111
00112 void clear() {set_.clear();}
00113
00114
00115 iterator find(const Key& x) {return set_.find(x);}
00116
00117
00118 const_iterator find(const Key& x) const {return set_.find(x);}
00119
00120
00121 iterator lower_bound(const Key& x) {return set_.lower_bound(x);}
00122
00123
00124 const_iterator lower_bound(const Key& x) const {return set_.lower_bound(x);}
00125
00126
00127 iterator upper_bound(const Key& x) {return set_.upper_bound(x);}
00128
00129
00130 const_iterator upper_bound(const Key& x) const {return set_.upper_bound(x);}
00131
00132
00133 std::pair<iterator,iterator> equal_range(const Key& x)
00134 {return set_.equal_range(x);}
00135
00136
00137 std::pair<const_iterator,const_iterator> equal_range(const Key& x) const
00138 {return set_.equal_range(x);}
00139
00140
00141 int size() const {return set_.size();}
00142
00143
00144 int max_size() const {return set_.max_size();}
00145
00146
00147 bool empty() const {return set_.empty();}
00148
00149
00150 const std::set<Key, Compare>& set() const {return set_;}
00151
00152
00153 std::set<Key, Compare>& set() {return set_;}
00154
00155
00156 bool contains(const Key& key) const {return this->find(key) != this->end();}
00157
00158
00159 void put(const Key& key) {insert(key);}
00160
00161
00162 Array<Key> elements() const ;
00163
00164
00165 void elements(Array<Key>& keys) const ;
00166
00167
00168 void merge(const Set<Key, Compare>& other);
00169
00170
00171 Set<Key, Compare> intersection(const Set<Key, Compare>& other) const ;
00172
00173
00174 Set<Key, Compare> setUnion(const Set<Key, Compare>& other) const ;
00175
00176
00177 Set<Key, Compare> setDifference(const Set<Key, Compare>& other) const ;
00178
00179
00180 std::ostream& toStream(std::ostream& os) const ;
00181
00182
00183 std::string toString() const ;
00184
00185 private:
00186 std::set<Key, Compare> set_;
00187 };
00188
00189
00190 template<class Key, class Compare> inline
00191 Array<Key> Set<Key, Compare>::elements() const
00192 {
00193 Array<Key> rtn;
00194
00195 typename Set<Key, Compare>::const_iterator iter;
00196
00197 for (iter=this->begin(); iter != this->end(); iter++)
00198 {
00199 rtn.append(*iter);
00200 }
00201 return rtn;
00202 }
00203
00204
00205 template<class Key, class Compare> inline
00206 void Set<Key, Compare>::elements(Array<Key>& rtn) const
00207 {
00208 rtn.resize(0);
00209 typename Set<Key, Compare>::const_iterator iter;
00210
00211 for (iter=this->begin(); iter != this->end(); iter++)
00212 {
00213 rtn.append(*iter);
00214 }
00215 }
00216
00217 template<class Key, class Compare> inline
00218 void Set<Key, Compare>::merge(const Set<Key, Compare>& other)
00219 {
00220 typename Set<Key, Compare>::const_iterator iter;
00221
00222 for (iter=other.begin(); iter != other.end(); iter++)
00223 {
00224 put(*iter);
00225 }
00226 }
00227
00228 template<class Key, class Compare> inline
00229 Set<Key, Compare> Set<Key, Compare>::intersection(const Set<Key, Compare>& other) const
00230 {
00231 Set<Key, Compare> rtn;
00232
00233 set_intersection(this->begin(), this->end(),
00234 other.begin(), other.end(),
00235 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00236 return rtn;
00237 }
00238
00239 template<class Key, class Compare> inline
00240 Set<Key, Compare> Set<Key, Compare>::setUnion(const Set<Key, Compare>& other) const
00241 {
00242 Set<Key, Compare> rtn;
00243
00244 set_union(this->begin(), this->end(),
00245 other.begin(), other.end(),
00246 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00247 return rtn;
00248 }
00249
00250 template<class Key, class Compare> inline
00251 Set<Key, Compare> Set<Key, Compare>::setDifference(const Set<Key, Compare>& other) const
00252 {
00253 Set<Key, Compare> rtn;
00254
00255 set_difference(this->begin(), this->end(),
00256 other.begin(), other.end(),
00257 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00258 return rtn;
00259 }
00260
00261 template<class Key, class Compare> inline
00262 std::ostream& Set<Key, Compare>::toStream(std::ostream& os) const
00263 {
00264 typename Set<Key, Compare>::const_iterator iter;
00265
00266 int k = 0;
00267 os << "{";
00268 for (iter=this->begin(); iter != this->end(); iter++, k++)
00269 {
00270 os << *iter;
00271 if (k<(this->size()-1)) os << ", ";
00272 }
00273 os << "}";
00274
00275 return os;
00276 }
00277
00278 template<class Key, class Compare> inline
00279 string Set<Key, Compare>::toString() const
00280 {
00281 std::ostringstream os;
00282 os << *this;
00283 return os.str();
00284 }
00285
00286
00287 template<class Key> inline
00288 Set<Key> makeSet(const Key& k)
00289 {
00290 Set<Key> rtn;
00291 rtn.put(k);
00292 return rtn;
00293 }
00294
00295
00296 template<class Key> inline
00297 Set<Key> makeSet(const Key& k1, const Key& k2)
00298 {
00299 Set<Key> rtn = makeSet<Key>(k1);
00300 rtn.put(k2);
00301 return rtn;
00302 }
00303
00304
00305 template<class Key> inline
00306 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3)
00307 {
00308 Set<Key> rtn = makeSet<Key>(k1, k2);
00309 rtn.put(k3);
00310 return rtn;
00311 }
00312
00313
00314 template<class Key> inline
00315 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4)
00316 {
00317 Set<Key> rtn = makeSet<Key>(k1, k2, k3);
00318 rtn.put(k4);
00319 return rtn;
00320 }
00321
00322
00323 template<class Key> inline
00324 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00325 const Key& k5)
00326 {
00327 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4);
00328 rtn.put(k5);
00329 return rtn;
00330 }
00331
00332
00333 template<class Key> inline
00334 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00335 const Key& k5, const Key& k6)
00336 {
00337 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5);
00338 rtn.put(k6);
00339 return rtn;
00340 }
00341
00342
00343 template<class Key> inline
00344 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00345 const Key& k5, const Key& k6, const Key& k7)
00346 {
00347 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6);
00348 rtn.put(k7);
00349 return rtn;
00350 }
00351
00352
00353 template<class Key> inline
00354 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00355 const Key& k5, const Key& k6, const Key& k7, const Key& k8)
00356 {
00357 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6, k7);
00358 rtn.put(k8);
00359 return rtn;
00360 }
00361
00362
00363
00364 template <typename Key, typename Compare> bool operator==(
00365 const Set<Key, Compare>& L,
00366 const Set<Key, Compare>& R)
00367 {
00368 return L.set() == R.set();
00369 }
00370
00371
00372 template <typename Key, typename Compare> bool operator!=(
00373 const Set<Key, Compare>& L,
00374 const Set<Key, Compare>& R)
00375 {
00376 return L.set() != R.set();
00377 }
00378
00379
00380 template <typename Key, typename Compare> bool operator<=(
00381 const Set<Key, Compare>& L,
00382 const Set<Key, Compare>& R)
00383 {
00384 return L.set() <= R.set();
00385 }
00386
00387
00388 template <typename Key, typename Compare> bool operator<(
00389 const Set<Key, Compare>& L,
00390 const Set<Key, Compare>& R)
00391 {
00392 return L.set() < R.set();
00393 }
00394
00395
00396
00397 template <typename Key, typename Compare> bool operator>(
00398 const Set<Key, Compare>& L,
00399 const Set<Key, Compare>& R)
00400 {
00401 return L.set() > R.set();
00402 }
00403
00404
00405 template <typename Key, typename Compare> bool operator>=(
00406 const Set<Key, Compare>& L,
00407 const Set<Key, Compare>& R)
00408 {
00409 return L.set() >= R.set();
00410 }
00411
00412
00413
00414
00415 }
00416
00417 namespace std
00418 {
00419
00420 template<class Key, class Compare> inline
00421 ostream& operator<<(std::ostream& os, const Sundance::Set<Key, Compare>& m)
00422 {return m.toStream(os);}
00423 }
00424
00425
00426 #endif