| The Gaudi Framework v32r1 (f65d50dc)

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;
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 // 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>
164 std::pair<iterator, bool> emplace( Args&&... 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>
305 const typename Map<K, T, M>::result_type Map<K, T, M>::s_null_value = typename Map<K, T, M>::result_type();
306 // ==========================================================================
307 } // namespace GaudiUtils
308 // ============================================================================
309
310 // ============================================================================
311 // The END
312 // ============================================================================
313 #endif // GAUDIKERNEL_MAP_H
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(value_type &&val)
Definition: Map.h:171
GaudiUtils::Map::at
const result_type & at(const argument_type &key) const
checked access to the map
Definition: Map.h:254
GaudiUtils::Map
Extension of the STL map.
Definition: Map.h:81
GaudiUtils::Map::end
const_iterator end() const
Definition: Map.h:133
GaudiUtils::Map::argument_type
K argument_type
Definition: Map.h:90
GaudiUtils::Map::equal_range
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: Map.h:158
GaudiUtils::Map::key_type
K key_type
Definition: Map.h:86
GaudiUtils::Map::s_null_value
static const result_type s_null_value
Definition: Map.h:105
GaudiUtils::Map::Map
Map(In &&first, In &&last)
Construct from a subset.
Definition: Map.h:120
std
STL namespace.
GaudiUtils::Map::size_type
map_type::size_type size_type
Definition: Map.h:95
std::end
T end(T... args)
GaudiUtils::Map::erase
size_type erase(const key_type &key)
Definition: Map.h:183
GaudiUtils::Map::m_map
map_type m_map
Definition: Map.h:104
GaudiUtils::Map::insert
iterator insert(iterator, ValueType &&val)
Definition: Map.h:179
GaudiUtils::Map::upper_bound
const_iterator upper_bound(const key_type &key) const
Definition: Map.h:155
GaudiUtils::Map::operator[]
const mapped_type & operator[](const key_type &key) const
Access elements of a const Map.
Definition: Map.h:247
GaudiUtils::Map::update
void update(const key_type &key, const mapped_type &mapped)
Definition: Map.h:273
GaudiUtils::Map::lower_bound
const_iterator lower_bound(const key_type &key) const
Definition: Map.h:153
GaudiUtils::Map::insert
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:168
GaudiUtils::Map::operator[]
mapped_type & operator[](const key_type &key)
Definition: Map.h:143
GaudiUtils::Map::end
iterator end()
Definition: Map.h:130
GaudiUtils::Map::map_type
M map_type
Definition: Map.h:85
GaudiUtils::Map::erase
iterator erase(const_iterator first, const_iterator last)
Definition: Map.h:184
GaudiUtils::Map::key_at
const key_type & key_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:288
gaudirun.args
args
Definition: gaudirun.py:307
GaudiUtils::Map::upper_bound
iterator upper_bound(const key_type &key)
Definition: Map.h:154
std::next
T next(T... args)
GaudiUtils::Map::lower_bound
iterator lower_bound(const key_type &key)
Definition: Map.h:152
MapBase.h
GaudiUtils::Map::count
size_type count(const key_type &key) const
Definition: Map.h:150
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:147
Gaudi::Utils::MapBase::throw_out_of_range_exception
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:28
GaudiUtils::Map::~Map
virtual ~Map()=default
Virtual destructor. You can inherit from this map type.
GaudiUtils::Map::const_iterator
map_type::const_iterator const_iterator
Definition: Map.h:98
GaudiUtils::Map::find
const_iterator find(const key_type &key) const
Definition: Map.h:148
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:129
std::pair
GaudiUtils::Map::merge
Map & merge(const Map &other)
Merge two maps.
Definition: Map.h:262
GaudiUtils::Map::insert
void insert(In &&first, In &&last)
Definition: Map.h:175
GaudiUtils::Map::mapped_type
T mapped_type
Definition: Map.h:87
GaudiUtils::Map::iterator
map_type::iterator iterator
Definition: Map.h:97
GaudiUtils::Map::size
size_type size() const
Definition: Map.h:189
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:182
GaudiUtils::Map::swap
void swap(map_type &other)
Definition: Map.h:192
std::begin
T begin(T... args)
GaudiUtils::Map::value_at
const mapped_type & value_at(const size_type index) const
useful method for python decoration:
Definition: Map.h:297
GaudiUtils::Map::Map
Map(const map_type &other)
Constructor from a standard map.
Definition: Map.h:114
GaudiUtils::Map::value_type
std::pair< const K, T > value_type
Definition: Map.h:92
GaudiUtils::Map::empty
bool empty() const
Definition: Map.h:191
GaudiUtils::Map::merge
Map & merge(const Map< K1, K2, K3 > &other)
Merge two maps.
Definition: Map.h:268
GaudiUtils::Map::max_size
size_type max_size() const
Definition: Map.h:190
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
GaudiUtils::Map::clear
void clear()
Definition: Map.h:185
GaudiUtils::Map::begin
const_iterator begin() const
Definition: Map.h:132
Gaudi::Utils::MapBase
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi.
Definition: MapBase.h:42
GaudiUtils::Map::merge
Map & merge(const map_type &other)
Merge two maps.
Definition: Map.h:257
GaudiUtils::Map::const_reference
const value_type & const_reference
Definition: Map.h:93
GaudiUtils::Map::result_type
T result_type
Definition: Map.h:91
GaudiUtils::Map::Map
Map()=default
Standard constructor.
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:164
GaudiUtils::Map::equal_range
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: Map.h:157
GaudiUtils::Map::operator()
const result_type & operator()(const argument_type &key) const
Allow to use Map as an unary function.
Definition: Map.h:218
std::unordered_map< Gaudi::StringKey, Node, Hash< Gaudi::StringKey > >