Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/Kernel.h"
15 // ============================================================================
31 // ============================================================================
32 namespace Gaudi
33 {
34  // ==========================================================================
35  namespace details
36  {
37  // ========================================================================
44  ( const long index ,
45  const size_t size ) ;
46  // ========================================================================
47  template <typename T>
49  {
50  private:
51  template <typename T1> static typename T1::Container test(int);
52  template <typename> static void test(...);
53  public:
55  };
56  template <class CONTAINER,bool> struct _container ;
57  template <class CONTAINER>
58  struct _container<CONTAINER,true> { typedef typename CONTAINER::Container Container ; };
59  template <class CONTAINER>
60  struct _container<CONTAINER,false> { typedef CONTAINER Container ; };
61  // ==========================================================================
63  template <class CONTAINER>
64  struct container
65  {
67  typedef typename CONTAINER::const_iterator Iterator ;
68  };
69  // =========================================================================
70  } // the end of namespace Gaudi::details
71  // ==========================================================================
77  struct RangeBase_ {} ;
78  // ==========================================================================
102  class Range_ : public RangeBase_
103  {
104  public:
105  // ========================================================================
107  // ========================================================================
108  public:
109  // ========================================================================
112  //
113  typedef ITERATOR iterator ;
114  typedef ITERATOR const_iterator ;
115  //
116  private:
117  //
119  //
120  public:
121  //
122  typedef typename iter_traits::value_type value_type ;
125  //
129  // ========================================================================
130  public:
131  // ========================================================================
133  Range_() = default;
138  Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
142  Range_( const Base& base ) : m_base( base ) {}
146  Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
147  /* constructor of empty range/sequence
148  * @param ibegin iterator to begin of empty sequence
149  */
150  Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
152  ~Range_() = default;
153  // ========================================================================
155  bool empty () const { return m_base.second == m_base.first ; }
157  size_t size () const
158  { return std::distance ( m_base.first , m_base.second ) ; }
160  iterator begin () const { return m_base.first ; }
162  iterator end () const { return m_base.second ; }
164  iterator cbegin () const { return m_base.first ; }
166  iterator cend () const { return m_base.second ; }
168  reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
170  reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
172  const_reference front () const { return *begin() ; }
174  const_reference back () const { return *std::prev( end() ); }
175  // ========================================================================
177  Range_ slice( long index1 , long index2 ) const
178  {
179  // trivial cases
180  if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
181  // adjust indices
182  if ( index1 < 0 ) { index1 += size () ; }
183  if ( index2 < 0 ) { index2 += size () ; }
184  // check
185  if ( index1 < 0 ) { return Range_ () ; } // RETURN
186  if ( index2 < index1 ) { return Range_ () ; } // RETURN
187 
188  if ( index1 > (long) size () ) { return Range_() ; } // RETURN
189  if ( index2 > (long) size () ) { index2 = size() ; }
190 
191  // construct the slice
192  return Range_( std::next ( begin() , index1 ) ,
193  std::next ( begin() , index2 ) ) ; // RETURN
194  }
195  // ========================================================================
200  inline const_reference operator () ( const size_t index ) const
201  { return *std::next ( begin() , index ) ; }
206  inline const_reference operator [] ( const long index ) const
207  { return (*this)( index ) ; }
213  inline const_reference at ( const long index ) const
214  {
215  if ( index < 0 || index >= (long) size () )
216  { Gaudi::details::rangeException( index , size() ) ; }
217  return (*this) ( index );
218  }
219  // ========================================================================
220  public:
221  // ========================================================================
223  template <class C, class I>
224  bool operator< ( const Range_<C,I>& right ) const
225  {
227  ( begin () , end () , right.begin () , right.end () ) ;
228  }
230  template <class ANOTHERCONTAINER>
231  bool operator< ( const ANOTHERCONTAINER& right ) const
232  {
234  ( begin () , end () , right.begin () , right.end () ) ;
235  }
236  // ========================================================================
237  public:
238  // ========================================================================
240  bool operator==( const Range_& right ) const
241  {
242  if ( &right == this ) { return true ; } // RETURN
243  return right.size() == size() &&
244  std::equal ( begin () , end () , right.begin() ) ;
245  }
247  template <class CNT, class IT>
248  bool operator==( const Range_<CNT,IT>& right ) const
249  {
250  return right.size() == size() &&
251  std::equal ( begin () , end () , right.begin() ) ;
252  }
254  template <class ANOTHERCONTAINER>
255  bool operator==( const ANOTHERCONTAINER& right ) const
256  {
257  return right.size() == size() &&
258  std::equal ( begin () , end () , right.begin() ) ;
259  }
260  // ========================================================================
261  public:
262  // ========================================================================
264  bool operator! () const { return empty () ; }
266  explicit operator bool() const { return !empty () ; }
267  // ========================================================================
268  public:
269  // ========================================================================
271  operator const Base& () const { return base () ; }
273  inline const Base& base () const { return m_base ; }
274  // ========================================================================
275  private:
276  // ========================================================================
277  // the base itself
278  Base m_base ;
279  // ========================================================================
280  }; // end of class Range_
281  // ==========================================================================
309  template <class CONTAINER>
310  inline
312  range ( const CONTAINER& cnt )
313  { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
314  // ==========================================================================
315 } // end of namespace Gaudi
316 // ============================================================================
317 // The END
318 // ============================================================================
319 #endif // GAUDI_RANGE_H
320 // ============================================================================
bool empty() const
empty sequence ?
Definition: Range.h:155
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:127
Base m_base
the base itself
Definition: Range.h:278
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
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:164
iter_traits::value_type value_type
Definition: Range.h:122
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:126
helper structure to get container type
Definition: Range.h:64
std::iterator_traits< iterator > iter_traits
Definition: Range.h:118
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:177
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:106
helper class to simplify the dealing with ranges in Python
Definition: Range.h:77
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:168
iter_traits::reference reference
Definition: Range.h:123
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:248
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:172
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:48
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:138
T prev(T...args)
iter_traits::reference const_reference
Definition: Range.h:124
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:129
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:111
T next(T...args)
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:213
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:142
ITERATOR iterator
Definition: Range.h:113
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
T lexicographical_compare(T...args)
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:240
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
ITERATOR const_iterator
Definition: Range.h:114
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:162
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:273
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:166
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:255
Range_(iterator ibegin)
Definition: Range.h:150
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:102
details::_container< CONTAINER, details::_has_typename_container_< CONTAINER >::value >::Container Container
Definition: Range.h:66
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:157
CONTAINER::const_iterator Iterator
Definition: Range.h:67
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:170
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:174
#define GAUDI_API
Definition: Kernel.h:107
T equal(T...args)
Range_(const Container &cont)
constructor from the container
Definition: Range.h:146
Helper functions to set/get the application return code.
Definition: __init__.py:1
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:160