Go to the documentation of this file.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_MULTISET_H
00043 #define SUNDANCE_MULTISET_H
00044
00045 #include "SundanceDefs.hpp"
00046 #include "SundanceSet.hpp"
00047 #include "Teuchos_Array.hpp"
00048 #include <set>
00049
00050
00051 namespace Sundance
00052 {
00053 using namespace Teuchos;
00054
00055
00056
00057
00058
00059 template<class Key>
00060 class MultiSet : public std::multiset<Key>
00061 {
00062 public:
00063
00064 MultiSet() : std::multiset<Key>() {;}
00065
00066
00067 bool contains(const Key& key) const {return this->find(key) != this->end();}
00068
00069
00070 void put(const Key& key) {this->insert(key);}
00071
00072
00073 Array<Key> elements() const ;
00074
00075
00076 std::ostream& toStream(std::ostream& os) const ;
00077
00078
00079 MultiSet<Key> merge(const MultiSet<Key>& other) const ;
00080
00081
00082
00083 void mergeFrom(const MultiSet<Key>& other) ;
00084
00085
00086 Set<Key> toSet() const ;
00087
00088
00089 std::string toString() const ;
00090
00091
00092 bool operator==(const MultiSet<int>& other) const
00093 {
00094 return !((*this) < other || other < (*this));
00095 }
00096 };
00097
00098
00099 template<class Key> inline
00100 Array<Key> MultiSet<Key>::elements() const
00101 {
00102 Array<Key> rtn;
00103
00104 typename MultiSet<Key>::const_iterator iter;
00105
00106 for (iter=this->begin(); iter != this->end(); iter++)
00107 {
00108 rtn.append(*iter);
00109 }
00110 return rtn;
00111 }
00112
00113 template<class Key> inline
00114 MultiSet<Key> MultiSet<Key>::merge(const MultiSet<Key>& other) const
00115 {
00116 MultiSet<Key> rtn = *this;
00117
00118 typename MultiSet<Key>::const_iterator iter;
00119
00120 for (iter=other.begin(); iter != other.end(); iter++)
00121 {
00122 rtn.put(*iter);
00123 }
00124 return rtn;
00125 }
00126
00127 template<class Key> inline
00128 void MultiSet<Key>::mergeFrom(const MultiSet<Key>& other)
00129 {
00130 typename MultiSet<Key>::const_iterator iter;
00131
00132 for (iter=other.begin(); iter != other.end(); iter++)
00133 {
00134 put(*iter);
00135 }
00136 }
00137
00138
00139 template<class Key> inline
00140 Set<Key> MultiSet<Key>::toSet() const
00141 {
00142 Set<int> rtn;
00143 typename MultiSet<Key>::const_iterator iter;
00144
00145 for (iter=this->begin(); iter != this->end(); iter++)
00146 {
00147 rtn.put(*iter);
00148 }
00149 return rtn;
00150 }
00151
00152
00153
00154 template<class Key> inline
00155 std::ostream& MultiSet<Key>::toStream(std::ostream& os) const
00156 {
00157 typename MultiSet<Key>::const_iterator iter;
00158
00159 int k = 0;
00160 os << "{";
00161 for (iter=this->begin(); iter != this->end(); iter++, k++)
00162 {
00163 os << *iter;
00164 if (k<((int) this->size()-1)) os << ", ";
00165 }
00166 os << "}";
00167
00168 return os;
00169 }
00170
00171 template<class Key> inline
00172 string MultiSet<Key>::toString() const
00173 {
00174 std::ostringstream os;
00175 os << *this;
00176 return os.str();
00177 }
00178
00179
00180
00181 template<class Key> inline
00182 MultiSet<Key> makeMultiSet(const Key& k)
00183 {
00184 MultiSet<Key> rtn;
00185 rtn.put(k);
00186 return rtn;
00187 }
00188
00189
00190 template<class Key> inline
00191 MultiSet<Key> makeMultiSet(const Key& k1, const Key& k2)
00192 {
00193 MultiSet<Key> rtn = makeMultiSet<Key>(k1);
00194 rtn.put(k2);
00195 return rtn;
00196 }
00197
00198
00199 template<class Key> inline
00200 MultiSet<Key> makeMultiSet(const Key& k1, const Key& k2, const Key& k3)
00201 {
00202 MultiSet<Key> rtn = makeMultiSet<Key>(k1, k2);
00203 rtn.put(k3);
00204 return rtn;
00205 }
00206
00207
00208 template<class Key> inline
00209 MultiSet<Key> makeMultiSet(const Key& k1, const Key& k2,
00210 const Key& k3, const Key& k4)
00211 {
00212 MultiSet<Key> rtn = makeMultiSet<Key>(k1, k2, k3);
00213 rtn.put(k4);
00214 return rtn;
00215 }
00216
00217
00218 template<class Key> inline
00219 MultiSet<Key> makeMultiSet(const Key& k1, const Key& k2,
00220 const Key& k3, const Key& k4,
00221 const Key& k5)
00222 {
00223 MultiSet<Key> rtn = makeMultiSet<Key>(k1, k2, k3, k4);
00224 rtn.put(k5);
00225 return rtn;
00226 }
00227
00228
00229 template<class Key> inline
00230 MultiSet<Key> makeMultiSet(const Key& k1, const Key& k2,
00231 const Key& k3, const Key& k4,
00232 const Key& k5, const Key& k6)
00233 {
00234 MultiSet<Key> rtn = makeMultiSet<Key>(k1, k2, k3, k4, k5);
00235 rtn.put(k6);
00236 return rtn;
00237 }
00238
00239
00240 template<class Key> inline
00241 MultiSet<Key> makeMultiSet(const Array<Key>& k)
00242 {
00243 MultiSet<Key> rtn;
00244 for (int i=0; i<k.size(); i++) rtn.put(k[i]);
00245 return rtn;
00246 }
00247
00248 }
00249
00250 namespace std
00251 {
00252
00253
00254
00255 template<class Key> inline
00256 ostream& operator<<(std::ostream& os, const Sundance::MultiSet<Key>& m)
00257 {return m.toStream(os);}
00258 }
00259
00260
00261 #endif
00262