The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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;
95  typedef const value_type& const_reference;
96 
97  typedef typename map_type::size_type size_type;
98 
99  typedef typename map_type::iterator iterator;
100  typedef typename map_type::const_iterator const_iterator;
101  // typedef typename map_type::reverse_iterator reverse_iterator;
102  // typedef typename map_type::const_reverse_iterator const_reverse_iterator;
103  // ========================================================================
104  protected:
105  // ========================================================================
106  map_type m_map;
107  static const result_type s_null_value;
108  // ========================================================================
109  public:
110  // ---- Constructors
111 
113  Map() = default ;
114 
116  Map(const map_type& other): m_map(other) {}
117 
118  // /// Copy Constructor
119  // Map(const Map& other): m_map(other.m_map) {}
121  template <typename In>
122  Map(In&& first, In&& last): m_map(std::forward<In>(first),std::forward<In>(last)) {}
123 
125  virtual ~Map() = default;
126 
127  // ---- std::map interface
128 
129  // -- iterators
130 
131  inline iterator begin() { return m_map.begin(); }
132  inline iterator end() { return m_map.end(); }
133 
134  inline const_iterator begin() const { return m_map.begin(); }
135  inline const_iterator end() const { return m_map.end(); }
136 
137  // inline reverse_iterator rbegin() { return m_map.rbegin(); }
138  // inline reverse_iterator rend() { return m_map.rend(); }
139 
140  // inline const_reverse_iterator rbegin() const { return m_map.rbegin(); }
141  // inline const_reverse_iterator rend() const { return m_map.rend(); }
142 
143  // -- subscription
144 
145  inline mapped_type &operator[] (const key_type &key) { return m_map[key]; }
146 
147  // -- map operations
148 
149  inline iterator find(const key_type &key) { return m_map.find(key); }
150  inline const_iterator find(const key_type &key) const { return m_map.find(key); }
151 
152  inline size_type count(const key_type &key) const { return m_map.count(key); }
153 
154  inline iterator lower_bound(const key_type &key) { return m_map.lower_bound(key); }
155  inline const_iterator lower_bound(const key_type &key) const { return m_map.lower_bound(key); }
156  inline iterator upper_bound(const key_type &key) { return m_map.upper_bound(key); }
157  inline const_iterator upper_bound(const key_type &key) const { return m_map.upper_bound(key); }
158 
159  inline std::pair<iterator,iterator> equal_range(const key_type &key)
160  { return m_map.equal_range(key); }
161  inline std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const
162  { return m_map.equal_range(key); }
163 
164  // -- list operations
165  template< class... Args >
166  std::pair<iterator,bool> emplace( Args&&... args ) { return m_map.emplace(std::forward<Args>(args)...); }
167  template <typename ValueType>
168  inline std::pair<iterator,bool> insert(ValueType&& val) { return m_map.insert(std::forward<ValueType>(val)); }
169  inline std::pair<iterator,bool> insert(value_type&& val) { return m_map.insert(std::forward<value_type>(val)); }
170  template <typename In>
171  inline void insert(In&& first, In&& last) { m_map.insert(std::forward<In>(first),std::forward<In>(last)); }
172  template <typename ValueType>
173  inline iterator insert( iterator /* pos */ , ValueType && val)
174  { return m_map.insert( /* pos, */ std::forward<ValueType>(val) ).first ; }
175  inline iterator erase(const_iterator pos) { return m_map.erase(pos); }
176  inline size_type erase(const key_type &key) { return m_map.erase(key); }
177  inline iterator erase(const_iterator first, const_iterator last) { return m_map.erase(first,last); }
178  inline void clear() { m_map.clear(); }
179 
180  // -- container operations
181 
182  inline size_type size() const { return m_map.size(); }
183  inline size_type max_size() const { return m_map.max_size(); }
184  inline bool empty() const { return size() == 0; }
185  inline void swap(map_type& other) { m_map.swap(other); }
186 
187  // ---- extra functionalities
188 
211  inline const result_type &operator() ( const argument_type &key ) const
212  {
213  // static const result_type s_null_value;
214  auto it = m_map.find(key);
216  return it != m_map.end() ? it->second : s_null_value;
217  }
218  // ========================================================================
241  inline const mapped_type &operator[] ( const key_type &key ) const
242  { return (*this)(key); }
243  // ========================================================================
249  inline const result_type & at ( const argument_type &key ) const
250  {
251  return m_map.at(key);
252  }
253  // ========================================================================
255  inline Map& merge ( const map_type& other )
256  {
257  m_map.insert( std::begin(other), std::end(other) );
258  return *this;
259  }
261  inline Map& merge ( const Map& other )
262  {
263  m_map.insert( std::begin(other), std::end(other) );
264  return *this;
265  }
267  template <class K1,class K2, class K3>
268  inline Map& merge ( const Map<K1,K2,K3>& other )
269  {
270  m_map.insert( std::begin(other), std::end(other) );
271  return *this;
272  }
273  // update the key
274  void update
275  ( const key_type& key ,
276  const mapped_type& mapped ) { (*this)[ key ] = mapped ; }
277  // ========================================================================
278  public:
279  // ========================================================================
281  inline operator map_type &() { return m_map ; }
282  inline operator const map_type &() const { return m_map ; }
283  // ========================================================================
284  public:
285  // ========================================================================
291  const key_type& key_at ( const size_type index ) const
292  {
293  if ( index >= size() )
294  { this->throw_out_of_range_exception () ; }
295  return std::next ( this->begin() , index )->first ;
296  }
302  const mapped_type& value_at ( const size_type index ) const
303  {
304  if ( index >= size() )
305  { this->throw_out_of_range_exception () ; }
306  return std::next( this->begin() , index )->second ;
307  }
308  // ========================================================================
309  };
310  // ==========================================================================
311  template <typename K, typename T, typename M>
313  // ==========================================================================
314 } // end of GaudiUtils namespace
315 // ============================================================================
316 
317 // ============================================================================
318 // The END
319 // ============================================================================
320 #endif // GAUDIKERNEL_MAP_H
Extension of the STL map.
Definition: Map.h:82
K argument_type
Definition: Map.h:92
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:166
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:161
size_type size() const
Definition: Map.h:182
const_iterator end() const
Definition: Map.h:135
K key_type
Definition: Map.h:88
static const result_type s_null_value
Definition: Map.h:107
T swap(T...args)
size_type count(const key_type &key) const
Definition: Map.h:152
Map(In &&first, In &&last)
Construct from a subset.
Definition: Map.h:122
STL namespace.
const result_type & at(const argument_type &key) const
checked access to the map for missing keys
Definition: Map.h:249
map_type::size_type size_type
Definition: Map.h:97
size_type erase(const key_type &key)
Definition: Map.h:176
map_type m_map
Definition: Map.h:106
std::pair< iterator, bool > insert(value_type &&val)
Definition: Map.h:169
iterator insert(iterator, ValueType &&val)
Definition: Map.h:173
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:275
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:159
const_iterator find(const key_type &key) const
Definition: Map.h:150
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:155
mapped_type & operator[](const key_type &key)
Definition: Map.h:145
iterator end()
Definition: Map.h:132
M map_type
Definition: Map.h:87
iterator erase(const_iterator first, const_iterator last)
Definition: Map.h:177
iterator upper_bound(const key_type &key)
Definition: Map.h:156
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:211
T next(T...args)
bool empty() const
Definition: Map.h:184
std::pair< const K, T > value_type
Definition: Map.h:94
iterator lower_bound(const key_type &key)
Definition: Map.h:154
T erase(T...args)
iterator find(const key_type &key)
Definition: Map.h:149
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:302
virtual ~Map()=default
Virtual destructor. You can inherit from this map type.
T clear(T...args)
map_type::const_iterator const_iterator
Definition: Map.h:100
T count(T...args)
iterator begin()
Definition: Map.h:131
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:261
void insert(In &&first, In &&last)
Definition: Map.h:171
T insert(T...args)
T mapped_type
Definition: Map.h:89
T find(T...args)
T size(T...args)
map_type::iterator iterator
Definition: Map.h:99
iterator erase(const_iterator pos)
Definition: Map.h:175
void swap(map_type &other)
Definition: Map.h:185
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:291
T begin(T...args)
const_iterator begin() const
Definition: Map.h:134
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:116
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:157
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition: Map.h:268
T max_size(T...args)
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:149
T emplace(T...args)
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:168
void clear()
Definition: Map.h:178
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:28
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:44
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:255
const value_type & const_reference
Definition: Map.h:95
size_type max_size() const
Definition: Map.h:183
T result_type
Definition: Map.h:93
Map()=default
Standard constructor.
T equal_range(T...args)