Map.h
Go to the documentation of this file.00001
00002
00003
00004
00005 #ifndef GAUDIKERNEL_MAP_H
00006 #define GAUDIKERNEL_MAP_H 1
00007
00008
00009
00010
00011
00012 #include <map>
00013
00014
00015
00016 #include "GaudiKernel/MapBase.h"
00017
00018 namespace GaudiUtils
00019 {
00020
00085 template <typename K, typename T, typename M = std::map<K,T> >
00086 class Map : public Gaudi::Utils::MapBase
00087 {
00088 public:
00089
00090
00091 typedef M map_type;
00092 typedef K key_type;
00093 typedef T mapped_type;
00094
00095
00096 typedef K argument_type;
00097 typedef T result_type;
00098 typedef std::pair<const K,T> value_type;
00099
00100 typedef typename map_type::size_type size_type;
00101
00102 typedef typename map_type::iterator iterator;
00103 typedef typename map_type::const_iterator const_iterator;
00104
00105
00106
00107 protected:
00108
00109 map_type m_map;
00110 static const result_type s_null_value;
00111
00112 public:
00113
00114
00116 Map(): m_map() {}
00117
00119 Map(const map_type& other): m_map(other) {}
00120
00121
00122
00124 template <typename In>
00125 Map(In first, In last): m_map(first,last) {}
00126
00128 virtual ~Map() {}
00129
00130
00131
00132
00133
00134 inline iterator begin() { return m_map.begin(); }
00135 inline iterator end() { return m_map.end(); }
00136
00137 inline const_iterator begin() const { return m_map.begin(); }
00138 inline const_iterator end() const { return m_map.end(); }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 inline mapped_type &operator[] (const key_type &key) { return m_map[key]; }
00149
00150
00151
00152 inline iterator find(const key_type &key) { return m_map.find(key); }
00153 inline const_iterator find(const key_type &key) const { return m_map.find(key); }
00154
00155 inline size_type count(const key_type &key) const { return m_map.count(key); }
00156
00157 inline iterator lower_bound(const key_type &key) { return m_map.lower_bound(key); }
00158 inline const_iterator lower_bound(const key_type &key) const { return m_map.lower_bound(key); }
00159 inline iterator upper_bound(const key_type &key) { return m_map.upper_bound(key); }
00160 inline const_iterator upper_bound(const key_type &key) const { return m_map.upper_bound(key); }
00161
00162 inline std::pair<iterator,iterator> equal_range(const key_type &key)
00163 { return m_map.equal_range(key); }
00164 inline std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const
00165 { return m_map.equal_range(key); }
00166
00167
00168
00169 inline std::pair<iterator,bool> insert(const value_type &val) { return m_map.insert(val); }
00170 inline std::pair<iterator,bool> insert
00171 ( const key_type & key ,
00172 const mapped_type & val ) { return insert ( value_type ( key , val ) ) ; }
00173 template <typename In>
00174 inline void insert(In first, In last) { m_map.insert(first,last); }
00175 inline iterator insert( iterator , const value_type &val)
00176 { return m_map.insert( val ).first ; }
00177 inline void erase(iterator pos) { m_map.erase(pos); }
00178 inline size_type erase(const key_type &key) { return m_map.erase(key); }
00179 inline void erase(iterator first, iterator last) { m_map.erase(first,last); }
00180 inline void clear() { m_map.clear(); }
00181
00182
00183
00184 inline size_type size() const { return m_map.size(); }
00185 inline size_type max_size() const { return m_map.max_size(); }
00186 inline bool empty() const { return size() == 0; }
00187 inline void swap(map_type& other) { m_map.swap(other); }
00188
00189
00190
00213 inline const result_type &operator() ( const argument_type &key ) const
00214 {
00215
00216 const_iterator it = m_map.find(key);
00217 if ( it != m_map.end() ) { return it->second ; }
00219 return s_null_value;
00220 }
00221
00244 inline const mapped_type &operator[] ( const key_type &key ) const
00245 { return (*this)(key); }
00246
00252 inline const result_type & at ( const argument_type &key ) const
00253 {
00254 const_iterator it = m_map.find ( key ) ;
00255 if ( it == m_map.end() ) { this->throw_out_of_range_exception () ; }
00256 return it->second ;
00257 }
00258
00260 inline Map& merge ( const map_type& other )
00261 {
00262 for ( typename map_type::const_iterator it = other.begin() ;
00263 other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
00264 return *this;
00265 }
00267 inline Map& merge ( const Map& other )
00268 {
00269 for ( const_iterator it = other.begin() ;
00270 other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
00271 return *this;
00272 }
00274 template <class K1,class K2, class K3>
00275 inline Map& merge ( const Map<K1,K2,K3>& other )
00276 {
00277 for ( typename Map<K1,K2,K3>::const_iterator it = other.begin() ;
00278 other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
00279 return *this;
00280 }
00281
00282 void update
00283 ( const key_type& key ,
00284 const mapped_type& mapped ) { (*this)[ key ] = mapped ; }
00285
00286 public:
00287
00289 inline operator map_type &() { return m_map ; }
00290 inline operator const map_type &() const { return m_map ; }
00291
00292 public:
00293
00299 const key_type& key_at ( const size_t index ) const
00300 {
00301 if ( index >= size() )
00302 { this->throw_out_of_range_exception () ; }
00303 const_iterator it = this->begin() ;
00304 std::advance ( it , index ) ;
00305 return it -> first ;
00306 }
00312 const mapped_type& value_at ( const size_t index ) const
00313 {
00314 if ( index >= size() )
00315 { this->throw_out_of_range_exception () ; }
00316 const_iterator it = this->begin() ;
00317 std::advance ( it , index ) ;
00318 return it -> second ;
00319 }
00320
00321 };
00322
00323 template <typename K, typename T, typename M>
00324 const typename Map<K,T,M>::result_type Map<K,T,M>::s_null_value = typename Map<K,T,M>::result_type();
00325
00326 }
00327
00328
00329
00330
00331
00332 #endif // GAUDIKERNEL_MAP_H
00333