The Gaudi Framework  v30r3 (a5ef0a68)
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 #include "GaudiKernel/detected.h"
16 // ============================================================================
32 // ============================================================================
33 namespace Gaudi
34 {
35  // ==========================================================================
36  namespace details
37  {
38  // ========================================================================
44  GAUDI_API void rangeException( const long index, const size_t size );
45  // ========================================================================
47  template <class CONTAINER>
48  struct container {
49  template <typename T>
50  using _has_container_t = typename T::Container;
51  using Container = Gaudi::cpp17::detected_or_t<CONTAINER, _has_container_t, CONTAINER>;
52  using Iterator = typename CONTAINER::const_iterator;
53  };
54  // =========================================================================
55  } // the end of namespace Gaudi::details
56  // ==========================================================================
62  struct RangeBase_ {
63  };
64  // ==========================================================================
88  class Range_ : public RangeBase_
89  {
90  public:
91  // ========================================================================
93  // ========================================================================
94  public:
95  // ========================================================================
98  //
99  typedef ITERATOR iterator;
100  typedef ITERATOR const_iterator;
101  //
102  private:
103  //
105  //
106  public:
107  //
108  typedef typename iter_traits::value_type value_type;
109  typedef typename iter_traits::reference reference;
110  typedef typename iter_traits::reference const_reference;
111  //
115  // ========================================================================
116  public:
117  // ========================================================================
119  Range_() = default;
124  template <typename InputIterator>
125  Range_( InputIterator first, InputIterator last ) : m_base( first, last )
126  {
127  }
131  Range_( const Base& base ) : m_base( base ) {}
135  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
136  /* constructor of empty range/sequence
137  * @param ibegin iterator to begin of empty sequence
138  */
139  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
141  ~Range_() = default;
142  // ========================================================================
144  bool empty() const { return m_base.second == m_base.first; }
146  size_t size() const { return std::distance( m_base.first, m_base.second ); }
148  iterator begin() const { return m_base.first; }
150  iterator end() const { return m_base.second; }
152  iterator cbegin() const { return m_base.first; }
154  iterator cend() const { return m_base.second; }
156  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
158  reverse_iterator rend() const { return reverse_iterator( begin() ); }
160  const_reference front() const { return *begin(); }
162  const_reference back() const { return *std::prev( end() ); }
163  // ========================================================================
165  Range_ slice( long index1, long index2 ) const
166  {
167  // trivial cases
168  if ( empty() || index1 == index2 ) {
169  return Range_();
170  } // RETURN
171  // adjust indices
172  if ( index1 < 0 ) {
173  index1 += size();
174  }
175  if ( index2 < 0 ) {
176  index2 += size();
177  }
178  // check
179  if ( index1 < 0 ) {
180  return Range_();
181  } // RETURN
182  if ( index2 < index1 ) {
183  return Range_();
184  } // RETURN
185 
186  if ( index1 > (long)size() ) {
187  return Range_();
188  } // RETURN
189  if ( index2 > (long)size() ) {
190  index2 = size();
191  }
192 
193  // construct the slice
194  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
195  }
196  // ========================================================================
201  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
206  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
212  inline const_reference at( const long index ) const
213  {
214  if ( index < 0 || index >= (long)size() ) {
216  }
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  {
226  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
227  }
229  template <class ANOTHERCONTAINER>
230  bool operator<( const ANOTHERCONTAINER& right ) const
231  {
232  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
233  }
234  // ========================================================================
235  public:
236  // ========================================================================
238  bool operator==( const Range_& right ) const
239  {
240  if ( &right == this ) {
241  return true;
242  } // RETURN
243  return right.size() == size() && std::equal( begin(), end(), right.begin() );
244  }
246  template <class CNT, class IT>
247  bool operator==( const Range_<CNT, IT>& right ) const
248  {
249  return right.size() == size() && std::equal( begin(), end(), right.begin() );
250  }
252  template <class ANOTHERCONTAINER>
253  bool operator==( const ANOTHERCONTAINER& right ) const
254  {
255  return right.size() == size() && std::equal( begin(), end(), right.begin() );
256  }
257  // ========================================================================
258  public:
259  // ========================================================================
261  bool operator!() const { return empty(); }
263  explicit operator bool() const { return !empty(); }
264  // ========================================================================
265  public:
266  // ========================================================================
268  operator const Base&() const { return base(); }
270  inline const Base& base() const { return m_base; }
271  // ========================================================================
272  private:
273  // ========================================================================
274  // the base itself
275  Base m_base;
276  // ========================================================================
277  }; // end of class Range_
278  // ==========================================================================
306  template <class CONTAINER>
307  inline Range_<CONTAINER> range( const CONTAINER& cnt )
308  {
309  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
310  }
311  // ==========================================================================
312 } // end of namespace Gaudi
313 // ============================================================================
314 // The END
315 // ============================================================================
316 #endif // GAUDI_RANGE_H
317 // ============================================================================
bool empty() const
empty sequence ?
Definition: Range.h:144
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:92
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:113
Base m_base
the base itself
Definition: Range.h:275
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:152
typename T::Container _has_container_t
Definition: Range.h:50
iter_traits::value_type value_type
Definition: Range.h:108
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:112
helper structure to get container type
Definition: Range.h:48
std::iterator_traits< iterator > iter_traits
Definition: Range.h:104
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:165
helper class to simplify the dealing with ranges in Python
Definition: Range.h:62
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:156
iter_traits::reference reference
Definition: Range.h:109
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:247
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:160
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
bool operator!() const
empty sequence?
Definition: Range.h:261
T prev(T...args)
iter_traits::reference const_reference
Definition: Range.h:110
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:206
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:121
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:125
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:97
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:212
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:131
ITERATOR iterator
Definition: Range.h:99
T lexicographical_compare(T...args)
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:238
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
ITERATOR const_iterator
Definition: Range.h:100
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:150
typename CONTAINER::const_iterator Iterator
Definition: Range.h:52
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:270
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:154
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:253
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:230
Range_(iterator ibegin)
Definition: Range.h:139
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:88
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:146
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:158
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:201
AttribStringParser::Iterator begin(const AttribStringParser &parser)
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:162
#define GAUDI_API
Definition: Kernel.h:104
T equal(T...args)
Range_(const Container &cont)
constructor from the container
Definition: Range.h:135
Helper functions to set/get the application return code.
Definition: __init__.py:1
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:148
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:51