The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
VectorMap.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>
16#include <algorithm>
17#include <functional>
18#include <initializer_list>
19#include <ostream>
20#include <utility>
21#include <vector>
22
23namespace GaudiUtils {
98 template <class KEY, class VALUE, class KEYCOMPARE = std::less<const KEY>,
99 class ALLOCATOR = std::allocator<std::pair<KEY, VALUE>>>
101 public:
103 typedef KEY key_type;
105 typedef VALUE mapped_type;
107 typedef KEYCOMPARE key_compare;
109 typedef std::pair<key_type, mapped_type> value_type;
110
111 public:
113 typedef ALLOCATOR allocator_type;
115 typedef typename ALLOCATOR::value_type const& reference;
117 typedef typename ALLOCATOR::value_type const& const_reference;
119 typedef typename ALLOCATOR::size_type size_type;
121 typedef typename ALLOCATOR::difference_type difference_type;
122
123 public:
125 typedef std::vector<value_type, allocator_type> _vector;
126
127 protected:
129 typedef typename _vector::iterator _iterator;
130
131 public:
133 typedef typename _vector::const_iterator iterator;
135 typedef typename _vector::const_iterator const_iterator;
137 typedef std::reverse_iterator<iterator> reverse_iterator;
139 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141 typedef std::pair<iterator, iterator> iterators;
143 typedef std::pair<iterator, bool> result_type;
144
145 public:
150 struct _compare_type : public key_compare {
151 public:
153 _compare_type( const key_compare& cmp ) : key_compare( cmp ) {}
157 bool operator()( const key_type& k1, const key_type& k2 ) const {
158 return this->key_compare::operator()( k1, k2 );
159 }
160
161 bool operator()( const value_type& v1, const value_type& v2 ) const { return operator()( v1.first, v2.first ); }
163 bool operator()( const key_type& k, const value_type& v ) const { return operator()( k, v.first ); }
165 bool operator()( const value_type& v, const key_type& k ) const { return operator()( v.first, k ); }
166 };
167
169
170 public:
171 // sequential access (only const-versions!)
173 iterator begin() const { return m_vct.begin(); }
175 iterator end() const { return m_vct.end(); }
177 reverse_iterator rbegin() const { return m_vct.rbegin(); }
179 reverse_iterator rend() const { return m_vct.rend(); }
180 // list operations : erase & insert
184 void erase( iterator pos ) { m_vct.erase( iter( pos ) ); }
201 size_type erase( const key_type& key ) {
202 iterator pos = find( key );
203 if ( end() == pos ) { return 0; }
204 erase( pos );
205 return 1;
206 }
207
213 m_vct.erase( iter( first ), iter( last ) );
214 return last - first;
215 }
216
236 template <class TYPE>
237 size_type erase( TYPE first, TYPE last ) {
238 size_type res = 0;
239 for ( ; first != last; ++first ) { res += erase( *first ); }
240 return res;
241 }
242
283 result_type insert( const key_type& key, const mapped_type& mapped ) { return insert( value_type( key, mapped ) ); }
324 result_type insert( const value_type& value ) {
325 bool found = true;
326 _iterator result = lower_bound( value.first );
327 if ( end() == result || compare( value.first, result->first ) ) {
328 result = m_vct.insert( result, value );
329 found = false;
330 }
331 return result_type( iter( result ), !found );
332 }
333
341 result_type insert( iterator pos, const value_type& value ) {
342 if ( pos != end() && compare( *pos, value ) &&
343 ( pos == end() - 1 || ( !compare( value, *( pos + 1 ) ) && compare( *( pos + 1 ), value ) ) ) ) {
344 return result_type( m_vct.insert( iter( pos ), value ), true );
345 }
346 return insert( value );
347 }
348
357 result_type insert( iterator pos, const key_type& key, const mapped_type& mapped ) {
358 return insert( pos, value_type( key, mapped ) );
359 }
360
365 template <class PAIRS>
366 void insert( PAIRS first, PAIRS last ) {
367 for ( ; first != last; ++first ) { insert( *first ); }
368 }
369
376 template <class KEYS, class VALUES>
377 void insert( KEYS kf, KEYS kl, VALUES vf ) {
378 for ( ; kf != kl; ++kf, ++vf ) { insert( *kf, *vf ); }
379 }
380 // map operations: lookup, count, ...
404 iterator find( const key_type& key ) const {
405 iterator res = lower_bound( key );
406 if ( end() != res && compare( key, res->first ) ) { res = end(); }
407 return res;
408 }
409
424 size_type count( const key_type& key ) const { return end() == find( key ) ? 0 : 1; }
425 iterator lower_bound( const key_type& key ) const { return std::lower_bound( begin(), end(), key, compare() ); }
426 iterator upper_bound( const key_type& key ) const { return std::upper_bound( begin(), end(), key, compare() ); }
427 iterators equal_range( const key_type& key ) const { return std::equal_range( begin(), end(), key, compare() ); }
428 // general container operations :
430 bool empty() const { return m_vct.empty(); }
432 size_type size() const { return m_vct.size(); }
434 size_type max_size() const { return m_vct.max_size(); }
436 void clear() { m_vct.clear(); }
438 void reserve( size_type num ) { m_vct.reserve( num ); }
440 void swap( VectorMap& other ) { std::swap( m_vct, other.m_vct ); }
441 // The basic comparison operators for container
443 bool operator==( const VectorMap& other ) const { return m_vct == other.m_vct; }
445 bool operator<( const VectorMap& other ) const { return m_vct < other.m_vct; }
446 // The derived comparison operators for container
447 friend bool operator>( const VectorMap& left, const VectorMap& right ) { return right < left; }
448 friend bool operator!=( const VectorMap& left, const VectorMap& right ) { return !( left == right ); }
449 friend bool operator>=( const VectorMap& left, const VectorMap& right ) { return !( left < right ); }
450 friend bool operator<=( const VectorMap& left, const VectorMap& right ) { return !( right < left ); }
480 bool update( const key_type& key, const mapped_type& mapped ) {
481 _iterator result = lower_bound( key );
482 if ( end() == result || compare( key, result->first ) ) {
483 result = m_vct.insert( result, value_type( key, mapped ) );
484 return false;
485 } else {
486 result->second = mapped;
487 }
488 //
489 return true;
490 }
491
519 bool update( const value_type& val ) { return update( val.first, val.second ); }
551 const mapped_type& operator()( const key_type& key ) const {
552 static const mapped_type s_default = mapped_type();
553 iterator res = find( key );
554 if ( end() == res ) { return s_default; }
555 return res->second;
556 }
557
587 const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
605 const mapped_type& at( const key_type& key ) const {
606 iterator res = find( key );
607 if ( end() == res ) this->throw_out_of_range_exception();
608 return res->second; // cppcheck-suppress derefInvalidIteratorRedundantCheck; the above throws
609 }
610
611 public:
612 // Constructors, destructors, etc.
617 VectorMap( const allocator_type& alloc = allocator_type() ) : m_vct( alloc ) {}
624 template <class INPUT>
625 VectorMap( INPUT first, INPUT last, const allocator_type& alloc = allocator_type() ) : m_vct( first, last, alloc ) {
626 std::sort( m_vct.begin(), m_vct.end(), compare() );
627 }
628
633 VectorMap( std::initializer_list<value_type> first, const allocator_type& alloc = allocator_type() )
634 : m_vct( first, alloc ) {
635 std::sort( m_vct.begin(), m_vct.end(), compare() );
636 }
637
638 public:
639 // The specific public accessors
641 const compare_type& compare() const {
642 static const compare_type s_cmp = compare_type();
643 return s_cmp;
644 }
645
646 const key_compare& compare_key() const { return compare(); }
648 friend std::ostream& operator<<( std::ostream& str, const VectorMap& /* obj */ ) { return str; }
649
650 public:
652 inline VectorMap& merge( const VectorMap& right ) {
653 for ( const auto& i : right ) { update( i.first, i.second ); }
654 return *this;
655 }
656
657 template <class K1, class K2, class K3, class K4>
658 inline VectorMap& merge( const VectorMap<K1, K2, K3, K4>& right ) {
659 for ( const auto& i : right ) { update( i.first, i.second ); }
660 return *this;
661 }
662
663 public:
669 const key_type& key_at( const size_t index ) const {
670 if ( index >= size() ) { this->throw_out_of_range_exception(); }
671 auto it = this->begin();
672 std::advance( it, index );
673 return it->first;
674 }
675
680 const mapped_type& value_at( const size_t index ) const {
681 if ( index >= size() ) { this->throw_out_of_range_exception(); }
682 auto it = this->begin();
683 std::advance( it, index );
684 return it->second;
685 }
686
687 protected:
688 // Pure technical helper functions
694 template <class TYPE1, class TYPE2>
695 bool compare( const TYPE1& obj1, const TYPE2& obj2 ) const {
696 return compare()( obj1, obj2 );
697 }
698
700 return std::lower_bound( m_vct.begin(), m_vct.end(), key, compare() );
701 }
702
704 auto result = m_vct.begin();
705 std::advance( result, std::distance( begin(), p ) );
706 return result;
707 }
708
710 auto result = begin();
711 std::advance( result, std::distance( m_vct.begin(), p ) );
712 return result;
713 }
714
715 private:
717 _vector m_vct; // the underlying sorted vector of (key,mapped) pairs
718 };
719} // namespace GaudiUtils
720namespace std {
725 template <class KEY, class VALUE, class KEYCOMPARE, class ALLOCATOR>
730} // namespace std
731namespace Gaudi {
732 namespace Parsers {
742 GAUDI_API StatusCode parse( GaudiUtils::VectorMap<std::string, double>& result, std::string_view input );
753 GAUDI_API StatusCode parse( GaudiUtils::VectorMap<Gaudi::StringKey, double>& result, std::string_view input );
754 } // namespace Parsers
755} // namespace Gaudi
#define GAUDI_API
Definition Kernel.h:49
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
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition VectorMap.h:100
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition VectorMap.h:137
friend std::ostream & operator<<(std::ostream &str, const VectorMap &)
printout to ostream - not implemented
Definition VectorMap.h:648
size_type size() const
number of elements
Definition VectorMap.h:432
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition VectorMap.h:175
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition VectorMap.h:121
size_type count(const key_type &key) const
count number of elements with the certain key
Definition VectorMap.h:424
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition VectorMap.h:139
result_type insert(iterator pos, const value_type &value)
insert the element with some guess about its new position With the right guess the method could be mo...
Definition VectorMap.h:341
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition VectorMap.h:449
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition VectorMap.h:450
_iterator lower_bound(const key_type &key)
'lower-bound' - non-const version
Definition VectorMap.h:699
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition VectorMap.h:680
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition VectorMap.h:445
void swap(VectorMap &other)
swap function, which 'swaps' the content of two containers
Definition VectorMap.h:440
ALLOCATOR::value_type const & reference
the types to conform STL
Definition VectorMap.h:115
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition VectorMap.h:625
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition VectorMap.h:135
std::pair< iterator, bool > result_type
visible iterator pait
Definition VectorMap.h:143
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition VectorMap.h:109
const compare_type & compare() const
get the comparison criteria itself
Definition VectorMap.h:641
ALLOCATOR::size_type size_type
the types to conform STL
Definition VectorMap.h:119
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition VectorMap.h:695
void erase(iterator pos)
erase the element using the iterator
Definition VectorMap.h:184
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition VectorMap.h:669
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition VectorMap.h:173
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition VectorMap.h:237
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition VectorMap.h:448
size_type max_size() const
maximal allowed size
Definition VectorMap.h:434
void reserve(size_type num)
reserve the space in the container for at least 'num' elements
Definition VectorMap.h:438
KEYCOMPARE key_compare
comparison of keys
Definition VectorMap.h:107
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition VectorMap.h:113
bool update(const value_type &val)
forced insertion of the key/mapped pair The method acts like "insert" but it DOES overwrite the mappe...
Definition VectorMap.h:519
KEY key_type
the actual type of key
Definition VectorMap.h:103
void insert(KEYS kf, KEYS kl, VALUES vf)
insert into the container the elements from 2 "parallel" sequences
Definition VectorMap.h:377
iterators equal_range(const key_type &key) const
Definition VectorMap.h:427
iterator upper_bound(const key_type &key) const
Definition VectorMap.h:426
iterator iter(_iterator p)
the conversion from 'non-const' to 'const' iterator
Definition VectorMap.h:709
bool update(const key_type &key, const mapped_type &mapped)
forced insertion of the key/mapped pair The method acts like "insert" but it DOES overwrite the exist...
Definition VectorMap.h:480
result_type insert(iterator pos, const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container With the right guess the method could be more efficien...
Definition VectorMap.h:357
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition VectorMap.h:283
_vector::iterator _iterator
the regular iterator (no export)
Definition VectorMap.h:129
const mapped_type & operator()(const key_type &key) const
access to element by key (const version) there is no container increment for missing keys
Definition VectorMap.h:551
const key_compare & compare_key() const
get the comparison criteria for keys
Definition VectorMap.h:646
std::pair< iterator, iterator > iterators
visible iterator pait
Definition VectorMap.h:141
void insert(PAIRS first, PAIRS last)
insert the sequence of elements into the container
Definition VectorMap.h:366
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition VectorMap.h:168
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition VectorMap.h:177
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition VectorMap.h:179
size_type erase(const key_type &key)
erase the element using the key
Definition VectorMap.h:201
_iterator iter(iterator p)
the conversion from 'const' to 'non-const' iterator
Definition VectorMap.h:703
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition VectorMap.h:125
void clear()
clear the container
Definition VectorMap.h:436
_vector::const_iterator iterator
visible const_iterator (exported)
Definition VectorMap.h:133
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition VectorMap.h:633
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition VectorMap.h:212
iterator find(const key_type &key) const
find the element by key
Definition VectorMap.h:404
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition VectorMap.h:658
ALLOCATOR::value_type const & const_reference
the types to conform STL
Definition VectorMap.h:117
result_type insert(const value_type &value)
insert the (key,value) pair into the container
Definition VectorMap.h:324
bool empty() const
empty container ?
Definition VectorMap.h:430
VectorMap & merge(const VectorMap &right)
merge two maps
Definition VectorMap.h:652
const mapped_type & operator[](const key_type &key) const
access to element by key (const version) there is no container increment for missing keys
Definition VectorMap.h:587
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition VectorMap.h:443
iterator lower_bound(const key_type &key) const
Definition VectorMap.h:425
const mapped_type & at(const key_type &key) const
checked access to elements by key throw std::out_of_range exception for non-existing keys
Definition VectorMap.h:605
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition VectorMap.h:447
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition VectorMap.h:617
VALUE mapped_type
the actual type of value
Definition VectorMap.h:105
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
STL namespace.
void swap(GaudiUtils::VectorMap< KEY, VALUE, KEYCOMPARE, ALLOCATOR > &left, GaudiUtils::VectorMap< KEY, VALUE, KEYCOMPARE, ALLOCATOR > &right)
the definition of specialized algorithm for swapping
Definition VectorMap.h:726
The actual structure used to compare the elements Only "key" is important for comparison.
Definition VectorMap.h:150
_compare_type()
default constructor
Definition VectorMap.h:155
bool operator()(const key_type &k, const value_type &v) const
compare key and pair (key,mapped): use compare by keys
Definition VectorMap.h:163
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition VectorMap.h:153
bool operator()(const value_type &v1, const value_type &v2) const
compare pairs (key,mapped): use compare by keys
Definition VectorMap.h:161
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition VectorMap.h:157
bool operator()(const value_type &v, const key_type &k) const
compare pair (key,mapped) and the key: use compare by keys
Definition VectorMap.h:165