The Gaudi Framework  master (37c0b60a)
Map.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_MAP_H
12 #define GAUDIKERNEL_MAP_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <map>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
22 #include <GaudiKernel/MapBase.h>
23 // ============================================================================
24 namespace GaudiUtils {
25  // ==========================================================================
90  template <typename K, typename T, typename M = std::map<K, T>>
91  class Map : public Gaudi::Utils::MapBase {
92  public:
93  // ========================================================================
94  // ---- types
95  typedef M map_type;
96  typedef K key_type;
97  typedef T mapped_type;
98 
99  // -- from unary_function
100  typedef K argument_type;
101  typedef T result_type;
103  typedef const value_type& const_reference;
104 
105  typedef typename map_type::size_type size_type;
106 
107  typedef typename map_type::iterator iterator;
108  typedef typename map_type::const_iterator const_iterator;
109  // typedef typename map_type::reverse_iterator reverse_iterator;
110  // typedef typename map_type::const_reverse_iterator const_reverse_iterator;
111  // ========================================================================
112  protected:
113  // ========================================================================
115  static const result_type s_null_value;
116  // ========================================================================
117  public:
118  // ---- Constructors
119 
121  Map() = default;
122 
124  Map( const map_type& other ) : m_map( other ) {}
125 
126  // /// Copy Constructor
127  // Map(const Map& other): m_map(other.m_map) {}
129  template <typename In>
130  Map( In&& first, In&& last ) : m_map( std::forward<In>( first ), std::forward<In>( last ) ) {}
131 
133  virtual ~Map() = default;
134 
135  // ---- std::map interface
136 
137  // -- iterators
138 
139  inline iterator begin() { return m_map.begin(); }
140  inline iterator end() { return m_map.end(); }
141 
142  inline const_iterator begin() const { return m_map.begin(); }
143  inline const_iterator end() const { return m_map.end(); }
144 
145  // inline reverse_iterator rbegin() { return m_map.rbegin(); }
146  // inline reverse_iterator rend() { return m_map.rend(); }
147 
148  // inline const_reverse_iterator rbegin() const { return m_map.rbegin(); }
149  // inline const_reverse_iterator rend() const { return m_map.rend(); }
150 
151  // -- subscription
152 
153  inline mapped_type& operator[]( const key_type& key ) { return m_map[key]; }
154 
155  // -- map operations
156 
157  inline iterator find( const key_type& key ) { return m_map.find( key ); }
158  inline const_iterator find( const key_type& key ) const { return m_map.find( key ); }
159 
160  inline size_type count( const key_type& key ) const { return m_map.count( key ); }
161 
162  inline iterator lower_bound( const key_type& key ) { return m_map.lower_bound( key ); }
163  inline const_iterator lower_bound( const key_type& key ) const { return m_map.lower_bound( key ); }
164  inline iterator upper_bound( const key_type& key ) { return m_map.upper_bound( key ); }
165  inline const_iterator upper_bound( const key_type& key ) const { return m_map.upper_bound( key ); }
166 
167  inline std::pair<iterator, iterator> equal_range( const key_type& key ) { return m_map.equal_range( key ); }
169  return m_map.equal_range( key );
170  }
171 
172  // -- list operations
173  template <class... Args>
175  return m_map.emplace( std::forward<Args>( args )... );
176  }
177  template <typename ValueType>
178  inline std::pair<iterator, bool> insert( ValueType&& val ) {
179  return m_map.insert( std::forward<ValueType>( val ) );
180  }
182  return m_map.insert( std::forward<value_type>( val ) );
183  }
184  template <typename In>
185  inline void insert( In&& first, In&& last ) {
186  m_map.insert( std::forward<In>( first ), std::forward<In>( last ) );
187  }
188  template <typename ValueType>
189  inline iterator insert( iterator /* pos */, ValueType&& val ) {
190  return m_map.insert( /* pos, */ std::forward<ValueType>( val ) ).first;
191  }
192  inline iterator erase( const_iterator pos ) { return m_map.erase( pos ); }
193  inline size_type erase( const key_type& key ) { return m_map.erase( key ); }
194  inline iterator erase( const_iterator first, const_iterator last ) { return m_map.erase( first, last ); }
195  inline void clear() { m_map.clear(); }
196 
197  // -- container operations
198 
199  inline size_type size() const { return m_map.size(); }
200  inline size_type max_size() const { return m_map.max_size(); }
201  inline bool empty() const { return size() == 0; }
202  inline void swap( map_type& other ) { m_map.swap( other ); }
203 
204  // ---- extra functionalities
205 
228  inline const result_type& operator()( const argument_type& key ) const {
229  // static const result_type s_null_value;
230  auto it = m_map.find( key );
232  return it != m_map.end() ? it->second : s_null_value;
233  }
234  // ========================================================================
257  inline const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
258  // ========================================================================
264  inline const result_type& at( const argument_type& key ) const { return m_map.at( key ); }
265  // ========================================================================
267  inline Map& merge( const map_type& other ) {
268  m_map.insert( std::begin( other ), std::end( other ) );
269  return *this;
270  }
272  inline Map& merge( const Map& other ) {
273  m_map.insert( std::begin( other ), std::end( other ) );
274  return *this;
275  }
277  template <class K1, class K2, class K3>
278  inline Map& merge( const Map<K1, K2, K3>& other ) {
279  m_map.insert( std::begin( other ), std::end( other ) );
280  return *this;
281  }
282  // update the key
283  void update( const key_type& key, const mapped_type& mapped ) { ( *this )[key] = mapped; }
284  // ========================================================================
285  public:
286  // ========================================================================
288  inline operator map_type&() { return m_map; }
289  inline operator const map_type&() const { return m_map; }
290  // ========================================================================
291  public:
292  // ========================================================================
298  const key_type& key_at( const size_type index ) const {
299  if ( index >= size() ) { this->throw_out_of_range_exception(); }
300  return std::next( this->begin(), index )->first;
301  }
307  const mapped_type& value_at( const size_type index ) const {
308  if ( index >= size() ) { this->throw_out_of_range_exception(); }
309  return std::next( this->begin(), index )->second;
310  }
311  // ========================================================================
312  };
313  // ==========================================================================
314  template <typename K, typename T, typename M>
316  // ==========================================================================
317 } // namespace GaudiUtils
318 // ============================================================================
319 
320 // ============================================================================
321 // The END
322 // ============================================================================
323 #endif // GAUDIKERNEL_MAP_H
GaudiUtils::Map::erase
size_type erase(const key_type &key)
Definition: Map.h:193
GaudiUtils::Map::map_type
M map_type
Definition: Map.h:95
GaudiUtils::Map::m_map
map_type m_map
Definition: Map.h:114
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:157
GaudiUtils::Map::upper_bound
iterator upper_bound(const key_type &key)
Definition: Map.h:164
GaudiUtils::Map::Map
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:124
std::pair
GaudiUtils::Map::count
size_type count(const key_type &key) const
Definition: Map.h:160
GaudiUtils::Map::upper_bound
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:165
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(value_type &&val)
Definition: Map.h:181
GaudiUtils::Map::insert
iterator insert(iterator, ValueType &&val)
Definition: Map.h:189
GaudiUtils::Map::swap
void swap(map_type &other)
Definition: Map.h:202
GaudiUtils::Map::begin
const_iterator begin() const
Definition: Map.h:142
GaudiUtils::Map::operator()
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:228
GaudiUtils::Map::value_at
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:307
GaudiUtils::Map::operator[]
const mapped_type & operator[](const key_type &key) const
Access elements of a const Map.
Definition: Map.h:257
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:139
GaudiUtils::Map::empty
bool empty() const
Definition: Map.h:201
GaudiUtils::Map::const_iterator
map_type::const_iterator const_iterator
Definition: Map.h:108
GaudiUtils::Map::merge
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:272
GaudiUtils::Map::erase
iterator erase(const_iterator first, const_iterator last)
Definition: Map.h:194
GaudiUtils::Map::clear
void clear()
Definition: Map.h:195
GaudiUtils::Map::key_at
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:298
GaudiUtils::Map::lower_bound
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:163
GaudiUtils::Map::merge
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:267
GaudiUtils::Map::merge
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition: Map.h:278
GaudiUtils::Map::value_type
std::pair< const K, T > value_type
Definition: Map.h:102
GaudiUtils::Map::insert
void insert(In &&first, In &&last)
Definition: Map.h:185
GaudiUtils::Map::Map
Map()=default
Standard constructor.
GaudiUtils::Map::lower_bound
iterator lower_bound(const key_type &key)
Definition: Map.h:162
GaudiUtils::Map::at
const result_type & at(const argument_type &key) const
checked access to the map
Definition: Map.h:264
GaudiUtils::Map
Definition: Map.h:91
GaudiUtils::Map::const_reference
const value_type & const_reference
Definition: Map.h:103
GaudiUtils::Map::find
const_iterator find(const key_type &key) const
Definition: Map.h:158
GaudiUtils::Map::result_type
T result_type
Definition: Map.h:101
std::map< Key, Value >
GaudiUtils::Map::Map
Map(In &&first, In &&last)
Construct from a subset.
Definition: Map.h:130
GaudiUtils::Map::end
iterator end()
Definition: Map.h:140
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:192
GaudiUtils::Map::key_type
K key_type
Definition: Map.h:96
GaudiUtils::Map::equal_range
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:168
GaudiUtils::Map::equal_range
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:167
GaudiUtils::Map::size
size_type size() const
Definition: Map.h:199
gaudirun.args
args
Definition: gaudirun.py:336
std::begin
T begin(T... args)
std
STL namespace.
Gaudi::Utils::MapBase
Definition: MapBase.h:52
GaudiUtils::Map::iterator
map_type::iterator iterator
Definition: Map.h:107
GaudiUtils::Map::end
const_iterator end() const
Definition: Map.h:143
GaudiUtils::Map::max_size
size_type max_size() const
Definition: Map.h:200
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:178
GaudiUtils::Map::operator[]
mapped_type & operator[](const key_type &key)
Definition: Map.h:153
MapBase.h
Gaudi::Utils::MapBase::throw_out_of_range_exception
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:38
std::end
T end(T... args)
GaudiUtils
Definition: Allocator.h:72
GaudiUtils::Map::argument_type
K argument_type
Definition: Map.h:100
GaudiUtils::Map::update
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:283
GaudiUtils::Map::size_type
map_type::size_type size_type
Definition: Map.h:105
GaudiUtils::Map::~Map
virtual ~Map()=default
Virtual destructor. You can inherit from this map type.
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiUtils::Map::s_null_value
static const result_type s_null_value
Definition: Map.h:115
GaudiUtils::Map::mapped_type
T mapped_type
Definition: Map.h:97
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:174
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39
std::next
T next(T... args)