The Gaudi Framework  master (d98a2936)
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>
14 #include <GaudiKernel/StatusCode.h>
15 #include <GaudiKernel/StringKey.h>
16 #include <algorithm>
17 #include <functional>
18 #include <initializer_list>
19 #include <ostream>
20 #include <utility>
21 #include <vector>
22 
23 namespace 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  }
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  };
168  typedef _compare_type compare_type;
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 ) ); }
202  iterator pos = find( key );
203  if ( end() == pos ) { return 0; }
204  erase( pos );
205  return 1;
206  }
212  size_type erase( iterator first, iterator last ) {
213  m_vct.erase( iter( first ), iter( last ) );
214  return last - first;
215  }
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  }
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  }
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  }
357  result_type insert( iterator pos, const key_type& key, const mapped_type& mapped ) {
358  return insert( pos, value_type( key, mapped ) );
359  }
365  template <class PAIRS>
366  void insert( PAIRS first, PAIRS last ) {
367  for ( ; first != last; ++first ) { insert( *first ); }
368  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
700  return std::lower_bound( m_vct.begin(), m_vct.end(), key, compare() );
701  }
704  auto result = m_vct.begin();
705  std::advance( result, std::distance( begin(), p ) );
706  return result;
707  }
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
720 namespace std {
725  template <class KEY, class VALUE, class KEYCOMPARE, class ALLOCATOR>
728  left.swap( right );
729  }
730 } // namespace std
731 namespace Gaudi {
732  namespace Parsers {
742  GAUDI_API StatusCode parse( GaudiUtils::VectorMap<std::string, double>& result, std::string_view input );
754  } // namespace Parsers
755 } // namespace Gaudi
GaudiUtils::VectorMap::_vector
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:125
GaudiUtils::VectorMap::value_at
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:680
GaudiUtils::VectorMap::size_type
ALLOCATOR::size_type size_type
the types to conform STL
Definition: VectorMap.h:119
GaudiUtils::VectorMap::erase
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:212
GaudiUtils::VectorMap::result_type
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:143
GaudiUtils::VectorMap::compare_key
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:646
GaudiUtils::VectorMap::allocator_type
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:113
GaudiUtils::VectorMap::count
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:424
GaudiUtils::VectorMap::at
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
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
GaudiPartProp.tests.v1
v1
Definition: tests.py:39
GaudiUtils::VectorMap::merge
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:652
GaudiUtils::VectorMap::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:137
std::swap
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
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:21
GaudiUtils::VectorMap::difference_type
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:121
GaudiUtils::VectorMap::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:139
GaudiUtils::VectorMap::m_vct
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:717
GaudiUtils::VectorMap
Definition: VectorMap.h:100
GaudiUtils::VectorMap::equal_range
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:427
GaudiUtils::VectorMap::erase
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition: VectorMap.h:237
GaudiUtils::VectorMap::_compare_type::operator()
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:157
GaudiUtils::VectorMap::operator!=
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:448
StringKey.h
GaudiUtils::VectorMap::rbegin
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:177
GaudiUtils::VectorMap::update
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
GaudiUtils::VectorMap::max_size
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:434
StatusCode.h
GaudiUtils::VectorMap::begin
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:173
GaudiUtils::VectorMap::iter
_iterator iter(iterator p)
the conversion from 'const' to 'non-const' iterator
Definition: VectorMap.h:703
GaudiUtils::VectorMap::const_iterator
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:135
GaudiUtils::VectorMap::clear
void clear()
clear the container
Definition: VectorMap.h:436
GaudiUtils::VectorMap::merge
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:658
GaudiUtils::VectorMap::key_type
KEY key_type
the actual type of key
Definition: VectorMap.h:103
GaudiUtils::VectorMap::_compare_type::operator()
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
GaudiUtils::VectorMap::operator<=
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:450
GaudiUtils::VectorMap::compare
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:695
GaudiUtils::VectorMap::lower_bound
_iterator lower_bound(const key_type &key)
'lower-bound' - non-const version
Definition: VectorMap.h:699
GaudiUtils::VectorMap::_compare_type::operator()
bool operator()(const value_type &v1, const value_type &v2) const
compare pairs (key,mapped): use compare by keys
Definition: VectorMap.h:161
GaudiUtils::VectorMap::compare_type
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:168
GaudiUtils::VectorMap::insert
void insert(KEYS kf, KEYS kl, VALUES vf)
insert into the container the elements from 2 "parallel" sequences
Definition: VectorMap.h:377
GaudiUtils::VectorMap::mapped_type
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:105
GaudiUtils::VectorMap::lower_bound
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:425
StatusCode
Definition: StatusCode.h:64
GaudiUtils::VectorMap::key_at
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:669
GaudiUtils::VectorMap::operator<
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:445
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
GaudiUtils::VectorMap::value_type
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:109
GaudiUtils::VectorMap::end
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:175
GaudiUtils::VectorMap::upper_bound
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:426
GaudiUtils::VectorMap::iterators
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:141
GaudiUtils::VectorMap::_compare_type::_compare_type
_compare_type()
default constructor
Definition: VectorMap.h:155
GaudiUtils::VectorMap::insert
void insert(PAIRS first, PAIRS last)
insert the sequence of elements into the container
Definition: VectorMap.h:366
GaudiUtils::VectorMap::operator<<
friend std::ostream & operator<<(std::ostream &str, const VectorMap &)
printout to ostream - not implemented
Definition: VectorMap.h:648
GaudiUtils::VectorMap::size
size_type size() const
number of elements
Definition: VectorMap.h:432
GaudiUtils::VectorMap::insert
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
GaudiUtils::VectorMap::insert
result_type insert(const value_type &value)
insert the (key,value) pair into the container
Definition: VectorMap.h:324
GaudiUtils::VectorMap::operator>=
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:449
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
GaudiUtils::VectorMap::key_compare
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:107
GaudiUtils::VectorMap::iter
iterator iter(_iterator p)
the conversion from 'non-const' to 'const' iterator
Definition: VectorMap.h:709
GaudiUtils::VectorMap::empty
bool empty() const
empty container ?
Definition: VectorMap.h:430
GaudiUtils::VectorMap::operator()
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
GaudiUtils::VectorMap::_compare_type::_compare_type
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:153
GaudiUtils::VectorMap::reserve
void reserve(size_type num)
reserve the space in the container for at least 'num' elements
Definition: VectorMap.h:438
GaudiUtils::VectorMap::compare
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:641
GaudiUtils::VectorMap::operator>
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:447
GaudiUtils::VectorMap::_compare_type
Definition: VectorMap.h:150
GaudiUtils::VectorMap::_iterator
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:129
GaudiUtils::VectorMap::VectorMap
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition: VectorMap.h:633
Gaudi::Utils::MapBase
Definition: MapBase.h:45
GaudiUtils::VectorMap::swap
void swap(VectorMap &other)
swap function, which 'swaps' the content of two containers
Definition: VectorMap.h:440
GaudiUtils::VectorMap::insert
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition: VectorMap.h:283
GaudiUtils::VectorMap::operator==
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:443
GaudiUtils::VectorMap::_compare_type::operator()
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
Properties.v
v
Definition: Properties.py:122
GaudiUtils::VectorMap::insert
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
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
GaudiUtils::VectorMap::const_reference
ALLOCATOR::value_type const & const_reference
the types to conform STL
Definition: VectorMap.h:117
GaudiUtils::VectorMap::reference
ALLOCATOR::value_type const & reference
the types to conform STL
Definition: VectorMap.h:115
GaudiUtils
Definition: Allocator.h:62
GaudiUtils::VectorMap::VectorMap
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition: VectorMap.h:617
GaudiUtils::VectorMap::iterator
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:133
GaudiUtils::VectorMap::rend
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:179
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiUtils::VectorMap::erase
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:201
GaudiUtils::VectorMap::find
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:404
GaudiUtils::VectorMap::update
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
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
GaudiUtils::VectorMap::operator[]
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
GaudiUtils::VectorMap::VectorMap
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:625
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
GaudiUtils::VectorMap::erase
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:184