Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #include <initializer_list>
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
102  <
103  class KEY ,
104  class VALUE ,
105  class KEYCOMPARE=std::less<const KEY> ,
106  class ALLOCATOR=std::allocator<std::pair<KEY,VALUE> >
107  >
109  {
110  public:
111  // ========================================================================
113  typedef KEY key_type ;
115  typedef VALUE mapped_type ;
117  typedef KEYCOMPARE key_compare ;
120  // ========================================================================
121  public:
122  // ========================================================================
124  typedef ALLOCATOR allocator_type ;
126  typedef typename ALLOCATOR::const_reference reference ;
128  typedef typename ALLOCATOR::const_reference const_reference ;
130  typedef typename ALLOCATOR::size_type size_type ;
132  typedef typename ALLOCATOR::difference_type difference_type ;
133  // ========================================================================
134  public:
135  // ========================================================================
138  // ========================================================================
139  protected:
140  // ========================================================================
142  typedef typename _vector::iterator _iterator ;
143  // ========================================================================
144  public:
145  // ========================================================================
147  typedef typename _vector::const_iterator iterator ;
149  typedef typename _vector::const_iterator const_iterator ;
158  // ========================================================================
159  public:
160  // ========================================================================
165  struct _compare_type : public key_compare
166  {
167  public:
168  // ======================================================================
170  _compare_type ( const key_compare& cmp ) : key_compare ( cmp ) {}
172  _compare_type () : key_compare ( ) {}
174  bool operator () ( const key_type& k1 , const key_type& k2 ) const
175  { return this->key_compare::operator() ( k1 , k2 ) ; }
177  bool operator() ( const value_type& v1 , const value_type& v2 ) const
178  { return operator() ( v1.first, v2.first ); }
180  bool operator() ( const key_type& k , const value_type& v ) const
181  { return operator() ( k , v.first ) ; }
183  bool operator() ( const value_type& v , const key_type & k ) const
184  { return operator() ( v.first , k ) ; }
185  // ======================================================================
186  };
187  // ========================================================================
190  // ========================================================================
191  public:
192  // ========================================================================
193  // sequential access (only const-versions!)
194  // ========================================================================
196  iterator begin () const { return m_vct . begin () ; }
198  iterator end () const { return m_vct . end () ; }
200  reverse_iterator rbegin () const { return m_vct . rbegin () ; }
202  reverse_iterator rend () const { return m_vct . rend () ; }
203  // ========================================================================
204  // list operations : erase & insert
205  // ========================================================================
209  void erase ( iterator pos ) { m_vct.erase ( iter ( pos ) ) ; }
210  // ========================================================================
227  size_type erase ( const key_type& key )
228  {
229  iterator pos = find ( key ) ;
230  if ( end() == pos ) { return 0 ; }
231  erase ( pos ) ;
232  return 1 ;
233  }
234  // ========================================================================
240  size_type erase ( iterator first ,
241  iterator last )
242  {
243  m_vct.erase ( iter ( first ) , iter ( last ) ) ;
244  return last - first ;
245  }
246  // ========================================================================
267  template <class TYPE>
268  size_type erase ( TYPE first , TYPE last )
269  {
270  size_type res = 0 ;
271  for ( ; first != last ; ++first ) { res += erase ( *first ) ; }
272  return res ;
273  }
274  // ========================================================================
316  result_type insert
317  ( const key_type& key ,
318  const mapped_type& mapped )
319  { return insert ( value_type ( key , mapped ) ) ; }
320  // ========================================================================
361  result_type insert
362  ( const value_type& value )
363  {
364  bool found = true ;
365  _iterator result = lower_bound ( value.first ) ;
366  if ( end() == result || compare( value.first , result -> first ) )
367  { result = m_vct.insert ( result , value ) ; found = false ; }
368  return result_type ( iter ( result ) , !found ) ;
369  }
370  // ========================================================================
379  result_type insert
380  ( iterator pos ,
381  const value_type& value )
382  {
383  if ( pos != end() && compare ( *pos , value ) &&
384  ( pos == end() - 1 ||
385  ( !compare ( value , *( pos + 1 ) )
386  && compare ( *( pos + 1 ) , value ) ) ) )
387  { return result_type( m_vct.insert ( iter ( pos ) , value ) , true ) ; }
388  return insert ( value ) ;
389  }
390  // ========================================================================
400  result_type insert
401  ( iterator pos ,
402  const key_type& key ,
403  const mapped_type& mapped )
404  { return insert ( pos , value_type ( key , mapped ) ) ; }
405  // ========================================================================
411  template <class PAIRS>
412  void insert
413  ( PAIRS first ,
414  PAIRS last )
415  { for ( ; first != last ; ++first ) { insert ( *first ) ; } }
416  // ========================================================================
424  template <class KEYS, class VALUES> void insert
425  ( KEYS kf ,
426  KEYS kl ,
427  VALUES vf )
428  { for ( ; kf != kl ; ++kf, ++vf ) { insert ( *kf , *vf ) ; } }
429  // ========================================================================
430  // map operations: lookup, count, ...
431  // ========================================================================
455  iterator find ( const key_type& key ) const
456  {
457  iterator res = lower_bound ( key ) ;
458  if ( end() != res && compare ( key , res->first ) )
459  { res = end(); }
460  return res ;
461  }
462  // ========================================================================
478  size_type count ( const key_type& key ) const
479  { return end() == find ( key ) ? 0 : 1 ; }
480  // ========================================================================
481  iterator lower_bound ( const key_type& key ) const
482  { return std::lower_bound ( begin () , end () , key , compare () ) ; }
483  iterator upper_bound ( const key_type& key ) const
484  { return std::upper_bound ( begin () , end () , key , compare () ) ; }
485  iterators equal_range ( const key_type& key ) const
486  { return std::equal_range ( begin () , end () , key , compare () ) ; }
487  // ========================================================================
488  // general container operations :
489  // ========================================================================
491  bool empty () const { return m_vct . empty () ; }
493  size_type size () const { return m_vct . size () ; }
495  size_type max_size () const { return m_vct . max_size () ; }
497  void clear () { m_vct.clear () ; }
499  void reserve ( size_type num ) { m_vct.reserve ( num ) ; }
500  // ========================================================================
502  void swap ( VectorMap& other )
503  {
504  std::swap ( m_vct , other.m_vct ) ;
505  }
506  // ========================================================================
507  // The basic comparison operators for container
508  // ========================================================================
510  bool operator== ( const VectorMap& other ) const
511  { return m_vct == other.m_vct ; }
513  bool operator< ( const VectorMap& other ) const
514  { return m_vct < other.m_vct ; }
515  // ========================================================================
516  // The derived comparison operators for container
517  // ========================================================================
518  friend bool operator> ( const VectorMap& left ,
519  const VectorMap& right )
520  { return right < left ; }
521  friend bool operator!= ( const VectorMap& left ,
522  const VectorMap& right )
523  { return !( left == right ) ; }
524  friend bool operator>= ( const VectorMap& left ,
525  const VectorMap& right )
526  { return !( left < right ) ; }
527  friend bool operator<= ( const VectorMap& left ,
528  const VectorMap& right )
529  { return !( right < left ) ; }
530  // ========================================================================
560  bool update
561  ( const key_type& key ,
562  const mapped_type& mapped )
563  {
564  _iterator result = lower_bound ( key ) ;
565  if ( end() == result || compare ( key , result -> first ) )
566  {
567  result = m_vct.insert ( result , value_type(key,mapped) ) ;
568  return false ;
569  }
570  else { result->second = mapped ; }
571  //
572  return true ;
573  }
574  // ========================================================================
603  bool update ( const value_type& val )
604  { return update ( val.first , val.second ) ; }
605  // ========================================================================
637  const mapped_type& operator() ( const key_type& key ) const
638  {
639  static const mapped_type s_default = mapped_type() ;
640  iterator res = find ( key ) ;
641  if ( end() == res ) { return s_default ; }
642  return res->second ;
643  }
644  // ========================================================================
675  const mapped_type& operator[] ( const key_type& key ) const
676  { return (*this)( key ) ; }
677  // ========================================================================
695  const mapped_type& at ( const key_type& key ) const
696  {
697  iterator res = find ( key ) ;
698  if ( end() == res ) { this->throw_out_of_range_exception () ; }
699  return res->second ;
700  }
701  // ========================================================================
702  public:
703  // ========================================================================
704  // Constructors, destructors, etc.
705  // ========================================================================
710  VectorMap ( const allocator_type& alloc = allocator_type () )
711  : m_vct ( alloc )
712  {}
713  // ========================================================================
717  VectorMap ( const VectorMap& right )
718  : Gaudi::Utils::MapBase(right), m_vct ( right.m_vct )
719  {}
720  // ========================================================================
727  template <class INPUT>
728  VectorMap ( INPUT first ,
729  INPUT last ,
730  const allocator_type& alloc = allocator_type () )
731  : m_vct ( first , last , alloc )
732  { std::sort ( m_vct.begin(), m_vct.end(), compare() ) ; }
733  // ========================================================================
740  const allocator_type& alloc = allocator_type () )
741  : m_vct ( first , alloc )
742  { std::sort ( m_vct.begin(), m_vct.end(), compare() ) ; }
743  // ========================================================================
745  ~VectorMap() { clear() ; } // destructor (non-virtual!)
746  // ========================================================================
747  /* assignement operator
748  * @param rigth object to be assigned
749  * @return self
750  */
751  VectorMap& operator= ( const VectorMap& right )
752  {
753  if ( &right == this ) { return *this ; }
754  m_vct = right.m_vct ;
755  return *this ;
756  }
757  // ========================================================================
758  public:
759  // ========================================================================
760  // The specific public accessors
761  // ========================================================================
763  const compare_type& compare () const
764  {
765  static const compare_type s_cmp = compare_type() ;
766  return s_cmp ;
767  }
769  const key_compare& compare_key () const { return compare() ; }
771  friend std::ostream& operator<<
772  ( std::ostream& str , const VectorMap& /* obj */) { return str ; }
773  // ========================================================================
774  public:
775  // ========================================================================
777  inline VectorMap& merge ( const VectorMap& right )
778  {
779  for ( const auto& i : right ) { update ( i.first , i.second ) ; }
780  return *this ;
781  }
783  template <class K1,class K2, class K3,class K4>
784  inline VectorMap& merge ( const VectorMap<K1,K2,K3,K4>& right )
785  {
786  for ( const auto& i : right ) { update ( i.first , i.second ) ; }
787  return *this ;
788  }
789  // ========================================================================
790  public:
791  // ========================================================================
797  const key_type& key_at ( const size_t index ) const
798  {
799  if ( index >= size() )
800  { this->throw_out_of_range_exception () ; }
801  auto it = this->begin() ;
802  std::advance ( it , index ) ;
803  return it -> first ;
804  }
810  const mapped_type& value_at ( const size_t index ) const
811  {
812  if ( index >= size() )
813  { this->throw_out_of_range_exception () ; }
814  auto it = this->begin() ;
815  std::advance ( it , index ) ;
816  return it -> second ;
817  }
818  // ========================================================================
819  protected:
820  // ========================================================================
821  // Pure technical helper functions
822  // ========================================================================
828  template <class TYPE1, class TYPE2>
829  bool compare ( const TYPE1& obj1 ,
830  const TYPE2& obj2 ) const
831  {
832  return compare() ( obj1 , obj2 ) ;
833  }
834  // ========================================================================
836  _iterator lower_bound ( const key_type& key )
837  {
838  return std::lower_bound
839  ( m_vct.begin() , m_vct.end() , key , compare() ) ;
840  }
841  // ========================================================================
843  _iterator iter ( iterator p )
844  {
845  auto result = m_vct.begin() ;
846  std::advance ( result , std::distance ( begin() , p ) ) ;
847  return result ;
848  }
849  // ========================================================================
851  iterator iter ( _iterator p )
852  {
853  auto result = begin();
854  std::advance ( result , std::distance ( m_vct.begin() , p ) ) ;
855  return result ;
856  }
857  // ========================================================================
858  private:
859  // ========================================================================
861  _vector m_vct ; // the underlying sorted vector of (key,mapped) pairs
862  // ========================================================================
863  };
864  // ==========================================================================
865 } // end of namespace GaudiUtils
866 // ============================================================================
867 namespace std
868 {
869  // ==========================================================================
874  template
875  < class KEY ,
876  class VALUE ,
877  class KEYCOMPARE ,
878  class ALLOCATOR >
879  inline void swap
882  { left.swap( right ) ; }
883  // ===========================================================================
884 } // end of namespace std
885 // ============================================================================
886 // ============================================================================
887 namespace Gaudi
888 {
889  // ==========================================================================
890  namespace Parsers
891  {
892  // ========================================================================
904  const std::string& input ) ;
905  // ========================================================================
918  const std::string& input ) ;
919  // ========================================================================
920  } // end of namespace Gaudi::Parsers
921  // ==========================================================================
922 } // end of namespace Gaudi
923 
924 
925 // ============================================================================
926 // The END
927 // ============================================================================
928 #endif // GAUDIKERNEL_MAPS_H
929 // ============================================================================
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:675
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:108
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:521
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:510
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:695
~VectorMap()
destructor (non-virtual!)
Definition: VectorMap.h:745
iterator iter(_iterator p)
the conversion from &#39;non-const&#39; to &#39;const&#39; iterator
Definition: VectorMap.h:851
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:499
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:829
T advance(T...args)
T upper_bound(T...args)
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:142
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:797
STL namespace.
STL class.
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:147
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition: VectorMap.h:268
T end(T...args)
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:174
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:317
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:784
VectorMap(const VectorMap &right)
copy constructor
Definition: VectorMap.h:717
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:227
constexpr double second
STL class.
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:495
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:455
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:769
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:200
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:155
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:481
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:198
ALLOCATOR::const_reference reference
the types to conform STL
Definition: VectorMap.h:126
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:240
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:861
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:153
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:196
T erase(T...args)
_compare_type()
default constructor
Definition: VectorMap.h:172
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:524
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:151
T clear(T...args)
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:518
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:157
ALLOCATOR::const_reference const_reference
the types to conform STL
Definition: VectorMap.h:128
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:170
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:603
T insert(T...args)
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition: VectorMap.h:739
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:527
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:728
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:202
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:115
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:189
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:149
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:483
T begin(T...args)
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:513
VectorMap & operator=(const VectorMap &right)
Definition: VectorMap.h:751
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:478
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:763
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:124
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:149
_iterator iter(iterator p)
the conversion from &#39;const&#39; to &#39;non-const&#39; iterator
Definition: VectorMap.h:843
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:561
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:117
void clear()
clear the container
Definition: VectorMap.h:497
KEY key_type
the actual type of key
Definition: VectorMap.h:113
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:502
The actual structure used to compare the elements Only "key" is important for comparison.
Definition: VectorMap.h:165
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:132
bool empty() const
empty container ?
Definition: VectorMap.h:491
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:485
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:119
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:777
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:130
#define GAUDI_API
Definition: Kernel.h:107
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:209
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:710
_iterator lower_bound(const key_type &key)
&#39;lower-bound&#39; - non-const version
Definition: VectorMap.h:836
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:493
T reserve(T...args)
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:137
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:810