SundanceMultiSet.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
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  * Extension of STL multiset, adding some nicer syntax 
00046  * and an iostream insertion operator.
00047  */
00048 template<class Key>
00049 class MultiSet : public std::multiset<Key>
00050 {
00051 public:
00052   /** */
00053   MultiSet() : std::multiset<Key>() {;}
00054 
00055   /** Test whether the specified key is present in the set */
00056   bool contains(const Key& key) const {return this->find(key) != this->end();}
00057 
00058   /** Put a new entry in the map */
00059   void put(const Key& key) {this->insert(key);}
00060 
00061   /** Write into an array */
00062   Array<Key> elements() const ;
00063 
00064   /** Write to stream */
00065   std::ostream& toStream(std::ostream& os) const ;
00066 
00067   /** Merge with another multiset, returning the merged set */
00068   MultiSet<Key> merge(const MultiSet<Key>& other) const ;
00069 
00070   /** Take another set and merge into this one, overwriting the original
00071    * with the merged set */
00072   void mergeFrom(const MultiSet<Key>& other) ;
00073 
00074   /** Write into a set, i.e., collapsing repeated entries */
00075   Set<Key> toSet() const ;
00076 
00077   /** Write to a std::string */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates Sundance::MultiSet 
00242  * Write to a stream
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 

Site Contact