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