Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 <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  // ==========================================================================
100  template <class KEY, class VALUE, class KEYCOMPARE = std::less<const KEY>,
101  class ALLOCATOR = std::allocator<std::pair<KEY, VALUE>>>
103  public:
104  // ========================================================================
106  typedef KEY key_type;
108  typedef VALUE mapped_type;
110  typedef KEYCOMPARE key_compare;
113  // ========================================================================
114  public:
115  // ========================================================================
117  typedef ALLOCATOR allocator_type;
119  typedef typename ALLOCATOR::const_reference reference;
121  typedef typename ALLOCATOR::const_reference const_reference;
123  typedef typename ALLOCATOR::size_type size_type;
125  typedef typename ALLOCATOR::difference_type difference_type;
126  // ========================================================================
127  public:
128  // ========================================================================
131  // ========================================================================
132  protected:
133  // ========================================================================
135  typedef typename _vector::iterator _iterator;
136  // ========================================================================
137  public:
138  // ========================================================================
140  typedef typename _vector::const_iterator iterator;
142  typedef typename _vector::const_iterator const_iterator;
151  // ========================================================================
152  public:
153  // ========================================================================
158  struct _compare_type : public key_compare {
159  public:
160  // ======================================================================
162  _compare_type( const key_compare& cmp ) : key_compare( cmp ) {}
164  _compare_type() : key_compare() {}
166  bool operator()( const key_type& k1, const key_type& k2 ) const {
167  return this->key_compare::operator()( k1, k2 );
168  }
170  bool operator()( const value_type& v1, const value_type& v2 ) const { return operator()( v1.first, v2.first ); }
172  bool operator()( const key_type& k, const value_type& v ) const { return operator()( k, v.first ); }
174  bool operator()( const value_type& v, const key_type& k ) const { return operator()( v.first, k ); }
175  // ======================================================================
176  };
177  // ========================================================================
180  // ========================================================================
181  public:
182  // ========================================================================
183  // sequential access (only const-versions!)
184  // ========================================================================
186  iterator begin() const { return m_vct.begin(); }
188  iterator end() const { return m_vct.end(); }
190  reverse_iterator rbegin() const { return m_vct.rbegin(); }
192  reverse_iterator rend() const { return m_vct.rend(); }
193  // ========================================================================
194  // list operations : erase & insert
195  // ========================================================================
199  void erase( iterator pos ) { m_vct.erase( iter( pos ) ); }
200  // ========================================================================
217  size_type erase( const key_type& key ) {
218  iterator pos = find( key );
219  if ( end() == pos ) { return 0; }
220  erase( pos );
221  return 1;
222  }
223  // ========================================================================
229  size_type erase( iterator first, iterator last ) {
230  m_vct.erase( iter( first ), iter( last ) );
231  return last - first;
232  }
233  // ========================================================================
254  template <class TYPE>
255  size_type erase( TYPE first, TYPE last ) {
256  size_type res = 0;
257  for ( ; first != last; ++first ) { res += erase( *first ); }
258  return res;
259  }
260  // ========================================================================
302  result_type insert( const key_type& key, const mapped_type& mapped ) { return insert( value_type( key, mapped ) ); }
303  // ========================================================================
344  result_type insert( const value_type& value ) {
345  bool found = true;
346  _iterator result = lower_bound( value.first );
347  if ( end() == result || compare( value.first, result->first ) ) {
348  result = m_vct.insert( result, value );
349  found = false;
350  }
351  return result_type( iter( result ), !found );
352  }
353  // ========================================================================
362  result_type insert( iterator pos, const value_type& value ) {
363  if ( pos != end() && compare( *pos, value ) &&
364  ( pos == end() - 1 || ( !compare( value, *( pos + 1 ) ) && compare( *( pos + 1 ), value ) ) ) ) {
365  return result_type( m_vct.insert( iter( pos ), value ), true );
366  }
367  return insert( value );
368  }
369  // ========================================================================
379  result_type insert( iterator pos, const key_type& key, const mapped_type& mapped ) {
380  return insert( pos, value_type( key, mapped ) );
381  }
382  // ========================================================================
388  template <class PAIRS>
389  void insert( PAIRS first, PAIRS last ) {
390  for ( ; first != last; ++first ) { insert( *first ); }
391  }
392  // ========================================================================
400  template <class KEYS, class VALUES>
401  void insert( KEYS kf, KEYS kl, VALUES vf ) {
402  for ( ; kf != kl; ++kf, ++vf ) { insert( *kf, *vf ); }
403  }
404  // ========================================================================
405  // map operations: lookup, count, ...
406  // ========================================================================
430  iterator find( const key_type& key ) const {
431  iterator res = lower_bound( key );
432  if ( end() != res && compare( key, res->first ) ) { res = end(); }
433  return res;
434  }
435  // ========================================================================
451  size_type count( const key_type& key ) const { return end() == find( key ) ? 0 : 1; }
452  // ========================================================================
453  iterator lower_bound( const key_type& key ) const { return std::lower_bound( begin(), end(), key, compare() ); }
454  iterator upper_bound( const key_type& key ) const { return std::upper_bound( begin(), end(), key, compare() ); }
455  iterators equal_range( const key_type& key ) const { return std::equal_range( begin(), end(), key, compare() ); }
456  // ========================================================================
457  // general container operations :
458  // ========================================================================
460  bool empty() const { return m_vct.empty(); }
462  size_type size() const { return m_vct.size(); }
464  size_type max_size() const { return m_vct.max_size(); }
466  void clear() { m_vct.clear(); }
468  void reserve( size_type num ) { m_vct.reserve( num ); }
469  // ========================================================================
471  void swap( VectorMap& other ) { std::swap( m_vct, other.m_vct ); }
472  // ========================================================================
473  // The basic comparison operators for container
474  // ========================================================================
476  bool operator==( const VectorMap& other ) const { return m_vct == other.m_vct; }
478  bool operator<( const VectorMap& other ) const { return m_vct < other.m_vct; }
479  // ========================================================================
480  // The derived comparison operators for container
481  // ========================================================================
482  friend bool operator>( const VectorMap& left, const VectorMap& right ) { return right < left; }
483  friend bool operator!=( const VectorMap& left, const VectorMap& right ) { return !( left == right ); }
484  friend bool operator>=( const VectorMap& left, const VectorMap& right ) { return !( left < right ); }
485  friend bool operator<=( const VectorMap& left, const VectorMap& right ) { return !( right < left ); }
486  // ========================================================================
516  bool update( const key_type& key, const mapped_type& mapped ) {
517  _iterator result = lower_bound( key );
518  if ( end() == result || compare( key, result->first ) ) {
519  result = m_vct.insert( result, value_type( key, mapped ) );
520  return false;
521  } else {
522  result->second = mapped;
523  }
524  //
525  return true;
526  }
527  // ========================================================================
556  bool update( const value_type& val ) { return update( val.first, val.second ); }
557  // ========================================================================
589  const mapped_type& operator()( const key_type& key ) const {
590  static const mapped_type s_default = mapped_type();
591  iterator res = find( key );
592  if ( end() == res ) { return s_default; }
593  return res->second;
594  }
595  // ========================================================================
626  const mapped_type& operator[]( const key_type& key ) const { return ( *this )( key ); }
627  // ========================================================================
645  const mapped_type& at( const key_type& key ) const {
646  iterator res = find( key );
647  if ( end() == res ) { this->throw_out_of_range_exception(); }
648  return res->second;
649  }
650  // ========================================================================
651  public:
652  // ========================================================================
653  // Constructors, destructors, etc.
654  // ========================================================================
659  VectorMap( const allocator_type& alloc = allocator_type() ) : m_vct( alloc ) {}
660  // ========================================================================
667  template <class INPUT>
668  VectorMap( INPUT first, INPUT last, const allocator_type& alloc = allocator_type() ) : m_vct( first, last, alloc ) {
669  std::sort( m_vct.begin(), m_vct.end(), compare() );
670  }
671  // ========================================================================
677  VectorMap( std::initializer_list<value_type> first, const allocator_type& alloc = allocator_type() )
678  : m_vct( first, alloc ) {
679  std::sort( m_vct.begin(), m_vct.end(), compare() );
680  }
681  // ========================================================================
682  public:
683  // ========================================================================
684  // The specific public accessors
685  // ========================================================================
687  const compare_type& compare() const {
688  static const compare_type s_cmp = compare_type();
689  return s_cmp;
690  }
692  const key_compare& compare_key() const { return compare(); }
694  friend std::ostream& operator<<( std::ostream& str, const VectorMap& /* obj */ ) { return str; }
695  // ========================================================================
696  public:
697  // ========================================================================
699  inline VectorMap& merge( const VectorMap& right ) {
700  for ( const auto& i : right ) { update( i.first, i.second ); }
701  return *this;
702  }
704  template <class K1, class K2, class K3, class K4>
705  inline VectorMap& merge( const VectorMap<K1, K2, K3, K4>& right ) {
706  for ( const auto& i : right ) { update( i.first, i.second ); }
707  return *this;
708  }
709  // ========================================================================
710  public:
711  // ========================================================================
717  const key_type& key_at( const size_t index ) const {
718  if ( index >= size() ) { this->throw_out_of_range_exception(); }
719  auto it = this->begin();
720  std::advance( it, index );
721  return it->first;
722  }
728  const mapped_type& value_at( const size_t index ) const {
729  if ( index >= size() ) { this->throw_out_of_range_exception(); }
730  auto it = this->begin();
731  std::advance( it, index );
732  return it->second;
733  }
734  // ========================================================================
735  protected:
736  // ========================================================================
737  // Pure technical helper functions
738  // ========================================================================
744  template <class TYPE1, class TYPE2>
745  bool compare( const TYPE1& obj1, const TYPE2& obj2 ) const {
746  return compare()( obj1, obj2 );
747  }
748  // ========================================================================
750  _iterator lower_bound( const key_type& key ) {
751  return std::lower_bound( m_vct.begin(), m_vct.end(), key, compare() );
752  }
753  // ========================================================================
755  _iterator iter( iterator p ) {
756  auto result = m_vct.begin();
757  std::advance( result, std::distance( begin(), p ) );
758  return result;
759  }
760  // ========================================================================
762  iterator iter( _iterator p ) {
763  auto result = begin();
764  std::advance( result, std::distance( m_vct.begin(), p ) );
765  return result;
766  }
767  // ========================================================================
768  private:
769  // ========================================================================
771  _vector m_vct; // the underlying sorted vector of (key,mapped) pairs
772  // ========================================================================
773  };
774  // ==========================================================================
775 } // end of namespace GaudiUtils
776 // ============================================================================
777 namespace std {
778  // ==========================================================================
783  template <class KEY, class VALUE, class KEYCOMPARE, class ALLOCATOR>
786  left.swap( right );
787  }
788  // ===========================================================================
789 } // end of namespace std
790 // ============================================================================
791 // ============================================================================
792 namespace Gaudi {
793  // ==========================================================================
794  namespace Parsers {
795  // ========================================================================
806  // ========================================================================
818  // ========================================================================
819  } // namespace Parsers
820  // ==========================================================================
821 } // end of namespace Gaudi
822 
823 // ============================================================================
824 // The END
825 // ============================================================================
826 #endif // GAUDIKERNEL_MAPS_H
std::pair< iterator, bool > result_type
visible iterator pait
Definition: VectorMap.h:150
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:626
bool operator()(const value_type &v1, const value_type &v2) const
compare pairs (key,mapped): use compare by keys
Definition: VectorMap.h:170
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:102
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:174
T empty(T...args)
friend bool operator!=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:483
bool operator==(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:476
std::vector< value_type, allocator_type > _vector
the actual storage container (no export)
Definition: VectorMap.h:130
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:645
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:589
iterator iter(_iterator p)
the conversion from &#39;non-const&#39; to &#39;const&#39; iterator
Definition: VectorMap.h:762
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:468
bool compare(const TYPE1 &obj1, const TYPE2 &obj2) const
compare the objects using the comaprison criteria
Definition: VectorMap.h:745
T advance(T...args)
T upper_bound(T...args)
_vector::iterator _iterator
the regular iterator (no export)
Definition: VectorMap.h:135
const key_type & key_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:717
STL namespace.
_vector::const_iterator iterator
visible const_iterator (exported)
Definition: VectorMap.h:140
size_type erase(TYPE first, TYPE last)
erase the sequence of elements using the sequence of keys
Definition: VectorMap.h:255
T end(T...args)
bool operator()(const key_type &k1, const key_type &k2) const
compare keys: use key_compare
Definition: VectorMap.h:166
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:302
friend std::ostream & operator<<(std::ostream &str, const VectorMap &)
printout to ostream - not implemented
Definition: VectorMap.h:694
VectorMap & merge(const VectorMap< K1, K2, K3, K4 > &right)
merge two maps
Definition: VectorMap.h:705
std::pair< key_type, mapped_type > value_type
the actual storage item
Definition: VectorMap.h:112
size_type erase(const key_type &key)
erase the element using the key
Definition: VectorMap.h:217
STL class.
size_type max_size() const
maximal allowed size
Definition: VectorMap.h:464
iterator find(const key_type &key) const
find the element by key
Definition: VectorMap.h:430
const key_compare & compare_key() const
get the comparison criteria for keys
Definition: VectorMap.h:692
reverse_iterator rbegin() const
"rbegin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:190
iterator lower_bound(const key_type &key) const
Definition: VectorMap.h:453
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:188
ALLOCATOR::const_reference reference
the types to conform STL
Definition: VectorMap.h:119
size_type erase(iterator first, iterator last)
erase the sequence of elements using the iterators
Definition: VectorMap.h:229
_vector m_vct
the underlying sorted vector of (key,mapped) pairs
Definition: VectorMap.h:771
std::reverse_iterator< const_iterator > const_reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:146
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:186
void insert(PAIRS first, PAIRS last)
insert the sequence of elements into the container
Definition: VectorMap.h:389
T erase(T...args)
_compare_type()
default constructor
Definition: VectorMap.h:164
void insert(KEYS kf, KEYS kl, VALUES vf)
insert into the container the elements from 2 "parallel" sequences
Definition: VectorMap.h:401
friend bool operator>=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:484
std::reverse_iterator< iterator > reverse_iterator
visible reverse const_iterator (exported)
Definition: VectorMap.h:144
T clear(T...args)
friend bool operator>(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:482
ALLOCATOR::const_reference const_reference
the types to conform STL
Definition: VectorMap.h:121
_compare_type(const key_compare &cmp)
constructor from the key-comparison criteria
Definition: VectorMap.h:162
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:556
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:379
T insert(T...args)
VectorMap(std::initializer_list< value_type > first, const allocator_type &alloc=allocator_type())
tconstructor from initializer list
Definition: VectorMap.h:677
friend bool operator<=(const VectorMap &left, const VectorMap &right)
Definition: VectorMap.h:485
VectorMap(INPUT first, INPUT last, const allocator_type &alloc=allocator_type())
templated constructor from "convertible" sequence
Definition: VectorMap.h:668
T size(T...args)
StatusCode parse(DataObjID &dest, const std::string &src)
Definition: DataObjID.cpp:46
reverse_iterator rend() const
"rend" iterator for sequential access (const-only version!)
Definition: VectorMap.h:192
std::pair< iterator, iterator > iterators
visible iterator pait
Definition: VectorMap.h:148
VALUE mapped_type
the actual type of value
Definition: VectorMap.h:108
_compare_type compare_type
the actual comparison criteria for valye_type objects
Definition: VectorMap.h:179
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:142
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
iterator upper_bound(const key_type &key) const
Definition: VectorMap.h:454
T begin(T...args)
bool operator<(const VectorMap &other) const
comparison criteria for containers
Definition: VectorMap.h:478
size_type count(const key_type &key) const
count number of elements with the certain key
Definition: VectorMap.h:451
const compare_type & compare() const
get the comparison criteria itself
Definition: VectorMap.h:687
ALLOCATOR allocator_type
allocator (could be useful for optimizations)
Definition: VectorMap.h:117
T max_size(T...args)
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
_iterator iter(iterator p)
the conversion from &#39;const&#39; to &#39;non-const&#39; iterator
Definition: VectorMap.h:755
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:516
KEYCOMPARE key_compare
comparison of keys
Definition: VectorMap.h:110
void clear()
clear the container
Definition: VectorMap.h:466
KEY key_type
the actual type of key
Definition: VectorMap.h:106
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:471
The actual structure used to compare the elements Only "key" is important for comparison.
Definition: VectorMap.h:158
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:362
ALLOCATOR::difference_type difference_type
the types to conform STL
Definition: VectorMap.h:125
bool empty() const
empty container ?
Definition: VectorMap.h:460
iterators equal_range(const key_type &key) const
Definition: VectorMap.h:455
result_type insert(const value_type &value)
insert the (key,value) pair into the container
Definition: VectorMap.h:344
VectorMap & merge(const VectorMap &right)
merge two maps
Definition: VectorMap.h:699
Helper base-class to allow the generic Python-decoration for all "map-like" classes in Gaudi...
Definition: MapBase.h:42
ALLOCATOR::size_type size_type
the types to conform STL
Definition: VectorMap.h:123
#define GAUDI_API
Definition: Kernel.h:71
void erase(iterator pos)
erase the element using the iterator
Definition: VectorMap.h:199
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:659
_iterator lower_bound(const key_type &key)
&#39;lower-bound&#39; - non-const version
Definition: VectorMap.h:750
size_type size() const
number of elements
Definition: VectorMap.h:462
bool operator()(const key_type &k, const value_type &v) const
compare key and pair (key,mapped): use compare by keys
Definition: VectorMap.h:172
T reserve(T...args)
const mapped_type & value_at(const size_t index) const
useful method for python decoration:
Definition: VectorMap.h:728
T rbegin(T...args)