The Gaudi Framework  master (d98a2936)
Map.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 #include <GaudiKernel/MapBase.h>
14 #include <map>
15 
16 namespace GaudiUtils {
81  template <typename K, typename T, typename M = std::map<K, T>>
82  class Map : public Gaudi::Utils::MapBase {
83  public:
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;
92  typedef std::pair<const K, T> value_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 
100  protected:
102  static const result_type s_null_value;
103 
104  public:
105  Map() = default;
106 
108  Map( const map_type& other ) : m_map( other ) {}
109 
111  template <typename In>
112  Map( In&& first, In&& last ) : m_map( std::forward<In>( first ), std::forward<In>( last ) ) {}
113 
115  virtual ~Map() = default;
116 
117  // ---- std::map interface
118 
119  // -- iterators
120 
121  inline iterator begin() { return m_map.begin(); }
122  inline iterator end() { return m_map.end(); }
123 
124  inline const_iterator begin() const { return m_map.begin(); }
125  inline const_iterator end() const { return m_map.end(); }
126 
127  // -- subscription
128 
129  inline mapped_type& operator[]( const key_type& key ) { return m_map[key]; }
130 
131  // -- map operations
132 
133  inline iterator find( const key_type& key ) { return m_map.find( key ); }
134  inline const_iterator find( const key_type& key ) const { return m_map.find( key ); }
135 
136  inline size_type count( const key_type& key ) const { return m_map.count( key ); }
137 
138  inline iterator lower_bound( const key_type& key ) { return m_map.lower_bound( key ); }
139  inline const_iterator lower_bound( const key_type& key ) const { return m_map.lower_bound( key ); }
140  inline iterator upper_bound( const key_type& key ) { return m_map.upper_bound( key ); }
141  inline const_iterator upper_bound( const key_type& key ) const { return m_map.upper_bound( key ); }
142 
143  inline std::pair<iterator, iterator> equal_range( const key_type& key ) { return m_map.equal_range( key ); }
144  inline std::pair<const_iterator, const_iterator> equal_range( const key_type& key ) const {
145  return m_map.equal_range( key );
146  }
147 
148  // -- list operations
149  template <class... Args>
150  std::pair<iterator, bool> emplace( Args&&... args ) {
151  return m_map.emplace( std::forward<Args>( args )... );
152  }
153  template <typename ValueType>
154  inline std::pair<iterator, bool> insert( ValueType&& val ) {
155  return m_map.insert( std::forward<ValueType>( val ) );
156  }
157  inline std::pair<iterator, bool> insert( value_type&& val ) {
158  return m_map.insert( std::forward<value_type>( val ) );
159  }
160  template <typename In>
161  inline void insert( In&& first, In&& last ) {
162  m_map.insert( std::forward<In>( first ), std::forward<In>( last ) );
163  }
164  template <typename ValueType>
165  inline iterator insert( iterator /* pos */, ValueType&& val ) {
166  return m_map.insert( /* pos, */ std::forward<ValueType>( val ) ).first;
167  }
168  inline iterator erase( const_iterator pos ) { return m_map.erase( pos ); }
169  inline size_type erase( const key_type& key ) { return m_map.erase( key ); }
170  inline iterator erase( const_iterator first, const_iterator last ) { return m_map.erase( first, last ); }
171  inline void clear() { m_map.clear(); }
172 
173  // -- container operations
174 
175  inline size_type size() const { return m_map.size(); }
176  inline size_type max_size() const { return m_map.max_size(); }
177  inline bool empty() const { return size() == 0; }
178  inline void swap( map_type& other ) { m_map.swap( other ); }
179 
180  // ---- extra functionalities
181 
204  inline const result_type& operator()( const argument_type& key ) const {
205  // static const result_type s_null_value;
206  auto it = m_map.find( key );
208  return it != m_map.end() ? it->second : s_null_value;
209  }
232  inline const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
238  inline const result_type& at( const argument_type& key ) const { return m_map.at( key ); }
240  inline Map& merge( const map_type& other ) {
241  m_map.insert( std::begin( other ), std::end( other ) );
242  return *this;
243  }
245  inline Map& merge( const Map& other ) {
246  m_map.insert( std::begin( other ), std::end( other ) );
247  return *this;
248  }
250  template <class K1, class K2, class K3>
251  inline Map& merge( const Map<K1, K2, K3>& other ) {
252  m_map.insert( std::begin( other ), std::end( other ) );
253  return *this;
254  }
255  // update the key
256  void update( const key_type& key, const mapped_type& mapped ) { ( *this )[key] = mapped; }
257 
258  public:
260  inline operator map_type&() { return m_map; }
261  inline operator const map_type&() const { return m_map; }
267  const key_type& key_at( const size_type index ) const {
268  if ( index >= size() ) { this->throw_out_of_range_exception(); }
269  return std::next( this->begin(), index )->first;
270  }
276  const mapped_type& value_at( const size_type index ) const {
277  if ( index >= size() ) { this->throw_out_of_range_exception(); }
278  return std::next( this->begin(), index )->second;
279  }
280  };
281  template <typename K, typename T, typename M>
283 } // namespace GaudiUtils
GaudiUtils::Map::erase
size_type erase(const key_type &key)
Definition: Map.h:169
GaudiUtils::Map::map_type
M map_type
Definition: Map.h:85
GaudiUtils::Map::m_map
map_type m_map
Definition: Map.h:101
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:133
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
GaudiUtils::Map::upper_bound
iterator upper_bound(const key_type &key)
Definition: Map.h:140
GaudiUtils::Map::Map
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:108
GaudiUtils::Map::count
size_type count(const key_type &key) const
Definition: Map.h:136
GaudiUtils::Map::upper_bound
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:141
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(value_type &&val)
Definition: Map.h:157
GaudiUtils::Map::insert
iterator insert(iterator, ValueType &&val)
Definition: Map.h:165
GaudiUtils::Map::swap
void swap(map_type &other)
Definition: Map.h:178
GaudiUtils::Map::begin
const_iterator begin() const
Definition: Map.h:124
GaudiUtils::Map::operator()
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:204
GaudiUtils::Map::value_at
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:276
GaudiUtils::Map::operator[]
const mapped_type & operator[](const key_type &key) const
Access elements of a const Map.
Definition: Map.h:232
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:121
GaudiUtils::Map::empty
bool empty() const
Definition: Map.h:177
GaudiUtils::Map::const_iterator
map_type::const_iterator const_iterator
Definition: Map.h:98
GaudiUtils::Map::merge
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:245
GaudiUtils::Map::erase
iterator erase(const_iterator first, const_iterator last)
Definition: Map.h:170
GaudiUtils::Map::clear
void clear()
Definition: Map.h:171
GaudiUtils::Map::key_at
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:267
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
GaudiUtils::Map::lower_bound
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:139
GaudiUtils::Map::merge
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:240
GaudiUtils::Map::merge
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition: Map.h:251
GaudiUtils::Map::value_type
std::pair< const K, T > value_type
Definition: Map.h:92
GaudiUtils::Map::insert
void insert(In &&first, In &&last)
Definition: Map.h:161
GaudiUtils::Map::Map
Map()=default
GaudiUtils::Map::lower_bound
iterator lower_bound(const key_type &key)
Definition: Map.h:138
GaudiUtils::Map::at
const result_type & at(const argument_type &key) const
checked access to the map
Definition: Map.h:238
GaudiUtils::Map
Definition: Map.h:82
GaudiUtils::Map::const_reference
const value_type & const_reference
Definition: Map.h:93
GaudiUtils::Map::find
const_iterator find(const key_type &key) const
Definition: Map.h:134
GaudiUtils::Map::result_type
T result_type
Definition: Map.h:91
GaudiUtils::Map::Map
Map(In &&first, In &&last)
Construct from a subset.
Definition: Map.h:112
GaudiUtils::Map::end
iterator end()
Definition: Map.h:122
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:168
GaudiUtils::Map::key_type
K key_type
Definition: Map.h:86
GaudiUtils::Map::equal_range
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:144
GaudiUtils::Map::equal_range
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:143
GaudiUtils::Map::size
size_type size() const
Definition: Map.h:175
gaudirun.args
args
Definition: gaudirun.py:336
Gaudi::Utils::MapBase
Definition: MapBase.h:45
GaudiUtils::Map::iterator
map_type::iterator iterator
Definition: Map.h:97
GaudiUtils::Map::end
const_iterator end() const
Definition: Map.h:125
GaudiUtils::Map::max_size
size_type max_size() const
Definition: Map.h:176
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:154
GaudiUtils::Map::operator[]
mapped_type & operator[](const key_type &key)
Definition: Map.h:129
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:23
IOTest.end
end
Definition: IOTest.py:125
GaudiUtils
Definition: Allocator.h:62
GaudiUtils::Map::argument_type
K argument_type
Definition: Map.h:90
GaudiUtils::Map::update
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:256
GaudiUtils::Map::size_type
map_type::size_type size_type
Definition: Map.h:95
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:102
GaudiUtils::Map::mapped_type
T mapped_type
Definition: Map.h:87
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:150
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