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