All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Map.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_MAP_H
2 #define GAUDIKERNEL_MAP_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <map>
9 // ============================================================================
10 // GaudiKernel
11 // ============================================================================
12 #include "GaudiKernel/MapBase.h"
13 // ============================================================================
14 namespace GaudiUtils
15 {
16  // ==========================================================================
81  template <typename K, typename T, typename M = std::map<K,T> >
82  class Map : public Gaudi::Utils::MapBase
83  {
84  public:
85  // ========================================================================
86  // ---- types
87  typedef M map_type;
88  typedef K key_type;
89  typedef T mapped_type;
90 
91  // -- from unary_function
92  typedef K argument_type;
93  typedef T result_type;
94  typedef std::pair<const K,T> value_type;
95 
96  typedef typename map_type::size_type size_type;
97 
98  typedef typename map_type::iterator iterator;
99  typedef typename map_type::const_iterator const_iterator;
100  // typedef typename map_type::reverse_iterator reverse_iterator;
101  // typedef typename map_type::const_reverse_iterator const_reverse_iterator;
102  // ========================================================================
103  protected:
104  // ========================================================================
106  static const result_type s_null_value;
107  // ========================================================================
108  public:
109  // ---- Constructors
110 
112  Map(): m_map() {}
113 
115  Map(const map_type& other): m_map(other) {}
116 
117  // /// Copy Constructor
118  // Map(const Map& other): m_map(other.m_map) {}
120  template <typename In>
121  Map(In first, In last): m_map(first,last) {}
122 
124  virtual ~Map() {}
125 
126  // ---- std::map interface
127 
128  // -- iterators
129 
130  inline iterator begin() { return m_map.begin(); }
131  inline iterator end() { return m_map.end(); }
132 
133  inline const_iterator begin() const { return m_map.begin(); }
134  inline const_iterator end() const { return m_map.end(); }
135 
136  // inline reverse_iterator rbegin() { return m_map.rbegin(); }
137  // inline reverse_iterator rend() { return m_map.rend(); }
138 
139  // inline const_reverse_iterator rbegin() const { return m_map.rbegin(); }
140  // inline const_reverse_iterator rend() const { return m_map.rend(); }
141 
142  // -- subscription
143 
144  inline mapped_type &operator[] (const key_type &key) { return m_map[key]; }
145 
146  // -- map operations
147 
148  inline iterator find(const key_type &key) { return m_map.find(key); }
149  inline const_iterator find(const key_type &key) const { return m_map.find(key); }
150 
151  inline size_type count(const key_type &key) const { return m_map.count(key); }
152 
153  inline iterator lower_bound(const key_type &key) { return m_map.lower_bound(key); }
154  inline const_iterator lower_bound(const key_type &key) const { return m_map.lower_bound(key); }
155  inline iterator upper_bound(const key_type &key) { return m_map.upper_bound(key); }
156  inline const_iterator upper_bound(const key_type &key) const { return m_map.upper_bound(key); }
157 
158  inline std::pair<iterator,iterator> equal_range(const key_type &key)
159  { return m_map.equal_range(key); }
160  inline std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const
161  { return m_map.equal_range(key); }
162 
163  // -- list operations
164 
165  inline std::pair<iterator,bool> insert(const value_type &val) { return m_map.insert(val); }
166  inline std::pair<iterator,bool> insert
167  ( const key_type & key ,
168  const mapped_type & val ) { return insert ( value_type ( key , val ) ) ; }
169  template <typename In>
170  inline void insert(In first, In last) { m_map.insert(first,last); }
171  inline iterator insert( iterator /* pos */ , const value_type &val)
172  { return m_map.insert( /* pos, */ val ).first ; }
173  inline void erase(iterator pos) { m_map.erase(pos); }
174  inline size_type erase(const key_type &key) { return m_map.erase(key); }
175  inline void erase(iterator first, iterator last) { m_map.erase(first,last); }
176  inline void clear() { m_map.clear(); }
177 
178  // -- container operations
179 
180  inline size_type size() const { return m_map.size(); }
181  inline size_type max_size() const { return m_map.max_size(); }
182  inline bool empty() const { return size() == 0; }
183  inline void swap(map_type& other) { m_map.swap(other); }
184 
185  // ---- extra functionalities
186 
209  inline const result_type &operator() ( const argument_type &key ) const
210  {
211  // static const result_type s_null_value;
212  const_iterator it = m_map.find(key);
213  if ( it != m_map.end() ) { return it->second ; }
215  return s_null_value; // return the default value
216  }
217  // ========================================================================
240  inline const mapped_type &operator[] ( const key_type &key ) const
241  { return (*this)(key); }
242  // ========================================================================
248  inline const result_type & at ( const argument_type &key ) const
249  {
250  const_iterator it = m_map.find ( key ) ;
251  if ( it == m_map.end() ) { this->throw_out_of_range_exception () ; }
252  return it->second ;
253  }
254  // ========================================================================
256  inline Map& merge ( const map_type& other )
257  {
258  for ( typename map_type::const_iterator it = other.begin() ;
259  other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
260  return *this;
261  }
263  inline Map& merge ( const Map& other )
264  {
265  for ( const_iterator it = other.begin() ;
266  other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
267  return *this;
268  }
270  template <class K1,class K2, class K3>
271  inline Map& merge ( const Map<K1,K2,K3>& other )
272  {
273  for ( typename Map<K1,K2,K3>::const_iterator it = other.begin() ;
274  other.end() != it ; ++it ) { (*this)[it->first] = it->second ; }
275  return *this;
276  }
277  // update the key
278  void update
279  ( const key_type& key ,
280  const mapped_type& mapped ) { (*this)[ key ] = mapped ; }
281  // ========================================================================
282  public:
283  // ========================================================================
285  inline operator map_type &() { return m_map ; }
286  inline operator const map_type &() const { return m_map ; }
287  // ========================================================================
288  public:
289  // ========================================================================
295  const key_type& key_at ( const size_type index ) const
296  {
297  if ( index >= size() )
298  { this->throw_out_of_range_exception () ; }
299  const_iterator it = this->begin() ;
300  std::advance ( it , index ) ;
301  return it -> first ;
302  }
308  const mapped_type& value_at ( const size_type index ) const
309  {
310  if ( index >= size() )
311  { this->throw_out_of_range_exception () ; }
312  const_iterator it = this->begin() ;
313  std::advance ( it , index ) ;
314  return it -> second ;
315  }
316  // ========================================================================
317  };
318  // ==========================================================================
319  template <typename K, typename T, typename M>
320  const typename Map<K,T,M>::result_type Map<K,T,M>::s_null_value = typename Map<K,T,M>::result_type();
321  // ==========================================================================
322 } // end of GaudiUtils namespace
323 // ============================================================================
324 
325 // ============================================================================
326 // The END
327 // ============================================================================
328 #endif // GAUDIKERNEL_MAP_H
329 // ============================================================================
Map()
Standard constructor.
Definition: Map.h:112
Extension of the STL map.
Definition: Map.h:82
K argument_type
Definition: Map.h:92
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:160
size_type size() const
Definition: Map.h:180
const_iterator end() const
Definition: Map.h:134
void erase(iterator first, iterator last)
Definition: Map.h:175
K key_type
Definition: Map.h:88
static const result_type s_null_value
Definition: Map.h:106
virtual ~Map()
Virtual destructor. You can inherit from this map type.
Definition: Map.h:124
size_type count(const key_type &key) const
Definition: Map.h:151
const result_type & at(const argument_type &key) const
checked access to the map for missing keys
Definition: Map.h:248
map_type::size_type size_type
Definition: Map.h:96
size_type erase(const key_type &key)
Definition: Map.h:174
map_type m_map
Definition: Map.h:105
void insert(In first, In last)
Definition: Map.h:170
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:279
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:158
const_iterator find(const key_type &key) const
Definition: Map.h:149
void erase(iterator pos)
Definition: Map.h:173
std::pair< iterator, bool > insert(const value_type &val)
Definition: Map.h:165
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:154
mapped_type & operator[](const key_type &key)
Definition: Map.h:144
iterator end()
Definition: Map.h:131
M map_type
Definition: Map.h:87
iterator upper_bound(const key_type &key)
Definition: Map.h:155
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:209
bool empty() const
Definition: Map.h:182
std::pair< const K, T > value_type
Definition: Map.h:94
iterator lower_bound(const key_type &key)
Definition: Map.h:153
iterator find(const key_type &key)
Definition: Map.h:148
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:308
map_type::const_iterator const_iterator
Definition: Map.h:99
iterator begin()
Definition: Map.h:130
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:263
Map(In first, In last)
Construct from a subset.
Definition: Map.h:121
T mapped_type
Definition: Map.h:89
map_type::iterator iterator
Definition: Map.h:98
iterator insert(iterator, const value_type &val)
Definition: Map.h:171
void swap(map_type &other)
Definition: Map.h:183
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:295
const_iterator begin() const
Definition: Map.h:133
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:115
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:156
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition: Map.h:271
void clear()
Definition: Map.h:176
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:29
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:46
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:256
size_type max_size() const
Definition: Map.h:181
T result_type
Definition: Map.h:93