SundanceMultiSet.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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  * Extension of STL multiset, adding some nicer syntax 
00057  * and an iostream insertion operator.
00058  */
00059 template<class Key>
00060 class MultiSet : public std::multiset<Key>
00061 {
00062 public:
00063   /** */
00064   MultiSet() : std::multiset<Key>() {;}
00065 
00066   /** Test whether the specified key is present in the set */
00067   bool contains(const Key& key) const {return this->find(key) != this->end();}
00068 
00069   /** Put a new entry in the map */
00070   void put(const Key& key) {this->insert(key);}
00071 
00072   /** Write into an array */
00073   Array<Key> elements() const ;
00074 
00075   /** Write to stream */
00076   std::ostream& toStream(std::ostream& os) const ;
00077 
00078   /** Merge with another multiset, returning the merged set */
00079   MultiSet<Key> merge(const MultiSet<Key>& other) const ;
00080 
00081   /** Take another set and merge into this one, overwriting the original
00082    * with the merged set */
00083   void mergeFrom(const MultiSet<Key>& other) ;
00084 
00085   /** Write into a set, i.e., collapsing repeated entries */
00086   Set<Key> toSet() const ;
00087 
00088   /** Write to a std::string */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \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)
00222 {
00223   MultiSet<Key> rtn = makeMultiSet<Key>(k1, k2, k3, k4);
00224   rtn.put(k5);
00225   return rtn;
00226 }
00227 
00228 /** \relates MultiSet Create a multiset */
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 /** \relates MultiSet Create a multiset */
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 /** \relates Sundance::MultiSet 
00253  * Write to a stream
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 

Site Contact