SundanceMap.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_MAP_H
00032 #define SUNDANCE_MAP_H
00033 
00034 #include "SundanceDefs.hpp"
00035 #include <map>
00036 
00037 #ifndef DOXYGEN_DEVELOPER_ONLY
00038 
00039 namespace Sundance
00040 {
00041   using namespace Teuchos;
00042 
00043   /**
00044    * Extension of STL map, adding some nicer put/get/contains syntax 
00045    * and an iostream insertion operator.
00046    */
00047   template<class Key, class Value, class Compare = std::less<Key> >
00048     class Map : public std::map<Key, Value, Compare>
00049     {
00050     public:
00051       /** */
00052       Map() : std::map<Key,Value,Compare>() {;}
00053 
00054       /** Test whether the specified key is present in the map */
00055       inline bool containsKey(const Key& key) const {return this->find(key) != this->end();}
00056 
00057       /** Put a new (key, value) entry in the map */
00058       inline void put(const Key& key, const Value& value)
00059         {this->operator[](key) = value;}
00060 
00061       /** Look up value and return a read-only reference */
00062       inline const Value& get(const Key& key) const
00063         {return (*(this->find)(key)).second;}
00064 
00065       /** Look up value and return a modifiable reference */
00066       inline Value& get(const Key& key) 
00067         {return (*(this->find)(key)).second;}
00068     };
00069 
00070 }
00071 
00072 namespace std
00073 {
00074    /** \relates Sundance::Map 
00075     * Write to a stream
00076     */
00077   template<class Key, class Value, class Compare> inline
00078     std::ostream& operator<<(std::ostream& os, const Sundance::Map<Key, Value, Compare>& m)
00079     {
00080       typename Sundance::Map<Key, Value, Compare>::const_iterator iter;
00081 
00082       os << "Map[";
00083       int k = 0 ;
00084       for (iter=m.begin(); iter != m.end(); iter++, k++)
00085         {
00086           os << "{" << (*iter).first << ", " << (*iter).second << "}";
00087           if (k < (int) m.size()-1) os << ", ";
00088         }
00089       os << "]";
00090       return os;
00091     }
00092 }
00093 
00094 #endif /* DOXYGEN_DEVELOPER_ONLY */
00095 #endif

Site Contact