All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VectorMap.h
Go to the documentation of this file.
1 // $Id: VectorMap.h,v 1.11 2007/05/24 14:39:11 hmd Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.11 $
4 // ============================================================================
5 #ifndef GAUDIKERNEL_VECTORMAP_H
6 #define GAUDIKERNEL_VECTORMAP_H 1
7 // ============================================================================
8 // Include files
9 // ============================================================================
10 // STD & STL
11 // ============================================================================
12 #include <utility>
13 #include <functional>
14 #include <vector>
15 #include <algorithm>
16 #include <ostream>
17 // ============================================================================
18 // GaudiKernel
19 // ============================================================================
20 #include "GaudiKernel/MapBase.h"
21 
22 // For parsers
23 #include "GaudiKernel/StatusCode.h"
24 #include "GaudiKernel/StringKey.h"
25 // ============================================================================
26 namespace GaudiUtils
27 {
28  // ==========================================================================
103  template
104  <
105  class KEY ,
106  class VALUE ,
107  class KEYCOMPARE=std::less<const KEY> ,
108  class ALLOCATOR=std::allocator<std::pair<KEY,VALUE> >
109  >
111  {
112  public:
113  // ========================================================================
115  typedef KEY key_type ;
117  typedef VALUE mapped_type ;
119  typedef KEYCOMPARE key_compare ;
121  typedef std::pair<key_type,mapped_type> value_type ;
122  // ========================================================================
123  public:
124  // ========================================================================
126  typedef ALLOCATOR allocator_type ;
128  typedef typename ALLOCATOR::const_reference reference ;
130  typedef typename ALLOCATOR::const_reference const_reference ;
132  typedef typename ALLOCATOR::size_type size_type ;
134  typedef typename ALLOCATOR::difference_type difference_type ;
135  // ========================================================================
136  public:
137  // ========================================================================
139  typedef std::vector<value_type,allocator_type> _vector ;
140  // ========================================================================
141  protected:
142  // ========================================================================
144  typedef typename _vector::iterator _iterator ;
145  // ========================================================================
146  public:
147  // ========================================================================
149  typedef typename _vector::const_iterator iterator ;
151  typedef typename _vector::const_iterator const_iterator ;
153  typedef std::reverse_iterator<iterator> reverse_iterator ;
155  typedef std::reverse_iterator<const_iterator> const_reverse_iterator ;
157  typedef std::pair<iterator,iterator> iterators ;
159  typedef std::pair<iterator,bool> result_type ;
160  // ========================================================================
161  public:
162  // ========================================================================
167  struct _compare_type : public key_compare
168  {
169  public:
170  // ======================================================================
172  _compare_type ( const key_compare& cmp ) : key_compare ( cmp ) {}
176  bool operator () ( const key_type& k1 , const key_type& k2 ) const
177  { return this->key_compare::operator() ( k1 , k2 ) ; }
179  bool operator() ( const value_type& v1 , const value_type& v2 ) const
180  { return operator() ( v1.first, v2.first ); }
182  bool operator() ( const key_type& k , const value_type& v ) const
183  { return operator() ( k , v.first ) ; }
185  bool operator() ( const value_type& v , const key_type & k ) const
186  { return operator() ( v.first , k ) ; }
187  // ======================================================================
188  };
189  // ========================================================================
191  typedef _compare_type compare_type ;
192  // ========================================================================
193  public:
194  // ========================================================================
195  // sequential access (only const-versions!)
196  // ========================================================================
198  iterator begin () const { return m_vct . begin () ; }
200  iterator end () const { return m_vct . end () ; }
202  reverse_iterator rbegin () const { return m_vct . rbegin () ; }
204  reverse_iterator rend () const { return m_vct . rend () ; }
205  // ========================================================================
206  // list operations : erase & insert
207  // ========================================================================
211  void erase ( iterator pos ) { m_vct.erase ( iter ( pos ) ) ; }
212  // ========================================================================
229  size_type erase ( const key_type& key )
230  {
231  iterator pos = find ( key ) ;
232  if ( end() == pos ) { return 0 ; }
233  erase ( pos ) ;
234  return 1 ;
235  }
236  // ========================================================================
243  iterator last )
244  {
245  m_vct.erase ( iter ( first ) , iter ( last ) ) ;
246  return last - first ;
247  }
248  // ========================================================================
269  template <class TYPE>
270  size_type erase ( TYPE first , TYPE last )
271  {
272  size_type res = 0 ;
273  for ( ; first != last ; ++first ) { res += erase ( *first ) ; }
274  return res ;
275  }
276  // ========================================================================
319  ( const key_type& key ,
320  const mapped_type& mapped )
321  { return insert ( value_type ( key , mapped ) ) ; }
322  // ========================================================================
364  ( const value_type& value )
365  {
366  bool found = true ;
367  _iterator result = lower_bound ( value.first ) ;
368  if ( end() == result || compare( value.first , result -> first ) )
369  { result = m_vct.insert ( result , value ) ; found = false ; }
370  return result_type ( iter ( result ) , !found ) ;
371  }
372  // ========================================================================
382  ( iterator pos ,
383  const value_type& value )
384  {
385  if ( pos != end() && compare ( *pos , value ) &&
386  ( pos == end() - 1 ||
387  ( !compare ( value , *( pos + 1 ) )
388  && compare ( *( pos + 1 ) , value ) ) ) )
389  { return result_type( m_vct.insert ( iter ( pos ) , value ) , true ) ; }
390  return insert ( value ) ;
391  }
392  // ========================================================================
403  ( iterator pos ,
404  const key_type& key ,
405  const mapped_type& mapped )
406  { return insert ( pos , value_type ( key , mapped ) ) ; }
407  // ========================================================================
413  template <class PAIRS>
414  void insert
415  ( PAIRS first ,
416  PAIRS last )
417  { for ( ; first != last ; ++first ) { insert ( *first ) ; } }
418  // ========================================================================
426  template <class KEYS, class VALUES> void insert
427  ( KEYS kf ,
428  KEYS kl ,
429  VALUES vf )
430  { for ( ; kf != kl ; ++kf, ++vf ) { insert ( *kf , *vf ) ; } }
431  // ========================================================================
432  // map operations: lookup, count, ...
433  // ========================================================================
457  iterator find ( const key_type& key ) const
458  {
459  iterator res = lower_bound ( key ) ;
460  if ( end() != res && compare ( key , res->first ) )
461  { res = end(); }
462  return res ;
463  }
464  // ========================================================================
480  size_type count ( const key_type& key ) const
481  { return end() == find ( key ) ? 0 : 1 ; }
482  // ========================================================================
483  iterator lower_bound ( const key_type& key ) const
484  { return std::lower_bound ( begin () , end () , key , compare () ) ; }
485  iterator upper_bound ( const key_type& key ) const
486  { return std::upper_bound ( begin () , end () , key , compare () ) ; }
487  iterators equal_range ( const key_type& key ) const
488  { return std::equal_range ( begin () , end () , key , compare () ) ; }
489  // ========================================================================
490  // general container operations :
491  // ========================================================================
493  bool empty () const { return m_vct . empty () ; }
495  size_type size () const { return m_vct . size () ; }
497  size_type max_size () const { return m_vct . max_size () ; }
499  void clear () { m_vct.clear () ; }
501  void reserve ( size_type num ) { m_vct.reserve ( num ) ; }
502  // ========================================================================
504  void swap ( VectorMap& other )
505  {
506  std::swap ( m_vct , other.m_vct ) ;
507  }
508  // ========================================================================
509  // The basic comparison operators for container
510  // ========================================================================
512  bool operator== ( const VectorMap& other ) const
513  { return m_vct == other.m_vct ; }
515  bool operator< ( const VectorMap& other ) const
516  { return m_vct < other.m_vct ; }
517  // ========================================================================
518  // The derived comparison operators for container
519  // ========================================================================
520  friend bool operator> ( const VectorMap& left ,
521  const VectorMap& right )
522  { return right < left ; }
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 !( left < right ) ; }
529  friend bool operator<= ( const VectorMap& left ,
530  const VectorMap& right )
531  { return !( right < left ) ; }
532  // ========================================================================
562  bool update
563  ( const key_type& key ,
564  const mapped_type& mapped )
565  {
566  _iterator result = lower_bound ( key ) ;
567  if ( end() == result || compare ( key , result -> first ) )
568  {
569  result = m_vct.insert ( result , value_type(key,mapped) ) ;
570  return false ;
571  }
572  else { result->second = mapped ; }
573  //
574  return true ;
575  }
576  // ========================================================================
605  bool update ( const value_type& val )
606  { return update ( val.first , val.second ) ; }
607  // ========================================================================
639  const mapped_type& operator() ( const key_type& key ) const
640  {
641  static const mapped_type s_default = mapped_type() ;
642  iterator res = find ( key ) ;
643  if ( end() == res ) { return s_default ; }
644  return res->second ;
645  }
646  // ========================================================================
677  const mapped_type& operator[] ( const key_type& key ) const
678  { return (*this)( key ) ; }
679  // ========================================================================
697  const mapped_type& at ( const key_type& key ) const
698  {
699  iterator res = find ( key ) ;
700  if ( end() == res ) { this->throw_out_of_range_exception () ; }
701  return res->second ;
702  }
703  // ========================================================================
704  public:
705  // ========================================================================
706  // Constructors, destructors, etc.
707  // ========================================================================
713  : m_vct ( alloc )
714  {}
715  // ========================================================================
719  VectorMap ( const VectorMap& right )
720  : Gaudi::Utils::MapBase(right), m_vct ( right.m_vct )
721  {}
722  // ========================================================================
729  template <class INPUT>
730  VectorMap ( INPUT first ,
731  INPUT last ,
732  const allocator_type& alloc = allocator_type () )
733  : m_vct ( first , last , alloc )
734  { std::sort ( m_vct.begin(), m_vct.end(), compare() ) ; }
735  // ========================================================================
737  ~VectorMap() { clear() ; } // destructor (non-virtual!)
738  // ========================================================================
739  /* assignement operator
740  * @param rigth object to be assigned
741  * @return self
742  */
743  VectorMap& operator= ( const VectorMap& right )
744  {
745  if ( &right == this ) { return *this ; }
746  m_vct = right.m_vct ;
747  return *this ;
748  }
749  // ========================================================================
750  public:
751  // ========================================================================
752  // The specific public accessors
753  // ========================================================================
755  const compare_type& compare () const
756  {
757  static const compare_type s_cmp = compare_type() ;
758  return s_cmp ;
759  }
761  const key_compare& compare_key () const { return compare() ; }
763  friend std::ostream& operator<<
764  ( std::ostream& str , const VectorMap& /* obj */) { return str ; }
765  // ========================================================================
766  public:
767  // ========================================================================
769  inline VectorMap& merge ( const VectorMap& right )
770  {
771  for ( const_iterator it = right.begin() ; right.end() != it ; ++it )
772  { update ( it->first , it->second ) ; }
773  //
774  return *this ;
775  }
777  template <class K1,class K2, class K3,class K4>
778  inline VectorMap& merge ( const VectorMap<K1,K2,K3,K4>& right )
779  {
780  for ( typename VectorMap<K1,K2,K3,K4>::const_iterator it =
781  right.begin() ; right.end() != it ; ++it )
782  { update ( it->first , it->second ) ; }
783  //
784  return *this ;
785  }
786  // ========================================================================
787  public:
788  // ========================================================================
794  const key_type& key_at ( const size_t index ) const
795  {
796  if ( index >= size() )
797  { this->throw_out_of_range_exception () ; }
798  const_iterator it = this->begin() ;
799  std::advance ( it , index ) ;
800  return it -> first ;
801  }
807  const mapped_type& value_at ( const size_t index ) const
808  {
809  if ( index >= size() )
810  { this->throw_out_of_range_exception () ; }
811  const_iterator it = this->begin() ;
812  std::advance ( it , index ) ;
813  return it -> second ;
814  }
815  // ========================================================================
816  protected:
817  // ========================================================================
818  // Pure technical helper functions
819  // ========================================================================
825  template <class TYPE1, class TYPE2>
826  bool compare ( const TYPE1& obj1 ,
827  const TYPE2& obj2 ) const
828  {
829  return compare() ( obj1 , obj2 ) ;
830  }
831  // ========================================================================
834  {
835  return std::lower_bound
836  ( m_vct.begin() , m_vct.end() , key , compare() ) ;
837  }
838  // ========================================================================
841  {
842  _iterator result = m_vct.begin() ;
843  std::advance ( result , std::distance ( begin() , p ) ) ;
844  return result ;
845  }
846  // ========================================================================
849  {
850  iterator result ( begin() ) ;
851  std::advance ( result , std::distance ( m_vct.begin() , p ) ) ;
852  return result ;
853  }
854  // ========================================================================
855  private:
856  // ========================================================================
858  _vector m_vct ; // the underlying sorted vector of (key,mapped) pairs
859  // ========================================================================
860  };
861  // ==========================================================================
862 } // end of namespace GaudiUtils
863 // ============================================================================
864 namespace std
865 {
866  // ==========================================================================
871  template
872  < class KEY ,
873  class VALUE ,
874  class KEYCOMPARE ,
875  class ALLOCATOR >
876  inline void swap
879  { left.swap( right ) ; }
880  // ===========================================================================
881 } // end of namespace std
882 // ============================================================================
883 // ============================================================================
884 namespace Gaudi
885 {
886  // ==========================================================================
887  namespace Parsers
888  {
889  // ========================================================================
901  const std::string& input ) ;
902  // ========================================================================
915  const std::string& input ) ;
916  // ========================================================================
917  } // end of namespace Gaudi::Parsers
918  // ==========================================================================
919 } // end of namespace Gaudi
920 
921 
922 // ============================================================================
923 // The END
924 // ============================================================================
925 #endif // GAUDIKERNEL_MAPS_H
926 // ============================================================================
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:677
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:110
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:523
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:512
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:697
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:639
~VectorMap()
destructor (non-virtual!)
Definition: VectorMap.h:737
iterator iter(_iterator p)
the conversion from 'non-const' to 'const' iterator
Definition: VectorMap.h:848
void reserve(size_type num)
reserve the space in the container for at least 'num' elements
Definition: VectorMap.h:501
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:826
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:144
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:794
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:149
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:270
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:176
result_type insert(const key_type &key, const mapped_type &mapped)
insert the (key,value) pair into the container
Definition: VectorMap.h:319
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:778
VectorMap(const VectorMap &right)
copy constructor
Definition: VectorMap.h:719
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:229
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:497
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:457
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:761
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:202
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:157
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:483
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:200
ALLOCATOR::const_reference reference
the types to conform STL
Definition: VectorMap.h:128
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:242
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:858
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:155
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:198
_compare_type()
default constructor
Definition: VectorMap.h:174
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:526
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:153
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:520
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:159
ALLOCATOR::const_reference const_reference
the types to conform STL
Definition: VectorMap.h:130
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:172
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:605
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:529
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:730
IInterface compliant class extending IInterface with the name() method.
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:204
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:117
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:191
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:151
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:485
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:515
VectorMap & operator=(const VectorMap &right)
Definition: VectorMap.h:743
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:480
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:755
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:126
std
AIDA -> ROTO converter.
Definition: GaudiAlgs.py:73
_iterator iter(iterator p)
the conversion from 'const' to 'non-const' iterator
Definition: VectorMap.h:840
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:563
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:119
void clear()
clear the container
Definition: VectorMap.h:499
KEY key_type
the actual type of key
Definition: VectorMap.h:115
void throw_out_of_range_exception() const
throw std::out_of_range exception
Definition: MapBase.cpp:29
void swap(VectorMap &other)
swap function, which 'swaps' the content of two containers
Definition: VectorMap.h:504
The actual structure used to compare the elements Only "key" is important for comparison.
Definition: VectorMap.h:167
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:134
bool empty() const
empty container ?
Definition: VectorMap.h:493
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:487
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:121
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:769
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:15
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:46
ALLOCATOR::size_type size_type
the types to conform STL
Definition: VectorMap.h:132
#define GAUDI_API
Definition: Kernel.h:108
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:211
VectorMap(const allocator_type &alloc=allocator_type())
default constructor from the the allocator
Definition: VectorMap.h:712
_iterator lower_bound(const key_type &key)
'lower-bound' - non-const version
Definition: VectorMap.h:833
size_type size() const
number of elements
Definition: VectorMap.h:495
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:139
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:807