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