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 <utility>
10 #include <functional>
11 #include <vector>
12 #include <algorithm>
13 #include <ostream>
14 // ============================================================================
15 // GaudiKernel
16 // ============================================================================
17 #include "GaudiKernel/MapBase.h"
18 
19 // For parsers
20 #include "GaudiKernel/StatusCode.h"
21 #include "GaudiKernel/StringKey.h"
22 // ============================================================================
23 namespace GaudiUtils
24 {
25  // ==========================================================================
100  template
101  <
102  class KEY ,
103  class VALUE ,
104  class KEYCOMPARE=std::less<const KEY> ,
105  class ALLOCATOR=std::allocator<std::pair<KEY,VALUE> >
106  >
108  {
109  public:
110  // ========================================================================
112  typedef KEY key_type ;
114  typedef VALUE mapped_type ;
116  typedef KEYCOMPARE key_compare ;
118  typedef std::pair<key_type,mapped_type> value_type ;
119  // ========================================================================
120  public:
121  // ========================================================================
123  typedef ALLOCATOR allocator_type ;
125  typedef typename ALLOCATOR::const_reference reference ;
127  typedef typename ALLOCATOR::const_reference const_reference ;
129  typedef typename ALLOCATOR::size_type size_type ;
131  typedef typename ALLOCATOR::difference_type difference_type ;
132  // ========================================================================
133  public:
134  // ========================================================================
136  typedef std::vector<value_type,allocator_type> _vector ;
137  // ========================================================================
138  protected:
139  // ========================================================================
141  typedef typename _vector::iterator _iterator ;
142  // ========================================================================
143  public:
144  // ========================================================================
146  typedef typename _vector::const_iterator iterator ;
148  typedef typename _vector::const_iterator const_iterator ;
150  typedef std::reverse_iterator<iterator> reverse_iterator ;
152  typedef std::reverse_iterator<const_iterator> const_reverse_iterator ;
154  typedef std::pair<iterator,iterator> iterators ;
156  typedef std::pair<iterator,bool> result_type ;
157  // ========================================================================
158  public:
159  // ========================================================================
164  struct _compare_type : public key_compare
165  {
166  public:
167  // ======================================================================
169  _compare_type ( const key_compare& cmp ) : key_compare ( cmp ) {}
171  _compare_type () : key_compare ( ) {}
173  bool operator () ( const key_type& k1 , const key_type& k2 ) const
174  { return this->key_compare::operator() ( k1 , k2 ) ; }
176  bool operator() ( const value_type& v1 , const value_type& v2 ) const
177  { return operator() ( v1.first, v2.first ); }
179  bool operator() ( const key_type& k , const value_type& v ) const
180  { return operator() ( k , v.first ) ; }
182  bool operator() ( const value_type& v , const key_type & k ) const
183  { return operator() ( v.first , k ) ; }
184  // ======================================================================
185  };
186  // ========================================================================
188  typedef _compare_type compare_type ;
189  // ========================================================================
190  public:
191  // ========================================================================
192  // sequential access (only const-versions!)
193  // ========================================================================
195  iterator begin () const { return m_vct . begin () ; }
197  iterator end () const { return m_vct . end () ; }
199  reverse_iterator rbegin () const { return m_vct . rbegin () ; }
201  reverse_iterator rend () const { return m_vct . rend () ; }
202  // ========================================================================
203  // list operations : erase & insert
204  // ========================================================================
208  void erase ( iterator pos ) { m_vct.erase ( iter ( pos ) ) ; }
209  // ========================================================================
226  size_type erase ( const key_type& key )
227  {
228  iterator pos = find ( key ) ;
229  if ( end() == pos ) { return 0 ; }
230  erase ( pos ) ;
231  return 1 ;
232  }
233  // ========================================================================
239  size_type erase ( iterator first ,
240  iterator last )
241  {
242  m_vct.erase ( iter ( first ) , iter ( last ) ) ;
243  return last - first ;
244  }
245  // ========================================================================
266  template <class TYPE>
267  size_type erase ( TYPE first , TYPE last )
268  {
269  size_type res = 0 ;
270  for ( ; first != last ; ++first ) { res += erase ( *first ) ; }
271  return res ;
272  }
273  // ========================================================================
315  result_type insert
316  ( const key_type& key ,
317  const mapped_type& mapped )
318  { return insert ( value_type ( key , mapped ) ) ; }
319  // ========================================================================
360  result_type insert
361  ( const value_type& value )
362  {
363  bool found = true ;
364  _iterator result = lower_bound ( value.first ) ;
365  if ( end() == result || compare( value.first , result -> first ) )
366  { result = m_vct.insert ( result , value ) ; found = false ; }
367  return result_type ( iter ( result ) , !found ) ;
368  }
369  // ========================================================================
378  result_type insert
379  ( iterator pos ,
380  const value_type& value )
381  {
382  if ( pos != end() && compare ( *pos , value ) &&
383  ( pos == end() - 1 ||
384  ( !compare ( value , *( pos + 1 ) )
385  && compare ( *( pos + 1 ) , value ) ) ) )
386  { return result_type( m_vct.insert ( iter ( pos ) , value ) , true ) ; }
387  return insert ( value ) ;
388  }
389  // ========================================================================
399  result_type insert
400  ( iterator pos ,
401  const key_type& key ,
402  const mapped_type& mapped )
403  { return insert ( pos , value_type ( key , mapped ) ) ; }
404  // ========================================================================
410  template <class PAIRS>
411  void insert
412  ( PAIRS first ,
413  PAIRS last )
414  { for ( ; first != last ; ++first ) { insert ( *first ) ; } }
415  // ========================================================================
423  template <class KEYS, class VALUES> void insert
424  ( KEYS kf ,
425  KEYS kl ,
426  VALUES vf )
427  { for ( ; kf != kl ; ++kf, ++vf ) { insert ( *kf , *vf ) ; } }
428  // ========================================================================
429  // map operations: lookup, count, ...
430  // ========================================================================
454  iterator find ( const key_type& key ) const
455  {
456  iterator res = lower_bound ( key ) ;
457  if ( end() != res && compare ( key , res->first ) )
458  { res = end(); }
459  return res ;
460  }
461  // ========================================================================
477  size_type count ( const key_type& key ) const
478  { return end() == find ( key ) ? 0 : 1 ; }
479  // ========================================================================
480  iterator lower_bound ( const key_type& key ) const
481  { return std::lower_bound ( begin () , end () , key , compare () ) ; }
482  iterator upper_bound ( const key_type& key ) const
483  { return std::upper_bound ( begin () , end () , key , compare () ) ; }
484  iterators equal_range ( const key_type& key ) const
485  { return std::equal_range ( begin () , end () , key , compare () ) ; }
486  // ========================================================================
487  // general container operations :
488  // ========================================================================
490  bool empty () const { return m_vct . empty () ; }
492  size_type size () const { return m_vct . size () ; }
494  size_type max_size () const { return m_vct . max_size () ; }
496  void clear () { m_vct.clear () ; }
498  void reserve ( size_type num ) { m_vct.reserve ( num ) ; }
499  // ========================================================================
501  void swap ( VectorMap& other )
502  {
503  std::swap ( m_vct , other.m_vct ) ;
504  }
505  // ========================================================================
506  // The basic comparison operators for container
507  // ========================================================================
509  bool operator== ( const VectorMap& other ) const
510  { return m_vct == other.m_vct ; }
512  bool operator< ( const VectorMap& other ) const
513  { return m_vct < other.m_vct ; }
514  // ========================================================================
515  // The derived comparison operators for container
516  // ========================================================================
517  friend bool operator> ( const VectorMap& left ,
518  const VectorMap& right )
519  { return right < left ; }
520  friend bool operator!= ( const VectorMap& left ,
521  const VectorMap& right )
522  { return !( left == right ) ; }
523  friend bool operator>= ( const VectorMap& left ,
524  const VectorMap& right )
525  { return !( left < right ) ; }
526  friend bool operator<= ( const VectorMap& left ,
527  const VectorMap& right )
528  { return !( right < left ) ; }
529  // ========================================================================
559  bool update
560  ( const key_type& key ,
561  const mapped_type& mapped )
562  {
563  _iterator result = lower_bound ( key ) ;
564  if ( end() == result || compare ( key , result -> first ) )
565  {
566  result = m_vct.insert ( result , value_type(key,mapped) ) ;
567  return false ;
568  }
569  else { result->second = mapped ; }
570  //
571  return true ;
572  }
573  // ========================================================================
602  bool update ( const value_type& val )
603  { return update ( val.first , val.second ) ; }
604  // ========================================================================
636  const mapped_type& operator() ( const key_type& key ) const
637  {
638  static const mapped_type s_default = mapped_type() ;
639  iterator res = find ( key ) ;
640  if ( end() == res ) { return s_default ; }
641  return res->second ;
642  }
643  // ========================================================================
674  const mapped_type& operator[] ( const key_type& key ) const
675  { return (*this)( key ) ; }
676  // ========================================================================
694  const mapped_type& at ( const key_type& key ) const
695  {
696  iterator res = find ( key ) ;
697  if ( end() == res ) { this->throw_out_of_range_exception () ; }
698  return res->second ;
699  }
700  // ========================================================================
701  public:
702  // ========================================================================
703  // Constructors, destructors, etc.
704  // ========================================================================
709  VectorMap ( const allocator_type& alloc = allocator_type () )
710  : m_vct ( alloc )
711  {}
712  // ========================================================================
716  VectorMap ( const VectorMap& right )
717  : Gaudi::Utils::MapBase(right), m_vct ( right.m_vct )
718  {}
719  // ========================================================================
726  template <class INPUT>
727  VectorMap ( INPUT first ,
728  INPUT last ,
729  const allocator_type& alloc = allocator_type () )
730  : m_vct ( first , last , alloc )
731  { std::sort ( m_vct.begin(), m_vct.end(), compare() ) ; }
732  // ========================================================================
734  ~VectorMap() { clear() ; } // destructor (non-virtual!)
735  // ========================================================================
736  /* assignement operator
737  * @param rigth object to be assigned
738  * @return self
739  */
740  VectorMap& operator= ( const VectorMap& right )
741  {
742  if ( &right == this ) { return *this ; }
743  m_vct = right.m_vct ;
744  return *this ;
745  }
746  // ========================================================================
747  public:
748  // ========================================================================
749  // The specific public accessors
750  // ========================================================================
752  const compare_type& compare () const
753  {
754  static const compare_type s_cmp = compare_type() ;
755  return s_cmp ;
756  }
758  const key_compare& compare_key () const { return compare() ; }
760  friend std::ostream& operator<<
761  ( std::ostream& str , const VectorMap& /* obj */) { return str ; }
762  // ========================================================================
763  public:
764  // ========================================================================
766  inline VectorMap& merge ( const VectorMap& right )
767  {
768  for ( const_iterator it = right.begin() ; right.end() != it ; ++it )
769  { update ( it->first , it->second ) ; }
770  //
771  return *this ;
772  }
774  template <class K1,class K2, class K3,class K4>
775  inline VectorMap& merge ( const VectorMap<K1,K2,K3,K4>& right )
776  {
777  for ( typename VectorMap<K1,K2,K3,K4>::const_iterator it =
778  right.begin() ; right.end() != it ; ++it )
779  { update ( it->first , it->second ) ; }
780  //
781  return *this ;
782  }
783  // ========================================================================
784  public:
785  // ========================================================================
791  const key_type& key_at ( const size_t index ) const
792  {
793  if ( index >= size() )
794  { this->throw_out_of_range_exception () ; }
795  const_iterator it = this->begin() ;
796  std::advance ( it , index ) ;
797  return it -> first ;
798  }
804  const mapped_type& value_at ( const size_t index ) const
805  {
806  if ( index >= size() )
807  { this->throw_out_of_range_exception () ; }
808  const_iterator it = this->begin() ;
809  std::advance ( it , index ) ;
810  return it -> second ;
811  }
812  // ========================================================================
813  protected:
814  // ========================================================================
815  // Pure technical helper functions
816  // ========================================================================
822  template <class TYPE1, class TYPE2>
823  bool compare ( const TYPE1& obj1 ,
824  const TYPE2& obj2 ) const
825  {
826  return compare() ( obj1 , obj2 ) ;
827  }
828  // ========================================================================
830  _iterator lower_bound ( const key_type& key )
831  {
832  return std::lower_bound
833  ( m_vct.begin() , m_vct.end() , key , compare() ) ;
834  }
835  // ========================================================================
837  _iterator iter ( iterator p )
838  {
839  _iterator result = m_vct.begin() ;
840  std::advance ( result , std::distance ( begin() , p ) ) ;
841  return result ;
842  }
843  // ========================================================================
845  iterator iter ( _iterator p )
846  {
847  iterator result ( begin() ) ;
848  std::advance ( result , std::distance ( m_vct.begin() , p ) ) ;
849  return result ;
850  }
851  // ========================================================================
852  private:
853  // ========================================================================
855  _vector m_vct ; // the underlying sorted vector of (key,mapped) pairs
856  // ========================================================================
857  };
858  // ==========================================================================
859 } // end of namespace GaudiUtils
860 // ============================================================================
861 namespace std
862 {
863  // ==========================================================================
868  template
869  < class KEY ,
870  class VALUE ,
871  class KEYCOMPARE ,
872  class ALLOCATOR >
873  inline void swap
876  { left.swap( right ) ; }
877  // ===========================================================================
878 } // end of namespace std
879 // ============================================================================
880 // ============================================================================
881 namespace Gaudi
882 {
883  // ==========================================================================
884  namespace Parsers
885  {
886  // ========================================================================
898  const std::string& input ) ;
899  // ========================================================================
912  const std::string& input ) ;
913  // ========================================================================
914  } // end of namespace Gaudi::Parsers
915  // ==========================================================================
916 } // end of namespace Gaudi
917 
918 
919 // ============================================================================
920 // The END
921 // ============================================================================
922 #endif // GAUDIKERNEL_MAPS_H
923 // ============================================================================
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:674
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:107
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:520
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:509
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:694
#define GAUDI_API
Definition: Kernel.h:107
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:636
~VectorMap()
destructor (non-virtual!)
Definition: VectorMap.h:734
iterator iter(_iterator p)
the conversion from 'non-const' to 'const' iterator
Definition: VectorMap.h:845
void reserve(size_type num)
reserve the space in the container for at least 'num' elements
Definition: VectorMap.h:498
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:823
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:141
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:791
STL namespace.
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:146
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition: VectorMap.h:267
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:173
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition: VectorMap.h:316
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:775
VectorMap(const VectorMap &right)
copy constructor
Definition: VectorMap.h:716
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:226
constexpr double second
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:494
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:454
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:758
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:199
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:154
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:480
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:197
ALLOCATOR::const_reference reference
the types to conform STL
Definition: VectorMap.h:125
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:239
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:855
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:152
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:195
_compare_type()
default constructor
Definition: VectorMap.h:171
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:523
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:150
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:517
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:156
ALLOCATOR::const_reference const_reference
the types to conform STL
Definition: VectorMap.h:127
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:169
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:602
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:526
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:727
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:201
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:114
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:188
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:148
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:482
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:512
VectorMap & operator=(const VectorMap &right)
Definition: VectorMap.h:740
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:477
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:752
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:123
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:139
_iterator iter(iterator p)
the conversion from 'const' to 'non-const' iterator
Definition: VectorMap.h:837
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:560
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:116
void clear()
clear the container
Definition: VectorMap.h:496
KEY key_type
the actual type of key
Definition: VectorMap.h:112
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:28
void swap(VectorMap &other)
swap function, which 'swaps' the content of two containers
Definition: VectorMap.h:501
The actual structure used to compare the elements Only "key" is important for comparison.
Definition: VectorMap.h:164
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:131
bool empty() const
empty container ?
Definition: VectorMap.h:490
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:484
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:118
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:766
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:129
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:208
Helper functions to set/get the application return code.
Definition: __init__.py:1
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition: VectorMap.h:709
_iterator lower_bound(const key_type &key)
'lower-bound' - non-const version
Definition: VectorMap.h:830
size_type size() const
number of elements
Definition: VectorMap.h:492
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:136
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:804