All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Range.h
Go to the documentation of this file.
1 // $Id: Range.h,v 1.2 2008/11/03 11:24:47 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDI_RANGE_H
4 #define GAUDI_RANGE_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <utility>
11 #include <vector>
12 #include <algorithm>
13 #include "GaudiKernel/Kernel.h"
14 // ============================================================================
30 // ============================================================================
31 namespace Gaudi
32 {
33  namespace details
34  {
35  // ========================================================================
42  ( const long index ,
43  const size_t size ) ;
44  // ========================================================================
45  }
46  // ==========================================================================
52  struct RangeBase_ {} ;
53  // ==========================================================================
76  template <class CONTAINER>
77  class Range_ : public RangeBase_
78  {
79  public:
80  // ========================================================================
81  typedef std::pair<typename CONTAINER::const_iterator,
82  typename CONTAINER::const_iterator> Base ;
83  // ========================================================================
84  public:
85  // ========================================================================
87  typedef CONTAINER Container ;
88  typedef typename Container::value_type value_type ;
89  typedef typename Container::const_iterator iterator ;
90  typedef typename Container::const_iterator const_iterator ;
91  typedef typename Container::const_reverse_iterator reverse_iterator ;
92  typedef typename Container::const_reverse_iterator const_reverse_iterator ;
93  typedef typename Container::const_reference reference ;
94  typedef typename Container::const_reference const_reference ;
96  typedef std::pair<iterator,iterator> _Base ;
98  // ========================================================================
99  public:
100  // ========================================================================
102  Range_() : m_base( iterator() , iterator() ) {}
107  Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
111  Range_( const Base& base ) : m_base( base ) {}
115  Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
116  /* constructor of empty range/sequence
117  * @param ibegin iterator to begin of empty sequence
118  */
119  Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
122  // ========================================================================
124  inline bool empty () const { return m_base.second == m_base.first ; }
126  inline size_t size () const
127  { return std::distance ( m_base.first , m_base.second ) ; }
129  inline iterator begin () const { return m_base.first ; }
131  inline iterator end () const { return m_base.second ; }
133  inline reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
135  inline reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
137  inline const_reference front () const { return *( begin () ) ; }
139  inline const_reference back () const
140  {
141  const_iterator i = end() ;
142  std::advance ( i , -1 ) ;
143  return *i ;
144  }
145  // ========================================================================
147  inline Range_ slice( long index1 , long index2 ) const
148  {
149  // trivial cases
150  if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
151  // adjust indices
152  if ( index1 < 0 ) { index1 += size () ; }
153  if ( index2 < 0 ) { index2 += size () ; }
154  // check
155  if ( index1 < 0 ) { return Range_ () ; } // RETURN
156  if ( index2 < index1 ) { return Range_ () ; } // RETURN
157 
158  if ( index1 > (long) size () ) { return Range_() ; } // RETURN
159  if ( index2 > (long) size () ) { index2 = size() ; }
160 
161  const_iterator i1 = begin() ;
162  std::advance ( i1 , index1 ) ;
163  const_iterator i2 = begin() ;
164  std::advance ( i2 , index2 ) ;
165  // construct the slice
166  return Range_( i1 , i2 ) ; // RETURN
167  }
168  // ========================================================================
173  inline const_reference operator () ( const size_t index ) const
174  {
175  const_iterator i = begin() ;
176  std::advance ( i , index ) ;
177  return *i ;
178  }
183  inline const_reference operator [] ( const long index ) const
184  { return (*this)( index ) ; }
190  inline const_reference at ( const long index ) const
191  {
192  if ( index < 0 || index >= (long) size () )
193  { Gaudi::details::rangeException( index , size() ) ; }
194  return (*this) ( index );
195  }
196  // ========================================================================
197  public:
198  // ========================================================================
200  bool operator< ( const Range_& right ) const
201  {
202  return std::lexicographical_compare
203  ( begin () , end () , right.begin () , right.end () ) ;
204  }
206  bool operator< ( const Container& right ) const
207  {
208  return std::lexicographical_compare
209  ( begin () , end () , right.begin () , right.end () ) ;
210  }
211  // ========================================================================
212  public:
213  // ========================================================================
215  bool operator==( const Range_& right ) const
216  {
217  if ( &right == this ) { return true ; } // RETURN
218  if ( right.size () != size () ) { return false ; } // RETURN
219  return std::equal ( begin () , end () , right.begin() ) ;
220  }
222  bool operator==( const Container& right ) const
223  {
224  if ( right.size () != size () ) { return false ; } // RETURN
225  return std::equal ( begin () , end () , right.begin() ) ;
226  }
227  // ========================================================================
228  public:
229  // ========================================================================
231  bool operator! () const { return empty () ; }
232  // ========================================================================
233  public:
234  // ========================================================================
236  operator const Base& () const { return base () ; }
238  inline const Base& base () const { return m_base ; }
239  // ========================================================================
240  private:
241  // ========================================================================
242  // the base itself
244  // ========================================================================
245  }; // end of class Range_
246  // ==========================================================================
274  template <class CONTAINER>
275  inline
277  range ( const CONTAINER& cnt )
278  { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
279  // ==========================================================================
280 } // end of namespace Gaudi
281 // ============================================================================
282 // The END
283 // ============================================================================
284 #endif // GAUDI_RANGE_H
285 // ============================================================================
bool empty() const
empty sequence ?
Definition: Range.h:124
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:215
std::pair< iterator, iterator > _Base
internal types
Definition: Range.h:96
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:183
std::pair< typename CONTAINER::const_iterator, typename CONTAINER::const_iterator > Base
Definition: Range.h:82
~Range_()
destructor
Definition: Range.h:121
GAUDI_API void rangeException(const long index, const size_t size)
Helpful function to throw an "out-of-range exception" for class Range_.
Definition: Range.cpp:31
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:147
const_reference operator()(const size_t index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:173
helper class to simplify the dealing with ranges in Python
Definition: Range.h:52
Base m_base
the base itself
Definition: Range.h:243
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:107
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:139
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:137
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences) for out-of-range access...
Definition: Range.h:190
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:126
Range_< Container > _Self
Definition: Range.h:97
bool operator<(const Range_ &right) const
compare with another range
Definition: Range.h:200
Container::const_reverse_iterator const_reverse_iterator
Definition: Range.h:92
CONTAINER Container
type for actual contained iterator
Definition: Range.h:87
Container::value_type value_type
Definition: Range.h:88
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:129
bool operator==(const Container &right) const
equality with the base container
Definition: Range.h:222
Container::const_iterator const_iterator
Definition: Range.h:90
Range_(const Container &cont)
constructor from the container
Definition: Range.h:115
Container::const_iterator iterator
Definition: Range.h:89
Container::const_reverse_iterator reverse_iterator
Definition: Range.h:91
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:238
Range_(iterator ibegin)
Definition: Range.h:119
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:77
Range_()
default constructor
Definition: Range.h:102
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:131
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:111
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:15
NamedRange_< CONTAINER > range(const CONTAINER &cnt, const std::string &name)
simple function to create the named range form arbitrary container
Definition: NamedRange.h:133
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:133
Container::const_reference reference
Definition: Range.h:93
#define GAUDI_API
Definition: Kernel.h:108
list i
Definition: ana.py:128
Container::const_reference const_reference
Definition: Range.h:94
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:135
bool operator!() const
empty sequence?
Definition: Range.h:231