The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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>
15#include <map>
16
17namespace GaudiUtils {
82 template <typename K, typename T, typename M = std::map<K, T>>
83 class Map : public Gaudi::Utils::MapBase {
84 public:
85 // ---- types
86 typedef M map_type;
87 typedef K key_type;
88 typedef T mapped_type;
89
90 // -- from unary_function
91 typedef K argument_type;
92 typedef T result_type;
93 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
101 protected:
104
105 public:
106 Map() = default;
107
109 Map( const map_type& other ) : m_map( other ) {}
110
112 template <typename In>
113 Map( In&& first, In&& last ) : m_map( std::forward<In>( first ), std::forward<In>( last ) ) {}
114
116 virtual ~Map() = default;
117
118 // ---- std::map interface
119
120 // -- iterators
121
122 inline iterator begin() { return m_map.begin(); }
123 inline iterator end() { return m_map.end(); }
124
125 inline const_iterator begin() const { return m_map.begin(); }
126 inline const_iterator end() const { return m_map.end(); }
127
128 // -- subscription
129
130 inline mapped_type& operator[]( const key_type& key ) { return m_map[key]; }
131
132 // -- map operations
133
134 inline iterator find( const key_type& key ) { return m_map.find( key ); }
135 inline const_iterator find( const key_type& key ) const { return m_map.find( key ); }
136
137 inline size_type count( const key_type& key ) const { return m_map.count( key ); }
138
139 inline iterator lower_bound( const key_type& key ) { return m_map.lower_bound( key ); }
140 inline const_iterator lower_bound( const key_type& key ) const { return m_map.lower_bound( key ); }
141 inline iterator upper_bound( const key_type& key ) { return m_map.upper_bound( key ); }
142 inline const_iterator upper_bound( const key_type& key ) const { return m_map.upper_bound( key ); }
143
144 inline std::pair<iterator, iterator> equal_range( const key_type& key ) { return m_map.equal_range( key ); }
145 inline std::pair<const_iterator, const_iterator> equal_range( const key_type& key ) const {
146 return m_map.equal_range( key );
147 }
148
149 // -- list operations
150 template <class... Args>
151 std::pair<iterator, bool> emplace( Args&&... args ) {
152 return m_map.emplace( std::forward<Args>( args )... );
153 }
154 template <typename ValueType>
155 inline std::pair<iterator, bool> insert( ValueType&& val ) {
156 return m_map.insert( std::forward<ValueType>( val ) );
157 }
158 inline std::pair<iterator, bool> insert( value_type&& val ) {
159 return m_map.insert( std::forward<value_type>( val ) );
160 }
161 template <typename In>
162 inline void insert( In&& first, In&& last ) {
163 m_map.insert( std::forward<In>( first ), std::forward<In>( last ) );
164 }
165 template <typename ValueType>
166 inline iterator insert( iterator /* pos */, ValueType&& val ) {
167 return m_map.insert( /* pos, */ std::forward<ValueType>( val ) ).first;
168 }
169 inline iterator erase( const_iterator pos ) { return m_map.erase( pos ); }
170 inline size_type erase( const key_type& key ) { return m_map.erase( key ); }
171 inline iterator erase( const_iterator first, const_iterator last ) { return m_map.erase( first, last ); }
172 inline void clear() { m_map.clear(); }
173
174 // -- container operations
175
176 inline size_type size() const { return m_map.size(); }
177 inline size_type max_size() const { return m_map.max_size(); }
178 inline bool empty() const { return size() == 0; }
179 inline void swap( map_type& other ) { m_map.swap( other ); }
180
181 // ---- extra functionalities
182
205 inline const result_type& operator()( const argument_type& key ) const {
206 // static const result_type s_null_value;
207 auto it = m_map.find( key );
209 return it != m_map.end() ? it->second : s_null_value;
210 }
211
233 inline const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
239 inline const result_type& at( const argument_type& key ) const { return m_map.at( key ); }
241 inline Map& merge( const map_type& other ) {
242 m_map.insert( std::begin( other ), std::end( other ) );
243 return *this;
244 }
245
246 inline Map& merge( const Map& other ) {
247 m_map.insert( std::begin( other ), std::end( other ) );
248 return *this;
249 }
250
251 template <class K1, class K2, class K3>
252 inline Map& merge( const Map<K1, K2, K3>& other ) {
253 m_map.insert( std::begin( other ), std::end( other ) );
254 return *this;
255 }
256 // update the key
257 void update( const key_type& key, const mapped_type& mapped ) { ( *this )[key] = mapped; }
258
259 public:
261 inline operator map_type&() { return m_map; }
262 inline operator const map_type&() const { return m_map; }
268 const key_type& key_at( const size_type index ) const {
269 if ( index >= size() ) { this->throw_out_of_range_exception(); }
270 return std::next( this->begin(), index )->first;
271 }
272
277 const mapped_type& value_at( const size_type index ) const {
278 if ( index >= size() ) { this->throw_out_of_range_exception(); }
279 return std::next( this->begin(), index )->second;
280 }
281
283 friend std::ostream& operator<<( std::ostream& s, const GaudiUtils::Map<K, T, M>& m ) {
284 return s << static_cast<const M&>( m );
285 }
286 };
287 template <typename K, typename T, typename M>
289
290} // namespace GaudiUtils
Provide serialization function (output only) for some common STL classes (vectors,...
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi.
Definition MapBase.h:45
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition MapBase.cpp:23
Extension of the STL map.
Definition Map.h:83
std::pair< iterator, bool > insert(value_type &&val)
Definition Map.h:158
const result_type & at(const argument_type &key) const
checked access to the map
Definition Map.h:239
map_type::const_iterator const_iterator
Definition Map.h:99
void clear()
Definition Map.h:172
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition Map.h:145
iterator insert(iterator, ValueType &&val)
Definition Map.h:166
Map(In &&first, In &&last)
Construct from a subset.
Definition Map.h:113
Map & merge(const Map &other)
Merge two maps.
Definition Map.h:246
K argument_type
Definition Map.h:91
const value_type & const_reference
Definition Map.h:94
void update(const key_type &key, const mapped_type &mapped)
Definition Map.h:257
std::pair< const K, T > value_type
Definition Map.h:93
const_iterator begin() const
Definition Map.h:125
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition Map.h:144
void swap(map_type &other)
Definition Map.h:179
std::pair< iterator, bool > insert(ValueType &&val)
Definition Map.h:155
iterator lower_bound(const key_type &key)
Definition Map.h:139
std::pair< iterator, bool > emplace(Args &&... args)
Definition Map.h:151
iterator find(const key_type &key)
Definition Map.h:134
static const result_type s_null_value
Definition Map.h:103
virtual ~Map()=default
Virtual destructor. You can inherit from this map type.
size_type count(const key_type &key) const
Definition Map.h:137
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition Map.h:277
Map(const map_type &other)
Constructor from a standard map.
Definition Map.h:109
size_type erase(const key_type &key)
Definition Map.h:170
iterator erase(const_iterator pos)
Definition Map.h:169
bool empty() const
Definition Map.h:178
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition Map.h:252
friend std::ostream & operator<<(std::ostream &s, const GaudiUtils::Map< K, T, M > &m)
Serialize a GaudiUtils::Map in a python like format. E.g. "{a: 1, b: 2}".
Definition Map.h:283
const mapped_type & operator[](const key_type &key) const
Access elements of a const Map.
Definition Map.h:233
iterator begin()
Definition Map.h:122
iterator upper_bound(const key_type &key)
Definition Map.h:141
void insert(In &&first, In &&last)
Definition Map.h:162
map_type::size_type size_type
Definition Map.h:96
const_iterator lower_bound(const key_type &key) const
Definition Map.h:140
map_type::iterator iterator
Definition Map.h:98
const_iterator upper_bound(const key_type &key) const
Definition Map.h:142
size_type size() const
Definition Map.h:176
mapped_type & operator[](const key_type &key)
Definition Map.h:130
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition Map.h:205
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition Map.h:268
size_type max_size() const
Definition Map.h:177
const_iterator end() const
Definition Map.h:126
T result_type
Definition Map.h:92
T mapped_type
Definition Map.h:88
const_iterator find(const key_type &key) const
Definition Map.h:135
iterator end()
Definition Map.h:123
Map & merge(const map_type &other)
Merge two maps.
Definition Map.h:241
iterator erase(const_iterator first, const_iterator last)
Definition Map.h:171
STL namespace.