The Gaudi Framework  v29r0 (ff2e7097)
VectorMap.h
Go to the documentation of this file.
1 // ============================================================================
2 #ifndef GAUDIKERNEL_VECTORMAP_H
3 #define GAUDIKERNEL_VECTORMAP_H 1
4 // ============================================================================
5 // Include files
6 // ============================================================================
7 // STD & STL
8 // ============================================================================
9 #include <algorithm>
10 #include <functional>
11 #include <initializer_list>
12 #include <ostream>
13 #include <utility>
14 #include <vector>
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
18 #include "GaudiKernel/MapBase.h"
19 
20 // For parsers
21 #include "GaudiKernel/StatusCode.h"
22 #include "GaudiKernel/StringKey.h"
23 // ============================================================================
24 namespace GaudiUtils
25 {
26  // ==========================================================================
101  template <class KEY, class VALUE, class KEYCOMPARE = std::less<const KEY>,
102  class ALLOCATOR = std::allocator<std::pair<KEY, VALUE>>>
104  {
105  public:
106  // ========================================================================
108  typedef KEY key_type;
110  typedef VALUE mapped_type;
112  typedef KEYCOMPARE key_compare;
115  // ========================================================================
116  public:
117  // ========================================================================
119  typedef ALLOCATOR allocator_type;
121  typedef typename ALLOCATOR::const_reference reference;
123  typedef typename ALLOCATOR::const_reference const_reference;
125  typedef typename ALLOCATOR::size_type size_type;
127  typedef typename ALLOCATOR::difference_type difference_type;
128  // ========================================================================
129  public:
130  // ========================================================================
133  // ========================================================================
134  protected:
135  // ========================================================================
137  typedef typename _vector::iterator _iterator;
138  // ========================================================================
139  public:
140  // ========================================================================
142  typedef typename _vector::const_iterator iterator;
144  typedef typename _vector::const_iterator const_iterator;
153  // ========================================================================
154  public:
155  // ========================================================================
160  struct _compare_type : public key_compare {
161  public:
162  // ======================================================================
164  _compare_type( const key_compare& cmp ) : key_compare( cmp ) {}
166  _compare_type() : key_compare() {}
168  bool operator()( const key_type& k1, const key_type& k2 ) const
169  {
170  return this->key_compare::operator()( k1, k2 );
171  }
173  bool operator()( const value_type& v1, const value_type& v2 ) const { return operator()( v1.first, v2.first ); }
175  bool operator()( const key_type& k, const value_type& v ) const { return operator()( k, v.first ); }
177  bool operator()( const value_type& v, const key_type& k ) const { return operator()( v.first, k ); }
178  // ======================================================================
179  };
180  // ========================================================================
183  // ========================================================================
184  public:
185  // ========================================================================
186  // sequential access (only const-versions!)
187  // ========================================================================
189  iterator begin() const { return m_vct.begin(); }
191  iterator end() const { return m_vct.end(); }
193  reverse_iterator rbegin() const { return m_vct.rbegin(); }
195  reverse_iterator rend() const { return m_vct.rend(); }
196  // ========================================================================
197  // list operations : erase & insert
198  // ========================================================================
202  void erase( iterator pos ) { m_vct.erase( iter( pos ) ); }
203  // ========================================================================
220  size_type erase( const key_type& key )
221  {
222  iterator pos = find( key );
223  if ( end() == pos ) {
224  return 0;
225  }
226  erase( pos );
227  return 1;
228  }
229  // ========================================================================
235  size_type erase( iterator first, iterator last )
236  {
237  m_vct.erase( iter( first ), iter( last ) );
238  return last - first;
239  }
240  // ========================================================================
261  template <class TYPE>
262  size_type erase( TYPE first, TYPE last )
263  {
264  size_type res = 0;
265  for ( ; first != last; ++first ) {
266  res += erase( *first );
267  }
268  return res;
269  }
270  // ========================================================================
312  result_type insert( const key_type& key, const mapped_type& mapped ) { return insert( value_type( key, mapped ) ); }
313  // ========================================================================
354  result_type insert( const value_type& value )
355  {
356  bool found = true;
357  _iterator result = lower_bound( value.first );
358  if ( end() == result || compare( value.first, result->first ) ) {
359  result = m_vct.insert( result, value );
360  found = false;
361  }
362  return result_type( iter( result ), !found );
363  }
364  // ========================================================================
373  result_type insert( iterator pos, const value_type& value )
374  {
375  if ( pos != end() && compare( *pos, value ) &&
376  ( pos == end() - 1 || ( !compare( value, *( pos + 1 ) ) && compare( *( pos + 1 ), value ) ) ) ) {
377  return result_type( m_vct.insert( iter( pos ), value ), true );
378  }
379  return insert( value );
380  }
381  // ========================================================================
391  result_type insert( iterator pos, const key_type& key, const mapped_type& mapped )
392  {
393  return insert( pos, value_type( key, mapped ) );
394  }
395  // ========================================================================
401  template <class PAIRS>
402  void insert( PAIRS first, PAIRS last )
403  {
404  for ( ; first != last; ++first ) {
405  insert( *first );
406  }
407  }
408  // ========================================================================
416  template <class KEYS, class VALUES>
417  void insert( KEYS kf, KEYS kl, VALUES vf )
418  {
419  for ( ; kf != kl; ++kf, ++vf ) {
420  insert( *kf, *vf );
421  }
422  }
423  // ========================================================================
424  // map operations: lookup, count, ...
425  // ========================================================================
449  iterator find( const key_type& key ) const
450  {
451  iterator res = lower_bound( key );
452  if ( end() != res && compare( key, res->first ) ) {
453  res = end();
454  }
455  return res;
456  }
457  // ========================================================================
473  size_type count( const key_type& key ) const { return end() == find( key ) ? 0 : 1; }
474  // ========================================================================
475  iterator lower_bound( const key_type& key ) const { return std::lower_bound( begin(), end(), key, compare() ); }
476  iterator upper_bound( const key_type& key ) const { return std::upper_bound( begin(), end(), key, compare() ); }
477  iterators equal_range( const key_type& key ) const { return std::equal_range( begin(), end(), key, compare() ); }
478  // ========================================================================
479  // general container operations :
480  // ========================================================================
482  bool empty() const { return m_vct.empty(); }
484  size_type size() const { return m_vct.size(); }
486  size_type max_size() const { return m_vct.max_size(); }
488  void clear() { m_vct.clear(); }
490  void reserve( size_type num ) { m_vct.reserve( num ); }
491  // ========================================================================
493  void swap( VectorMap& other ) { std::swap( m_vct, other.m_vct ); }
494  // ========================================================================
495  // The basic comparison operators for container
496  // ========================================================================
498  bool operator==( const VectorMap& other ) const { return m_vct == other.m_vct; }
500  bool operator<( const VectorMap& other ) const { return m_vct < other.m_vct; }
501  // ========================================================================
502  // The derived comparison operators for container
503  // ========================================================================
504  friend bool operator>( const VectorMap& left, const VectorMap& right ) { return right < left; }
505  friend bool operator!=( const VectorMap& left, const VectorMap& right ) { return !( left == right ); }
506  friend bool operator>=( const VectorMap& left, const VectorMap& right ) { return !( left < right ); }
507  friend bool operator<=( const VectorMap& left, const VectorMap& right ) { return !( right < left ); }
508  // ========================================================================
538  bool update( const key_type& key, const mapped_type& mapped )
539  {
540  _iterator result = lower_bound( key );
541  if ( end() == result || compare( key, result->first ) ) {
542  result = m_vct.insert( result, value_type( key, mapped ) );
543  return false;
544  } else {
545  result->second = mapped;
546  }
547  //
548  return true;
549  }
550  // ========================================================================
579  bool update( const value_type& val ) { return update( val.first, val.second ); }
580  // ========================================================================
612  const mapped_type& operator()( const key_type& key ) const
613  {
614  static const mapped_type s_default = mapped_type();
615  iterator res = find( key );
616  if ( end() == res ) {
617  return s_default;
618  }
619  return res->second;
620  }
621  // ========================================================================
652  const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
653  // ========================================================================
671  const mapped_type& at( const key_type& key ) const
672  {
673  iterator res = find( key );
674  if ( end() == res ) {
676  }
677  return res->second;
678  }
679  // ========================================================================
680  public:
681  // ========================================================================
682  // Constructors, destructors, etc.
683  // ========================================================================
688  VectorMap( const allocator_type& alloc = allocator_type() ) : m_vct( alloc ) {}
689  // ========================================================================
693  VectorMap( const VectorMap& right ) : Gaudi::Utils::MapBase( right ), m_vct( right.m_vct ) {}
694  // ========================================================================
701  template <class INPUT>
702  VectorMap( INPUT first, INPUT last, const allocator_type& alloc = allocator_type() ) : m_vct( first, last, alloc )
703  {
704  std::sort( m_vct.begin(), m_vct.end(), compare() );
705  }
706  // ========================================================================
712  VectorMap( std::initializer_list<value_type> first, const allocator_type& alloc = allocator_type() )
713  : m_vct( first, alloc )
714  {
715  std::sort( m_vct.begin(), m_vct.end(), compare() );
716  }
717  // ========================================================================
719  ~VectorMap() { clear(); }
720  // ========================================================================
721  /* assignement operator
722  * @param rigth object to be assigned
723  * @return self
724  */
725  VectorMap& operator=( const VectorMap& right )
726  {
727  if ( &right == this ) {
728  return *this;
729  }
730  m_vct = right.m_vct;
731  return *this;
732  }
733  // ========================================================================
734  public:
735  // ========================================================================
736  // The specific public accessors
737  // ========================================================================
739  const compare_type& compare() const
740  {
741  static const compare_type s_cmp = compare_type();
742  return s_cmp;
743  }
745  const key_compare& compare_key() const { return compare(); }
747  friend std::ostream& operator<<( std::ostream& str, const VectorMap& /* obj */ ) { return str; }
748  // ========================================================================
749  public:
750  // ========================================================================
752  inline VectorMap& merge( const VectorMap& right )
753  {
754  for ( const auto& i : right ) {
755  update( i.first, i.second );
756  }
757  return *this;
758  }
760  template <class K1, class K2, class K3, class K4>
761  inline VectorMap& merge( const VectorMap<K1, K2, K3, K4>& right )
762  {
763  for ( const auto& i : right ) {
764  update( i.first, i.second );
765  }
766  return *this;
767  }
768  // ========================================================================
769  public:
770  // ========================================================================
776  const key_type& key_at( const size_t index ) const
777  {
778  if ( index >= size() ) {
780  }
781  auto it = this->begin();
782  std::advance( it, index );
783  return it->first;
784  }
790  const mapped_type& value_at( const size_t index ) const
791  {
792  if ( index >= size() ) {
794  }
795  auto it = this->begin();
796  std::advance( it, index );
797  return it->second;
798  }
799  // ========================================================================
800  protected:
801  // ========================================================================
802  // Pure technical helper functions
803  // ========================================================================
809  template <class TYPE1, class TYPE2>
810  bool compare( const TYPE1& obj1, const TYPE2& obj2 ) const
811  {
812  return compare()( obj1, obj2 );
813  }
814  // ========================================================================
816  _iterator lower_bound( const key_type& key )
817  {
818  return std::lower_bound( m_vct.begin(), m_vct.end(), key, compare() );
819  }
820  // ========================================================================
822  _iterator iter( iterator p )
823  {
824  auto result = m_vct.begin();
825  std::advance( result, std::distance( begin(), p ) );
826  return result;
827  }
828  // ========================================================================
830  iterator iter( _iterator p )
831  {
832  auto result = begin();
833  std::advance( result, std::distance( m_vct.begin(), p ) );
834  return result;
835  }
836  // ========================================================================
837  private:
838  // ========================================================================
840  _vector m_vct; // the underlying sorted vector of (key,mapped) pairs
841  // ========================================================================
842  };
843  // ==========================================================================
844 } // end of namespace GaudiUtils
845 // ============================================================================
846 namespace std
847 {
848  // ==========================================================================
853  template <class KEY, class VALUE, class KEYCOMPARE, class ALLOCATOR>
856  {
857  left.swap( right );
858  }
859  // ===========================================================================
860 } // end of namespace std
861 // ============================================================================
862 // ============================================================================
863 namespace Gaudi
864 {
865  // ==========================================================================
866  namespace Parsers
867  {
868  // ========================================================================
879  // ========================================================================
891  // ========================================================================
892  } // end of namespace Gaudi::Parsers
893  // ==========================================================================
894 } // end of namespace Gaudi
895 
896 // ============================================================================
897 // The END
898 // ============================================================================
899 #endif // GAUDIKERNEL_MAPS_H
900 // ============================================================================
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:152
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:652
bool operator()(const value_type &v1, const value_type &v2) const
compare pairs (key,mapped): use compare by keys
Definition: VectorMap.h:173
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:103
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:177
T empty(T...args)
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:505
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:498
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:132
T distance(T...args)
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:671
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:612
~VectorMap()
destructor (non-virtual!)
Definition: VectorMap.h:719
iterator iter(_iterator p)
the conversion from &#39;non-const&#39; to &#39;const&#39; iterator
Definition: VectorMap.h:830
T rend(T...args)
T swap(T...args)
void reserve(size_type num)
reserve the space in the container for at least &#39;num&#39; elements
Definition: VectorMap.h:490
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:810
T advance(T...args)
T upper_bound(T...args)
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:137
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:776
STL namespace.
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:142
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition: VectorMap.h:262
T end(T...args)
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:168
T lower_bound(T...args)
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition: VectorMap.h:312
friend std::ostream & operator<<(std::ostream &str, const VectorMap &)
printout to ostream - not implemented
Definition: VectorMap.h:747
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:761
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:114
VectorMap(const VectorMap &right)
copy constructor
Definition: VectorMap.h:693
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:220
STL class.
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:486
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:449
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:745
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:193
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:475
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:191
ALLOCATOR::const_reference reference
the types to conform STL
Definition: VectorMap.h:121
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:235
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:840
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:148
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:189
void insert(PAIRS first, PAIRS last)
insert the sequence of elements into the container
Definition: VectorMap.h:402
T erase(T...args)
_compare_type()
default constructor
Definition: VectorMap.h:166
void insert(KEYS kf, KEYS kl, VALUES vf)
insert into the container the elements from 2 "parallel" sequences
Definition: VectorMap.h:417
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:506
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:146
T clear(T...args)
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:504
ALLOCATOR::const_reference const_reference
the types to conform STL
Definition: VectorMap.h:123
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:164
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:579
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:391
T insert(T...args)
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition: VectorMap.h:712
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:507
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:702
T size(T...args)
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:195
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:150
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:110
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:182
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:144
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:476
T begin(T...args)
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:500
VectorMap & operator=(const VectorMap &right)
Definition: VectorMap.h:725
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:473
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:739
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:119
T max_size(T...args)
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:140
_iterator iter(iterator p)
the conversion from &#39;const&#39; to &#39;non-const&#39; iterator
Definition: VectorMap.h:822
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:538
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:112
void clear()
clear the container
Definition: VectorMap.h:488
KEY key_type
the actual type of key
Definition: VectorMap.h:108
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:28
T sort(T...args)
void swap(VectorMap &other)
swap function, which &#39;swaps&#39; the content of two containers
Definition: VectorMap.h:493
The actual structure used to compare the elements Only "key" is important for comparison.
Definition: VectorMap.h:160
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:373
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:127
bool empty() const
empty container ?
Definition: VectorMap.h:482
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:477
result_type insert(const value_type &value)
insert the (key,value) pair into the container
Definition: VectorMap.h:354
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:752
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:44
ALLOCATOR::size_type size_type
the types to conform STL
Definition: VectorMap.h:125
#define GAUDI_API
Definition: Kernel.h:110
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:202
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1
T equal_range(T...args)
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition: VectorMap.h:688
_iterator lower_bound(const key_type &key)
&#39;lower-bound&#39; - non-const version
Definition: VectorMap.h:816
GAUDI_API StatusCode parse(GaudiUtils::VectorMap< Gaudi::StringKey, double > &result, const std::string &input)
parse the vector of keys from the string
size_type size() const
number of elements
Definition: VectorMap.h:484
bool operator()(const key_type &k, const value_type &v) const
compare key and pair (key,mapped): use compare by keys
Definition: VectorMap.h:175
T reserve(T...args)
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:790
T rbegin(T...args)