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 template <typename InputIterator>
00104 void insert(InputIterator first, InputIterator last) {set_.insert(first,last);}
00105
00106
00107 void erase(iterator pos) {set_.erase(pos);}
00108
00109
00110 void erase(const Key& x) {set_.erase(x);}
00111
00112
00113 void erase(iterator first, iterator last) {set_.erase(first, last);}
00114
00115
00116 void clear() {set_.clear();}
00117
00118
00119 iterator find(const Key& x) {return set_.find(x);}
00120
00121
00122 const_iterator find(const Key& x) const {return set_.find(x);}
00123
00124
00125 iterator lower_bound(const Key& x) {return set_.lower_bound(x);}
00126
00127
00128 const_iterator lower_bound(const Key& x) const {return set_.lower_bound(x);}
00129
00130
00131 iterator upper_bound(const Key& x) {return set_.upper_bound(x);}
00132
00133
00134 const_iterator upper_bound(const Key& x) const {return set_.upper_bound(x);}
00135
00136
00137 std::pair<iterator,iterator> equal_range(const Key& x)
00138 {return set_.equal_range(x);}
00139
00140
00141 std::pair<const_iterator,const_iterator> equal_range(const Key& x) const
00142 {return set_.equal_range(x);}
00143
00144
00145 int size() const {return set_.size();}
00146
00147
00148 int max_size() const {return set_.max_size();}
00149
00150
00151 bool empty() const {return set_.empty();}
00152
00153
00154 const std::set<Key, Compare>& set() const {return set_;}
00155
00156
00157 std::set<Key, Compare>& set() {return set_;}
00158
00159
00160 bool contains(const Key& key) const {return this->find(key) != this->end();}
00161
00162
00163 void put(const Key& key) {insert(key);}
00164
00165
00166 Array<Key> elements() const ;
00167
00168
00169 void elements(Array<Key>& keys) const ;
00170
00171
00172 void merge(const Set<Key, Compare>& other);
00173
00174
00175 Set<Key, Compare> intersection(const Set<Key, Compare>& other) const ;
00176
00177
00178 Set<Key, Compare> setUnion(const Set<Key, Compare>& other) const ;
00179
00180
00181 Set<Key, Compare> setDifference(const Set<Key, Compare>& other) const ;
00182
00183
00184 std::ostream& toStream(std::ostream& os) const ;
00185
00186
00187 std::string toString() const ;
00188
00189 private:
00190 std::set<Key, Compare> set_;
00191 };
00192
00193
00194 template<class Key, class Compare> inline
00195 Array<Key> Set<Key, Compare>::elements() const
00196 {
00197 Array<Key> rtn;
00198
00199 typename Set<Key, Compare>::const_iterator iter;
00200
00201 for (iter=this->begin(); iter != this->end(); iter++)
00202 {
00203 rtn.append(*iter);
00204 }
00205 return rtn;
00206 }
00207
00208
00209 template<class Key, class Compare> inline
00210 void Set<Key, Compare>::elements(Array<Key>& rtn) const
00211 {
00212 rtn.resize(0);
00213 typename Set<Key, Compare>::const_iterator iter;
00214
00215 for (iter=this->begin(); iter != this->end(); iter++)
00216 {
00217 rtn.append(*iter);
00218 }
00219 }
00220
00221 template<class Key, class Compare> inline
00222 void Set<Key, Compare>::merge(const Set<Key, Compare>& other)
00223 {
00224 typename Set<Key, Compare>::const_iterator iter;
00225
00226 for (iter=other.begin(); iter != other.end(); iter++)
00227 {
00228 put(*iter);
00229 }
00230 }
00231
00232 template<class Key, class Compare> inline
00233 Set<Key, Compare> Set<Key, Compare>::intersection(const Set<Key, Compare>& other) const
00234 {
00235 Set<Key, Compare> rtn;
00236
00237 set_intersection(this->begin(), this->end(),
00238 other.begin(), other.end(),
00239 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00240 return rtn;
00241 }
00242
00243 template<class Key, class Compare> inline
00244 Set<Key, Compare> Set<Key, Compare>::setUnion(const Set<Key, Compare>& other) const
00245 {
00246 Set<Key, Compare> rtn;
00247
00248 set_union(this->begin(), this->end(),
00249 other.begin(), other.end(),
00250 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00251 return rtn;
00252 }
00253
00254 template<class Key, class Compare> inline
00255 Set<Key, Compare> Set<Key, Compare>::setDifference(const Set<Key, Compare>& other) const
00256 {
00257 Set<Key, Compare> rtn;
00258
00259 set_difference(this->begin(), this->end(),
00260 other.begin(), other.end(),
00261 std::insert_iterator<Set<Key, Compare> >(rtn, rtn.begin()));
00262 return rtn;
00263 }
00264
00265 template<class Key, class Compare> inline
00266 std::ostream& Set<Key, Compare>::toStream(std::ostream& os) const
00267 {
00268 typename Set<Key, Compare>::const_iterator iter;
00269
00270 int k = 0;
00271 os << "{";
00272 for (iter=this->begin(); iter != this->end(); iter++, k++)
00273 {
00274 os << *iter;
00275 if (k<(this->size()-1)) os << ", ";
00276 }
00277 os << "}";
00278
00279 return os;
00280 }
00281
00282 template<class Key, class Compare> inline
00283 string Set<Key, Compare>::toString() const
00284 {
00285 std::ostringstream os;
00286 os << *this;
00287 return os.str();
00288 }
00289
00290
00291 template<class Key> inline
00292 Set<Key> makeSet(const Array<Key>& k)
00293 {
00294 Set<Key> rtn;
00295 for (int i=0; i<k.size(); i++) rtn.put(k[i]);
00296 return rtn;
00297 }
00298
00299
00300 template<class Key> inline
00301 Set<Key> makeSet(const Key& k)
00302 {
00303 Set<Key> rtn;
00304 rtn.put(k);
00305 return rtn;
00306 }
00307
00308
00309 template<class Key> inline
00310 Set<Key> makeSet(const Key& k1, const Key& k2)
00311 {
00312 Set<Key> rtn = makeSet<Key>(k1);
00313 rtn.put(k2);
00314 return rtn;
00315 }
00316
00317
00318 template<class Key> inline
00319 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3)
00320 {
00321 Set<Key> rtn = makeSet<Key>(k1, k2);
00322 rtn.put(k3);
00323 return rtn;
00324 }
00325
00326
00327 template<class Key> inline
00328 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4)
00329 {
00330 Set<Key> rtn = makeSet<Key>(k1, k2, k3);
00331 rtn.put(k4);
00332 return rtn;
00333 }
00334
00335
00336 template<class Key> inline
00337 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00338 const Key& k5)
00339 {
00340 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4);
00341 rtn.put(k5);
00342 return rtn;
00343 }
00344
00345
00346 template<class Key> inline
00347 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00348 const Key& k5, const Key& k6)
00349 {
00350 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5);
00351 rtn.put(k6);
00352 return rtn;
00353 }
00354
00355
00356 template<class Key> inline
00357 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00358 const Key& k5, const Key& k6, const Key& k7)
00359 {
00360 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6);
00361 rtn.put(k7);
00362 return rtn;
00363 }
00364
00365
00366 template<class Key> inline
00367 Set<Key> makeSet(const Key& k1, const Key& k2, const Key& k3, const Key& k4,
00368 const Key& k5, const Key& k6, const Key& k7, const Key& k8)
00369 {
00370 Set<Key> rtn = makeSet<Key>(k1, k2, k3, k4, k5, k6, k7);
00371 rtn.put(k8);
00372 return rtn;
00373 }
00374
00375
00376
00377 template <typename Key, typename Compare> bool operator==(
00378 const Set<Key, Compare>& L,
00379 const Set<Key, Compare>& R)
00380 {
00381 return L.set() == R.set();
00382 }
00383
00384
00385 template <typename Key, typename Compare> bool operator!=(
00386 const Set<Key, Compare>& L,
00387 const Set<Key, Compare>& R)
00388 {
00389 return L.set() != R.set();
00390 }
00391
00392
00393 template <typename Key, typename Compare> bool operator<=(
00394 const Set<Key, Compare>& L,
00395 const Set<Key, Compare>& R)
00396 {
00397 return L.set() <= R.set();
00398 }
00399
00400
00401 template <typename Key, typename Compare> bool operator<(
00402 const Set<Key, Compare>& L,
00403 const Set<Key, Compare>& R)
00404 {
00405 return L.set() < R.set();
00406 }
00407
00408
00409
00410 template <typename Key, typename Compare> bool operator>(
00411 const Set<Key, Compare>& L,
00412 const Set<Key, Compare>& R)
00413 {
00414 return L.set() > R.set();
00415 }
00416
00417
00418 template <typename Key, typename Compare> bool operator>=(
00419 const Set<Key, Compare>& L,
00420 const Set<Key, Compare>& R)
00421 {
00422 return L.set() >= R.set();
00423 }
00424
00425
00426
00427
00428 }
00429
00430 namespace std
00431 {
00432
00433 template<class Key, class Compare> inline
00434 ostream& operator<<(std::ostream& os, const Sundance::Set<Key, Compare>& m)
00435 {return m.toStream(os);}
00436 }
00437
00438
00439 #endif