All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Range.h
Go to the documentation of this file.
1 #ifndef GAUDI_RANGE_H
2 #define GAUDI_RANGE_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <utility>
9 #include <vector>
10 #include <algorithm>
11 #include "GaudiKernel/Kernel.h"
12 // ============================================================================
28 // ============================================================================
29 namespace Gaudi
30 {
31  namespace details
32  {
33  // ========================================================================
40  ( const long index ,
41  const size_t size ) ;
42  // ========================================================================
43  }
44  // ==========================================================================
50  struct RangeBase_ {} ;
51  // ==========================================================================
74  template <class CONTAINER>
75  class Range_ : public RangeBase_
76  {
77  public:
78  // ========================================================================
79  typedef std::pair<typename CONTAINER::const_iterator,
80  typename CONTAINER::const_iterator> Base ;
81  // ========================================================================
82  public:
83  // ========================================================================
85  typedef CONTAINER Container ;
86  typedef typename Container::value_type value_type ;
87  typedef typename Container::const_iterator iterator ;
88  typedef typename Container::const_iterator const_iterator ;
89  typedef typename Container::const_reverse_iterator reverse_iterator ;
90  typedef typename Container::const_reverse_iterator const_reverse_iterator ;
91  typedef typename Container::const_reference reference ;
92  typedef typename Container::const_reference const_reference ;
96  // ========================================================================
97  public:
98  // ========================================================================
100  Range_() = default;
105  Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
109  Range_( const Base& base ) : m_base( base ) {}
113  Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
114  /* constructor of empty range/sequence
115  * @param ibegin iterator to begin of empty sequence
116  */
117  Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
119  ~Range_() = default;
120  // ========================================================================
122  inline bool empty () const { return m_base.second == m_base.first ; }
124  inline size_t size () const
125  { return std::distance ( m_base.first , m_base.second ) ; }
127  inline iterator begin () const { return m_base.first ; }
129  inline iterator end () const { return m_base.second ; }
131  inline reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
133  inline reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
135  inline const_reference front () const { return *begin() ; }
137  inline const_reference back () const { return *std::prev( end() ); }
138  // ========================================================================
140  inline Range_ slice( long index1 , long index2 ) const
141  {
142  // trivial cases
143  if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
144  // adjust indices
145  if ( index1 < 0 ) { index1 += size () ; }
146  if ( index2 < 0 ) { index2 += size () ; }
147  // check
148  if ( index1 < 0 ) { return Range_ () ; } // RETURN
149  if ( index2 < index1 ) { return Range_ () ; } // RETURN
150 
151  if ( index1 > (long) size () ) { return Range_() ; } // RETURN
152  if ( index2 > (long) size () ) { index2 = size() ; }
153 
154  // construct the slice
155  return Range_( std::next ( begin() , index1 ) ,
156  std::next ( begin() , index2 ) ) ; // RETURN
157  }
158  // ========================================================================
163  inline const_reference operator () ( const size_t index ) const
164  {
165  return *std::next ( begin() , index ) ;
166  }
171  inline const_reference operator [] ( const long index ) const
172  { return (*this)( index ) ; }
178  inline const_reference at ( const long index ) const
179  {
180  if ( index < 0 || index >= (long) size () )
181  { Gaudi::details::rangeException( index , size() ) ; }
182  return (*this) ( index );
183  }
184  // ========================================================================
185  public:
186  // ========================================================================
188  bool operator< ( const Range_& right ) const
189  {
191  ( begin () , end () , right.begin () , right.end () ) ;
192  }
194  bool operator< ( const Container& right ) const
195  {
197  ( begin () , end () , right.begin () , right.end () ) ;
198  }
199  // ========================================================================
200  public:
201  // ========================================================================
203  bool operator==( const Range_& right ) const
204  {
205  if ( &right == this ) { return true ; } // RETURN
206  return right.size() == size() &&
207  std::equal ( begin () , end () , right.begin() ) ;
208  }
210  bool operator==( const Container& right ) const
211  {
212  return right.size() == size() &&
213  std::equal ( begin () , end () , right.begin() ) ;
214  }
215  // ========================================================================
216  public:
217  // ========================================================================
219  bool operator! () const { return empty () ; }
220  // ========================================================================
221  public:
222  // ========================================================================
224  operator const Base& () const { return base () ; }
226  inline const Base& base () const { return m_base ; }
227  // ========================================================================
228  private:
229  // ========================================================================
230  // the base itself
231  Base m_base ;
232  // ========================================================================
233  }; // end of class Range_
234  // ==========================================================================
262  template <class CONTAINER>
263  inline
265  range ( const CONTAINER& cnt )
266  { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
267  // ==========================================================================
268 } // end of namespace Gaudi
269 // ============================================================================
270 // The END
271 // ============================================================================
272 #endif // GAUDI_RANGE_H
273 // ============================================================================
bool empty() const
empty sequence ?
Definition: Range.h:122
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:203
std::pair< iterator, iterator > _Base
internal types
Definition: Range.h:94
std::pair< typename CONTAINER::const_iterator, typename CONTAINER::const_iterator > Base
Definition: Range.h:80
T distance(T...args)
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:140
helper class to simplify the dealing with ranges in Python
Definition: Range.h:50
Base m_base
the base itself
Definition: Range.h:231
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:105
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:137
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:135
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:178
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:124
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:47
Range_< Container > _Self
Definition: Range.h:95
T prev(T...args)
Container::const_reverse_iterator const_reverse_iterator
Definition: Range.h:90
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
Definition: NamedRange.h:130
CONTAINER Container
type for actual contained iterator
Definition: Range.h:85
T next(T...args)
Container::value_type value_type
Definition: Range.h:86
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:127
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:49
bool operator==(const Container &right) const
equality with the base container
Definition: Range.h:210
Container::const_iterator const_iterator
Definition: Range.h:88
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
T lexicographical_compare(T...args)
Range_(const Container &cont)
constructor from the container
Definition: Range.h:113
Container::const_iterator iterator
Definition: Range.h:87
Container::const_reverse_iterator reverse_iterator
Definition: Range.h:89
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:226
Range_(iterator ibegin)
Definition: Range.h:117
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:75
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:129
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:109
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:131
Container::const_reference reference
Definition: Range.h:91
#define GAUDI_API
Definition: Kernel.h:107
T equal(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1
Container::const_reference const_reference
Definition: Range.h:92
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:133