The Gaudi Framework  master (fcd9f667)
Loading...
Searching...
No Matches
VectorMap.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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 <concepts>
18#include <functional>
19#include <initializer_list>
20#include <ostream>
21#include <type_traits>
22#include <utility>
23#include <vector>
24
25namespace GaudiUtils {
100
101 namespace detail {
102 // Comp must order K with Key in both directions, and be transparent
103 template <typename K, typename Key, typename Comp>
105 (std::same_as<std::remove_cvref_t<K>, std::remove_cvref_t<Key>> && std::strict_weak_order<Comp, Key, Key>) ||
106 ( requires { typename Comp::is_transparent; } && std::strict_weak_order<Comp, K, Key> &&
107 std::strict_weak_order<Comp, Key, K> );
108 } // namespace detail
109
110 template <typename KEY, typename VALUE, typename KEYCOMPARE = std::less<>,
111 typename ALLOCATOR = std::allocator<std::pair<KEY, VALUE>>>
113 public:
115 typedef KEY key_type;
117 typedef VALUE mapped_type;
119 typedef KEYCOMPARE key_compare;
121 typedef std::pair<key_type, mapped_type> value_type;
122
123 public:
125 typedef ALLOCATOR allocator_type;
127 typedef typename ALLOCATOR::value_type const& reference;
129 typedef typename ALLOCATOR::value_type const& const_reference;
131 typedef typename ALLOCATOR::size_type size_type;
133 typedef typename ALLOCATOR::difference_type difference_type;
134
135 public:
137 typedef std::vector<value_type, allocator_type> _vector;
138
139 protected:
141 typedef typename _vector::iterator _iterator;
142
143 public:
145 typedef typename _vector::const_iterator iterator;
147 typedef typename _vector::const_iterator const_iterator;
149 typedef std::reverse_iterator<iterator> reverse_iterator;
151 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
153 typedef std::pair<iterator, iterator> iterators;
155 typedef std::pair<iterator, bool> result_type;
156
157 public:
162 struct _compare_type : public key_compare {
163 public:
164 using key_compare::key_compare;
166 using key_compare::operator();
168 bool operator()( const value_type& v1, const value_type& v2 ) const { return operator()( v1.first, v2.first ); }
170 template <detail::key_comparable<key_type, key_compare> K>
171 bool operator()( const K& k, const value_type& v ) const {
172 return operator()( k, v.first );
173 }
174
175 template <detail::key_comparable<key_type, key_compare> K>
176 bool operator()( const value_type& v, const K& k ) const {
177 return operator()( v.first, k );
178 }
179 };
180
182
183 public:
184 // sequential access (only const-versions!)
186 iterator begin() const { return m_vct.begin(); }
188 iterator end() const { return m_vct.end(); }
190 reverse_iterator rbegin() const { return m_vct.rbegin(); }
192 reverse_iterator rend() const { return m_vct.rend(); }
193 // list operations : erase & insert
197 void erase( iterator pos ) { m_vct.erase( iter( pos ) ); }
214 size_type erase( const key_type& key ) { return erase<key_type>( key ); }
215 template <detail::key_comparable<key_type, key_compare> K>
216 size_type erase( const K& key ) {
217 iterator pos = find( key );
218 if ( end() == pos ) { return 0; }
219 erase( pos );
220 return 1;
221 }
222
228 m_vct.erase( iter( first ), iter( last ) );
229 return last - first;
230 }
231
251 template <class TYPE>
252 size_type erase( TYPE first, TYPE last ) {
253 size_type res = 0;
254 for ( ; first != last; ++first ) { res += erase( *first ); }
255 return res;
256 }
257
298 result_type insert( const key_type& key, const mapped_type& mapped ) { return insert( value_type( key, mapped ) ); }
339 result_type insert( const value_type& value ) {
340 bool found = true;
341 _iterator result = lower_bound( value.first );
342 if ( end() == result || compare( value.first, result->first ) ) {
343 result = m_vct.insert( result, value );
344 found = false;
345 }
346 return result_type( iter( result ), !found );
347 }
348
356 result_type insert( iterator pos, const value_type& value ) {
357 if ( pos != end() && compare( *pos, value ) &&
358 ( pos == end() - 1 || ( !compare( value, *( pos + 1 ) ) && compare( *( pos + 1 ), value ) ) ) ) {
359 return result_type( m_vct.insert( iter( pos ), value ), true );
360 }
361 return insert( value );
362 }
363
373 return insert( pos, value_type( std::move( key ), std::move( mapped ) ) );
374 }
375
380 template <class PAIRS>
381 void insert( PAIRS first, PAIRS last ) {
382 for ( ; first != last; ++first ) { insert( *first ); }
383 }
384
391 template <class KEYS, class VALUES>
392 void insert( KEYS kf, KEYS kl, VALUES vf ) {
393 for ( ; kf != kl; ++kf, ++vf ) { insert( *kf, *vf ); }
394 }
395 // map operations: lookup, count, ...
419 iterator find( const key_type& key ) const { return find<key_type>( key ); }
420 template <detail::key_comparable<key_type, key_compare> K>
421 iterator find( const K& key ) const {
422 iterator res = lower_bound( key );
423 if ( end() != res && compare( key, res->first ) ) { res = end(); }
424 return res;
425 }
426
441
442 size_type count( const key_type& key ) const { return count<key_type>( key ); }
443 template <detail::key_comparable<key_type, key_compare> K>
444 size_type count( const K& key ) const {
445 return end() == find( key ) ? 0 : 1;
446 }
447 iterator lower_bound( const key_type& key ) const { return lower_bound<key_type>( key ); }
448 template <detail::key_comparable<key_type, key_compare> K>
449 iterator lower_bound( const K& key ) const {
450 return std::lower_bound( begin(), end(), key, compare() );
451 }
452 iterator upper_bound( const key_type& key ) const { return upper_bound<key_type>( key ); }
453 template <detail::key_comparable<key_type, key_compare> K>
454 iterator upper_bound( const K& key ) const {
455 return std::upper_bound( begin(), end(), key, compare() );
456 }
457 iterators equal_range( const key_type& key ) const { return equal_range<key_type>( key ); }
458 template <detail::key_comparable<key_type, key_compare> K>
459 iterators equal_range( const K& key ) const {
460 return std::equal_range( begin(), end(), key, compare() );
461 }
462 // general container operations :
464 bool empty() const { return m_vct.empty(); }
466 size_type size() const { return m_vct.size(); }
468 size_type max_size() const { return m_vct.max_size(); }
470 void clear() { m_vct.clear(); }
472 void reserve( size_type num ) { m_vct.reserve( num ); }
474 void swap( VectorMap& other ) { std::swap( m_vct, other.m_vct ); }
475 // The basic comparison operators for container
477 bool operator==( const VectorMap& other ) const { return m_vct == other.m_vct; }
479 auto operator<=>( const VectorMap& other ) const { return m_vct <=> other.m_vct; }
509 bool update( const key_type& key, const mapped_type& mapped ) { return update<key_type>( key, mapped ); }
510 template <detail::key_comparable<key_type, key_compare> K>
511 bool update( const K& key, const mapped_type& mapped ) {
512 _iterator result = lower_bound( key );
513 if ( end() == result || compare( key, result->first ) ) {
514 result = m_vct.insert( result, value_type( key, mapped ) );
515 return false;
516 } else {
517 result->second = mapped;
518 }
519 //
520 return true;
521 }
522
550 bool update( const value_type& val ) { return update( val.first, val.second ); }
582 const mapped_type& operator()( const key_type& key ) const { return this->template operator()<key_type>( key ); }
583 template <detail::key_comparable<key_type, key_compare> K>
584 const mapped_type& operator()( const K& key ) const {
585 static const mapped_type s_default = mapped_type();
586 iterator res = find( key );
587 if ( end() == res ) { return s_default; }
588 return res->second;
589 }
590
620 const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
621 template <detail::key_comparable<key_type, key_compare> K>
622 const mapped_type& operator[]( const K& key ) const {
623 return ( *this )( key );
624 }
625
642 const mapped_type& at( const key_type& key ) const { return at<key_type>( key ); }
643 template <detail::key_comparable<key_type, key_compare> K>
644 const mapped_type& at( const K& key ) const {
645 iterator res = find( key );
646 if ( end() == res ) this->throw_out_of_range_exception();
647 return res->second; // cppcheck-suppress derefInvalidIteratorRedundantCheck; the above throws
648 }
649
650 public:
651 // Constructors, destructors, etc.
656 VectorMap( const allocator_type& alloc = allocator_type() ) : m_vct( alloc ) {}
663 template <class INPUT>
664 VectorMap( INPUT first, INPUT last, const allocator_type& alloc = allocator_type() ) : m_vct( first, last, alloc ) {
665 std::sort( m_vct.begin(), m_vct.end(), compare() );
666 }
667
672 VectorMap( std::initializer_list<value_type> first, const allocator_type& alloc = allocator_type() )
673 : m_vct( first, alloc ) {
674 std::sort( m_vct.begin(), m_vct.end(), compare() );
675 }
676
677 public:
678 // The specific public accessors
680 const compare_type& compare() const {
681 static const compare_type s_cmp = compare_type();
682 return s_cmp;
683 }
684
685 const key_compare& compare_key() const { return compare(); }
687 friend std::ostream& operator<<( std::ostream& str, const VectorMap& /* obj */ ) { return str; }
688
689 public:
691 inline VectorMap& merge( const VectorMap& right ) {
692 for ( const auto& i : right ) { update( i.first, i.second ); }
693 return *this;
694 }
695
696 template <class K1, class K2, class K3, class K4>
697 inline VectorMap& merge( const VectorMap<K1, K2, K3, K4>& right ) {
698 for ( const auto& i : right ) { update( i.first, i.second ); }
699 return *this;
700 }
701
702 public:
708 const key_type& key_at( const size_t index ) const {
709 if ( index >= size() ) { this->throw_out_of_range_exception(); }
710 auto it = this->begin();
711 std::advance( it, index );
712 return it->first;
713 }
714
719 const mapped_type& value_at( const size_t index ) const {
720 if ( index >= size() ) { this->throw_out_of_range_exception(); }
721 auto it = this->begin();
722 std::advance( it, index );
723 return it->second;
724 }
725
726 protected:
727 // Pure technical helper functions
733 template <class TYPE1, class TYPE2>
734 bool compare( const TYPE1& obj1, const TYPE2& obj2 ) const {
735 return compare()( obj1, obj2 );
736 }
737
738 _iterator lower_bound( const key_type& key ) { return lower_bound<key_type>( key ); }
739 template <detail::key_comparable<key_type, key_compare> K>
740 _iterator lower_bound( const K& key ) {
741 return std::lower_bound( m_vct.begin(), m_vct.end(), key, compare() );
742 }
743
745 auto result = m_vct.begin();
746 std::advance( result, std::distance( begin(), p ) );
747 return result;
748 }
749
751 auto result = begin();
752 std::advance( result, std::distance( m_vct.begin(), p ) );
753 return result;
754 }
755
756 private:
758 _vector m_vct; // the underlying sorted vector of (key,mapped) pairs
759 };
760} // namespace GaudiUtils
761namespace std {
766 template <class KEY, class VALUE, class KEYCOMPARE, class ALLOCATOR>
771} // namespace std
772namespace Gaudi {
773 namespace Parsers {
783 GAUDI_API StatusCode parse( GaudiUtils::VectorMap<std::string, double>& result, std::string_view input );
794 GAUDI_API StatusCode parse( GaudiUtils::VectorMap<Gaudi::StringKey, double>& result, std::string_view input );
795 } // namespace Parsers
796} // 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:112
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition VectorMap.h:149
friend std::ostream & operator<<(std::ostream &str, const VectorMap &)
printout to ostream - not implemented
Definition VectorMap.h:687
size_type size() const
number of elements
Definition VectorMap.h:466
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition VectorMap.h:188
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition VectorMap.h:133
size_type count(const key_type &key) const
count number of elements with the certain key
Definition VectorMap.h:442
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition VectorMap.h:151
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:356
iterator lower_bound(const K &key) const
Definition VectorMap.h:449
size_type erase(const K &key)
Definition VectorMap.h:216
_iterator lower_bound(const key_type &key)
'lower-bound' - non-const version
Definition VectorMap.h:738
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition VectorMap.h:719
void swap(VectorMap &other)
swap function, which 'swaps' the content of two containers
Definition VectorMap.h:474
ALLOCATOR::value_type const & reference
the types to conform STL
Definition VectorMap.h:127
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition VectorMap.h:664
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition VectorMap.h:147
std::pair< iterator, bool > result_type
visible iterator pait
Definition VectorMap.h:155
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition VectorMap.h:121
const compare_type & compare() const
get the comparison criteria itself
Definition VectorMap.h:680
ALLOCATOR::size_type size_type
the types to conform STL
Definition VectorMap.h:131
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition VectorMap.h:734
void erase(iterator pos)
erase the element using the iterator
Definition VectorMap.h:197
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition VectorMap.h:708
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition VectorMap.h:186
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition VectorMap.h:252
bool update(const K &key, const mapped_type &mapped)
Definition VectorMap.h:511
const mapped_type & operator[](const K &key) const
Definition VectorMap.h:622
iterator upper_bound(const K &key) const
Definition VectorMap.h:454
size_type max_size() const
maximal allowed size
Definition VectorMap.h:468
void reserve(size_type num)
reserve the space in the container for at least 'num' elements
Definition VectorMap.h:472
KEYCOMPARE key_compare
comparison of keys
Definition VectorMap.h:119
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition VectorMap.h:125
result_type insert(iterator pos, key_type key, mapped_type mapped)
insert the (key,value) pair into the container With the right guess the method could be more efficien...
Definition VectorMap.h:372
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:550
KEY key_type
the actual type of key
Definition VectorMap.h:115
void insert(KEYS kf, KEYS kl, VALUES vf)
insert into the container the elements from 2 "parallel" sequences
Definition VectorMap.h:392
iterators equal_range(const key_type &key) const
Definition VectorMap.h:457
size_type count(const K &key) const
Definition VectorMap.h:444
iterator upper_bound(const key_type &key) const
Definition VectorMap.h:452
iterator iter(_iterator p)
the conversion from 'non-const' to 'const' iterator
Definition VectorMap.h:750
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:509
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition VectorMap.h:298
_vector::iterator _iterator
the regular iterator (no export)
Definition VectorMap.h:141
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:582
const key_compare & compare_key() const
get the comparison criteria for keys
Definition VectorMap.h:685
std::pair< iterator, iterator > iterators
visible iterator pait
Definition VectorMap.h:153
void insert(PAIRS first, PAIRS last)
insert the sequence of elements into the container
Definition VectorMap.h:381
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition VectorMap.h:181
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition VectorMap.h:190
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition VectorMap.h:192
size_type erase(const key_type &key)
erase the element using the key
Definition VectorMap.h:214
const mapped_type & at(const K &key) const
Definition VectorMap.h:644
_iterator iter(iterator p)
the conversion from 'const' to 'non-const' iterator
Definition VectorMap.h:744
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition VectorMap.h:137
void clear()
clear the container
Definition VectorMap.h:470
_vector::const_iterator iterator
visible const_iterator (exported)
Definition VectorMap.h:145
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition VectorMap.h:672
iterator find(const K &key) const
Definition VectorMap.h:421
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition VectorMap.h:227
iterator find(const key_type &key) const
find the element by key
Definition VectorMap.h:419
auto operator<=>(const VectorMap &other) const
comparison criteria for containers
Definition VectorMap.h:479
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition VectorMap.h:697
ALLOCATOR::value_type const & const_reference
the types to conform STL
Definition VectorMap.h:129
result_type insert(const value_type &value)
insert the (key,value) pair into the container
Definition VectorMap.h:339
bool empty() const
empty container ?
Definition VectorMap.h:464
_iterator lower_bound(const K &key)
Definition VectorMap.h:740
VectorMap & merge(const VectorMap &right)
merge two maps
Definition VectorMap.h:691
const mapped_type & operator()(const K &key) const
Definition VectorMap.h:584
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:620
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition VectorMap.h:477
iterator lower_bound(const key_type &key) const
Definition VectorMap.h:447
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:642
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition VectorMap.h:656
iterators equal_range(const K &key) const
Definition VectorMap.h:459
VALUE mapped_type
the actual type of value
Definition VectorMap.h:117
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:767
The actual structure used to compare the elements Only "key" is important for comparison.
Definition VectorMap.h:162
bool operator()(const K &k, const value_type &v) const
compare key and pair (key,mapped): use compare by keys
Definition VectorMap.h:171
bool operator()(const value_type &v, const K &k) const
compare pair (key,mapped) and the key: use compare by keys
Definition VectorMap.h:176
bool operator()(const value_type &v1, const value_type &v2) const
compare pairs (key,mapped): use compare by keys
Definition VectorMap.h:168