| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# Map.h

Go to the documentation of this file.

    00001 // $Id: Map.h,v 1.6 2007/12/19 15:42:56 marcocle Exp $
    00002 // ============================================================================
    00003 // CVS tag $Name:  $, version $Revision: 1.6 $
    00004 // ============================================================================
    00005 #ifndef GAUDIKERNEL_MAP_H
    00006 #define GAUDIKERNEL_MAP_H 1
    00007 // ============================================================================
    00008 // Include files
    00009 // ============================================================================
    00010 // STD & STL
    00011 // ============================================================================
    00012 #include <map>
    00013 // ============================================================================
    00014
    00015
    00016 namespace GaudiUtils
    00017 {
    00029   template <typename K, typename T, typename M = std::map<K,T> >
    00030   class Map
    00031   {
    00032   public:
    00033
    00034     // ---- types
    00035     typedef M                      map_type;
    00036     typedef K                      key_type;
    00037     typedef T                      mapped_type;
    00038
    00039     // -- from unary_function
    00040     typedef K                      argument_type;
    00041     typedef T                      result_type;
    00042     typedef std::pair<const K,T>   value_type;
    00043
    00044     typedef typename map_type::size_type size_type;
    00045
    00046     typedef typename map_type::iterator iterator;
    00047     typedef typename map_type::const_iterator const_iterator;
    00048     // typedef typename map_type::reverse_iterator reverse_iterator;
    00049     // typedef typename map_type::const_reverse_iterator const_reverse_iterator;
    00050
    00051   protected:
    00052
    00053     map_type m_map;
    00054     static const result_type s_null_value;
    00055
    00056   public:
    00057     // ---- Constructors
    00058
    00060     Map(): m_map() {}
    00061
    00063     Map(const map_type& other): m_map(other) {}
    00064
    00065     // /// Copy Constructor
    00066     // Map(const Map& other): m_map(other.m_map) {}
    00068     template <typename In>
    00069     Map(In first, In last): m_map(first,last) {}
    00070
    00072     virtual ~Map() {}
    00073
    00074     // ---- std::map interface
    00075
    00076     //    -- iterators
    00077
    00078     inline iterator begin() { return m_map.begin(); }
    00079     inline iterator end()   { return m_map.end(); }
    00080
    00081     inline const_iterator begin() const { return m_map.begin(); }
    00082     inline const_iterator end()   const { return m_map.end(); }
    00083
    00084     // inline reverse_iterator rbegin() { return m_map.rbegin(); }
    00085     // inline reverse_iterator rend()   { return m_map.rend(); }
    00086
    00087     // inline const_reverse_iterator rbegin() const { return m_map.rbegin(); }
    00088     // inline const_reverse_iterator rend()   const { return m_map.rend(); }
    00089
    00090     //    -- subscription
    00091
    00092     inline mapped_type &operator[] (const key_type &key) { return m_map[key]; }
    00093
    00094     //    -- map operations
    00095
    00096     inline iterator find(const key_type &key) { return m_map.find(key); }
    00097     inline const_iterator find(const key_type &key) const { return m_map.find(key); }
    00098
    00099     inline size_type count(const key_type &key) const { return m_map.count(key); }
    00100
    00101     inline iterator lower_bound(const key_type &key) { return m_map.lower_bound(key); }
    00102     inline const_iterator lower_bound(const key_type &key) const { return m_map.lower_bound(key); }
    00103     inline iterator upper_bound(const key_type &key) { return m_map.upper_bound(key); }
    00104     inline const_iterator upper_bound(const key_type &key) const { return m_map.upper_bound(key); }
    00105
    00106     inline std::pair<iterator,iterator> equal_range(const key_type &key)
    00107     { return m_map.equal_range(key); }
    00108     inline std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const
    00109     { return m_map.equal_range(key); }
    00110
    00111     //    -- list operations
    00112
    00113     inline std::pair<iterator,bool> insert(const value_type &val) { return m_map.insert(val); }
    00114     inline iterator insert(iterator pos, const value_type &val) { return m_map.insert(pos,val); }
    00115     template <typename In>
    00116     inline void insert(In first, In last) { m_map.insert(first,last); }
    00117
    00118     inline void erase(iterator pos) { m_map.erase(pos); }
    00119     inline size_type erase(const key_type &key) { return m_map.erase(key); }
    00120     inline void erase(iterator first, iterator last) { m_map.erase(first,last); }
    00121     inline void clear() { m_map.clear(); }
    00122
    00123     //    -- container operations
    00124
    00125     inline size_type size() const { return m_map.size(); }
    00126     inline size_type max_size() const { return m_map.max_size(); }
    00127     inline bool empty() const { return size() == 0; }
    00128     inline void swap(map_type& other) { m_map.swap(other); }
    00129
    00130     // ---- extra functionalities
    00131
    00154     inline const result_type &operator() ( const argument_type &key ) const
    00155     {
    00156       // static const result_type s_null_value;
    00157       const_iterator it = m_map.find(key);
    00158       if ( it != m_map.end() ) { return it->second ; }
    00159       // return the default value
    00160       return s_null_value;
    00161     }
    00162
    00185     inline const mapped_type &operator[] ( const key_type &key ) const
    00186     { return (*this)(key); }
    00187
    00189     inline Map& merge ( const map_type& other )
    00190     {
    00191       for ( typename map_type::const_iterator it = other.begin() ;
    00192             other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
    00193       return *this;
    00194     }
    00195
    00197     inline operator       map_type &()       { return m_map ; }
    00198     inline operator const map_type &() const { return m_map ; }
    00199   };
    00200
    00201   template <typename K, typename T, typename M>
    00202   const typename Map<K,T,M>::result_type Map<K,T,M>::s_null_value = typename Map<K,T,M>::result_type();
    00203
    00204 } // GaudiUtils namespace
    00205
    00206 // ============================================================================
    00208 // ============================================================================
    00209 #endif // GAUDIKERNEL_MAP_H
    00210 // ============================================================================