The Gaudi Framework  v29r0 (ff2e7097)
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 <algorithm>
9 #include <utility>
10 #include <vector>
11 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/Kernel.h"
15 // ============================================================================
31 // ============================================================================
32 namespace Gaudi
33 {
34  // ==========================================================================
35  namespace details
36  {
37  // ========================================================================
43  GAUDI_API void rangeException( const long index, const size_t size );
44  // ========================================================================
45  template <typename T>
47  private:
48  template <typename T1>
49  static typename T1::Container test( int );
50  template <typename>
51  static void test( ... );
52 
53  public:
55  };
56  template <class CONTAINER, bool>
57  struct _container;
58  template <class CONTAINER>
59  struct _container<CONTAINER, true> {
60  typedef typename CONTAINER::Container Container;
61  };
62  template <class CONTAINER>
63  struct _container<CONTAINER, false> {
64  typedef CONTAINER Container;
65  };
66  // ==========================================================================
68  template <class CONTAINER>
69  struct container {
72  typedef typename CONTAINER::const_iterator Iterator;
73  };
74  // =========================================================================
75  } // the end of namespace Gaudi::details
76  // ==========================================================================
82  struct RangeBase_ {
83  };
84  // ==========================================================================
108  class Range_ : public RangeBase_
109  {
110  public:
111  // ========================================================================
113  // ========================================================================
114  public:
115  // ========================================================================
118  //
119  typedef ITERATOR iterator;
120  typedef ITERATOR const_iterator;
121  //
122  private:
123  //
125  //
126  public:
127  //
128  typedef typename iter_traits::value_type value_type;
131  //
135  // ========================================================================
136  public:
137  // ========================================================================
139  Range_() = default;
144  Range_( iterator ibegin, iterator iend ) : m_base( ibegin, iend ) {}
148  Range_( const Base& base ) : m_base( base ) {}
152  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
153  /* constructor of empty range/sequence
154  * @param ibegin iterator to begin of empty sequence
155  */
156  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
158  ~Range_() = default;
159  // ========================================================================
161  bool empty() const { return m_base.second == m_base.first; }
163  size_t size() const { return std::distance( m_base.first, m_base.second ); }
165  iterator begin() const { return m_base.first; }
167  iterator end() const { return m_base.second; }
169  iterator cbegin() const { return m_base.first; }
171  iterator cend() const { return m_base.second; }
173  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
175  reverse_iterator rend() const { return reverse_iterator( begin() ); }
177  const_reference front() const { return *begin(); }
179  const_reference back() const { return *std::prev( end() ); }
180  // ========================================================================
182  Range_ slice( long index1, long index2 ) const
183  {
184  // trivial cases
185  if ( empty() || index1 == index2 ) {
186  return Range_();
187  } // RETURN
188  // adjust indices
189  if ( index1 < 0 ) {
190  index1 += size();
191  }
192  if ( index2 < 0 ) {
193  index2 += size();
194  }
195  // check
196  if ( index1 < 0 ) {
197  return Range_();
198  } // RETURN
199  if ( index2 < index1 ) {
200  return Range_();
201  } // RETURN
202 
203  if ( index1 > (long)size() ) {
204  return Range_();
205  } // RETURN
206  if ( index2 > (long)size() ) {
207  index2 = size();
208  }
209 
210  // construct the slice
211  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
212  }
213  // ========================================================================
218  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
223  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
229  inline const_reference at( const long index ) const
230  {
231  if ( index < 0 || index >= (long)size() ) {
232  Gaudi::details::rangeException( index, size() );
233  }
234  return ( *this )( index );
235  }
236  // ========================================================================
237  public:
238  // ========================================================================
240  template <class C, class I>
241  bool operator<( const Range_<C, I>& right ) const
242  {
243  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
244  }
246  template <class ANOTHERCONTAINER>
247  bool operator<( const ANOTHERCONTAINER& right ) const
248  {
249  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
250  }
251  // ========================================================================
252  public:
253  // ========================================================================
255  bool operator==( const Range_& right ) const
256  {
257  if ( &right == this ) {
258  return true;
259  } // RETURN
260  return right.size() == size() && std::equal( begin(), end(), right.begin() );
261  }
263  template <class CNT, class IT>
264  bool operator==( const Range_<CNT, IT>& right ) const
265  {
266  return right.size() == size() && std::equal( begin(), end(), right.begin() );
267  }
269  template <class ANOTHERCONTAINER>
270  bool operator==( const ANOTHERCONTAINER& right ) const
271  {
272  return right.size() == size() && std::equal( begin(), end(), right.begin() );
273  }
274  // ========================================================================
275  public:
276  // ========================================================================
278  bool operator!() const { return empty(); }
280  explicit operator bool() const { return !empty(); }
281  // ========================================================================
282  public:
283  // ========================================================================
285  operator const Base&() const { return base(); }
287  inline const Base& base() const { return m_base; }
288  // ========================================================================
289  private:
290  // ========================================================================
291  // the base itself
292  Base m_base;
293  // ========================================================================
294  }; // end of class Range_
295  // ==========================================================================
323  template <class CONTAINER>
324  inline Range_<CONTAINER> range( const CONTAINER& cnt )
325  {
326  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
327  }
328  // ==========================================================================
329 } // end of namespace Gaudi
330 // ============================================================================
331 // The END
332 // ============================================================================
333 #endif // GAUDI_RANGE_H
334 // ============================================================================
details::_container< CONTAINER, details::_has_typename_container_< CONTAINER >::value >::Container Container
Definition: Range.h:71
bool empty() const
empty sequence ?
Definition: Range.h:161
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:112
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:133
Base m_base
the base itself
Definition: Range.h:292
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:30
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:169
iter_traits::value_type value_type
Definition: Range.h:128
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:132
helper structure to get container type
Definition: Range.h:69
std::iterator_traits< iterator > iter_traits
Definition: Range.h:124
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:182
helper class to simplify the dealing with ranges in Python
Definition: Range.h:82
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:173
iter_traits::reference reference
Definition: Range.h:129
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:264
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:177
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
bool operator!() const
empty sequence?
Definition: Range.h:278
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:144
T prev(T...args)
iter_traits::reference const_reference
Definition: Range.h:130
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:223
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:121
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:117
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:229
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:148
ITERATOR iterator
Definition: Range.h:119
T lexicographical_compare(T...args)
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:255
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
ITERATOR const_iterator
Definition: Range.h:120
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:167
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:287
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:171
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:270
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:247
Range_(iterator ibegin)
Definition: Range.h:156
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:108
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:163
CONTAINER::const_iterator Iterator
Definition: Range.h:72
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:175
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:218
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:179
#define GAUDI_API
Definition: Kernel.h:110
T equal(T...args)
Range_(const Container &cont)
constructor from the container
Definition: Range.h:152
Helper functions to set/get the application return code.
Definition: __init__.py:1
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:165