Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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  // ==========================================================================
80  template <typename K, typename T, typename M = std::map<K, T>>
81  class Map : public Gaudi::Utils::MapBase {
82  public:
83  // ========================================================================
84  // ---- types
85  typedef M map_type;
86  typedef K key_type;
87  typedef T mapped_type;
88 
89  // -- from unary_function
90  typedef K argument_type;
91  typedef T result_type;
93  typedef const value_type& const_reference;
94 
95  typedef typename map_type::size_type size_type;
96 
97  typedef typename map_type::iterator iterator;
98  typedef typename map_type::const_iterator const_iterator;
99  // typedef typename map_type::reverse_iterator reverse_iterator;
100  // typedef typename map_type::const_reverse_iterator const_reverse_iterator;
101  // ========================================================================
102  protected:
103  // ========================================================================
104  map_type m_map;
105  static const result_type s_null_value;
106  // ========================================================================
107  public:
108  // ---- Constructors
109 
111  Map() = default;
112 
114  Map( const map_type& other ) : m_map( other ) {}
115 
116  // /// Copy Constructor
117  // Map(const Map& other): m_map(other.m_map) {}
119  template <typename In>
120  Map( In&& first, In&& last ) : m_map( std::forward<In>( first ), std::forward<In>( last ) ) {}
121 
123  virtual ~Map() = default;
124 
125  // ---- std::map interface
126 
127  // -- iterators
128 
129  inline iterator begin() { return m_map.begin(); }
130  inline iterator end() { return m_map.end(); }
131 
132  inline const_iterator begin() const { return m_map.begin(); }
133  inline const_iterator end() const { return m_map.end(); }
134 
135  // inline reverse_iterator rbegin() { return m_map.rbegin(); }
136  // inline reverse_iterator rend() { return m_map.rend(); }
137 
138  // inline const_reverse_iterator rbegin() const { return m_map.rbegin(); }
139  // inline const_reverse_iterator rend() const { return m_map.rend(); }
140 
141  // -- subscription
142 
143  inline mapped_type& operator[]( const key_type& key ) { return m_map[key]; }
144 
145  // -- map operations
146 
147  inline iterator find( const key_type& key ) { return m_map.find( key ); }
148  inline const_iterator find( const key_type& key ) const { return m_map.find( key ); }
149 
150  inline size_type count( const key_type& key ) const { return m_map.count( key ); }
151 
152  inline iterator lower_bound( const key_type& key ) { return m_map.lower_bound( key ); }
153  inline const_iterator lower_bound( const key_type& key ) const { return m_map.lower_bound( key ); }
154  inline iterator upper_bound( const key_type& key ) { return m_map.upper_bound( key ); }
155  inline const_iterator upper_bound( const key_type& key ) const { return m_map.upper_bound( key ); }
156 
157  inline std::pair<iterator, iterator> equal_range( const key_type& key ) { return m_map.equal_range( key ); }
158  inline std::pair<const_iterator, const_iterator> equal_range( const key_type& key ) const {
159  return m_map.equal_range( key );
160  }
161 
162  // -- list operations
163  template <class... Args>
165  return m_map.emplace( std::forward<Args>( args )... );
166  }
167  template <typename ValueType>
168  inline std::pair<iterator, bool> insert( ValueType&& val ) {
169  return m_map.insert( std::forward<ValueType>( val ) );
170  }
171  inline std::pair<iterator, bool> insert( value_type&& val ) {
172  return m_map.insert( std::forward<value_type>( val ) );
173  }
174  template <typename In>
175  inline void insert( In&& first, In&& last ) {
176  m_map.insert( std::forward<In>( first ), std::forward<In>( last ) );
177  }
178  template <typename ValueType>
179  inline iterator insert( iterator /* pos */, ValueType&& val ) {
180  return m_map.insert( /* pos, */ std::forward<ValueType>( val ) ).first;
181  }
182  inline iterator erase( const_iterator pos ) { return m_map.erase( pos ); }
183  inline size_type erase( const key_type& key ) { return m_map.erase( key ); }
184  inline iterator erase( const_iterator first, const_iterator last ) { return m_map.erase( first, last ); }
185  inline void clear() { m_map.clear(); }
186 
187  // -- container operations
188 
189  inline size_type size() const { return m_map.size(); }
190  inline size_type max_size() const { return m_map.max_size(); }
191  inline bool empty() const { return size() == 0; }
192  inline void swap( map_type& other ) { m_map.swap( other ); }
193 
194  // ---- extra functionalities
195 
218  inline const result_type& operator()( const argument_type& key ) const {
219  // static const result_type s_null_value;
220  auto it = m_map.find( key );
222  return it != m_map.end() ? it->second : s_null_value;
223  }
224  // ========================================================================
247  inline const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
248  // ========================================================================
254  inline const result_type& at( const argument_type& key ) const { return m_map.at( key ); }
255  // ========================================================================
257  inline Map& merge( const map_type& other ) {
258  m_map.insert( std::begin( other ), std::end( other ) );
259  return *this;
260  }
262  inline Map& merge( const Map& other ) {
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  m_map.insert( std::begin( other ), std::end( other ) );
270  return *this;
271  }
272  // update the key
273  void update( const key_type& key, const mapped_type& mapped ) { ( *this )[key] = mapped; }
274  // ========================================================================
275  public:
276  // ========================================================================
278  inline operator map_type&() { return m_map; }
279  inline operator const map_type&() const { return m_map; }
280  // ========================================================================
281  public:
282  // ========================================================================
288  const key_type& key_at( const size_type index ) const {
289  if ( index >= size() ) { this->throw_out_of_range_exception(); }
290  return std::next( this->begin(), index )->first;
291  }
297  const mapped_type& value_at( const size_type index ) const {
298  if ( index >= size() ) { this->throw_out_of_range_exception(); }
299  return std::next( this->begin(), index )->second;
300  }
301  // ========================================================================
302  };
303  // ==========================================================================
304  template <typename K, typename T, typename M>
306  // ==========================================================================
307 } // namespace GaudiUtils
308 // ============================================================================
309 
310 // ============================================================================
311 // The END
312 // ============================================================================
313 #endif // GAUDIKERNEL_MAP_H
std::pair< iterator, bool > insert(value_type &&val)
Definition: Map.h:171
Extension of the STL map.
Definition: Map.h:81
K argument_type
Definition: Map.h:90
size_type size() const
Definition: Map.h:189
const_iterator end() const
Definition: Map.h:133
K key_type
Definition: Map.h:86
static const result_type s_null_value
Definition: Map.h:105
T swap(T...args)
size_type count(const key_type &key) const
Definition: Map.h:150
Map(In &&first, In &&last)
Construct from a subset.
Definition: Map.h:120
STL namespace.
const result_type & at(const argument_type &key) const
checked access to the map for missing keys
Definition: Map.h:254
map_type::size_type size_type
Definition: Map.h:95
size_type erase(const key_type &key)
Definition: Map.h:183
map_type m_map
Definition: Map.h:104
iterator insert(iterator, ValueType &&val)
Definition: Map.h:179
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:273
const_iterator find(const key_type &key) const
Definition: Map.h:148
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:168
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:153
mapped_type & operator[](const key_type &key)
Definition: Map.h:143
iterator end()
Definition: Map.h:130
M map_type
Definition: Map.h:85
iterator erase(const_iterator first, const_iterator last)
Definition: Map.h:184
iterator upper_bound(const key_type &key)
Definition: Map.h:154
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:218
T next(T...args)
bool empty() const
Definition: Map.h:191
iterator lower_bound(const key_type &key)
Definition: Map.h:152
T erase(T...args)
iterator find(const key_type &key)
Definition: Map.h:147
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:297
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:164
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:98
T count(T...args)
iterator begin()
Definition: Map.h:129
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:262
void insert(In &&first, In &&last)
Definition: Map.h:175
T insert(T...args)
T mapped_type
Definition: Map.h:87
T find(T...args)
T size(T...args)
map_type::iterator iterator
Definition: Map.h:97
iterator erase(const_iterator pos)
Definition: Map.h:182
void swap(map_type &other)
Definition: Map.h:192
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:288
T begin(T...args)
const_iterator begin() const
Definition: Map.h:132
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:114
std::pair< const K, T > value_type
Definition: Map.h:92
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:158
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:155
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:136
T emplace(T...args)
void clear()
Definition: Map.h:185
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:28
const mapped_type & operator[](const key_type &key) const
Access elements of a const Map.
Definition: Map.h:247
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:42
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:257
const value_type & const_reference
Definition: Map.h:93
size_type max_size() const
Definition: Map.h:190
T result_type
Definition: Map.h:91
Map()=default
Standard constructor.
T equal_range(T...args)
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:157