Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 <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  namespace details {
36  // ========================================================================
42  GAUDI_API void rangeException( const long index, const size_t size );
43  // ========================================================================
45  template <class CONTAINER>
46  struct container {
47  template <typename T>
48  using _has_container_t = typename T::Container;
49  using Container = Gaudi::cpp17::detected_or_t<CONTAINER, _has_container_t, CONTAINER>;
50  using Iterator = typename CONTAINER::const_iterator;
51  };
52  // =========================================================================
53  } // namespace details
54  // ==========================================================================
60  struct RangeBase_ {};
61  // ==========================================================================
85  class Range_ : public RangeBase_ {
86  public:
87  // ========================================================================
89  // ========================================================================
90  public:
91  // ========================================================================
94  //
95  typedef ITERATOR iterator;
96  typedef ITERATOR const_iterator;
97  //
98  private:
99  //
101  //
102  public:
103  //
104  typedef typename iter_traits::value_type value_type;
105  typedef typename iter_traits::reference reference;
106  typedef typename iter_traits::reference const_reference;
107  //
111  // ========================================================================
112  public:
113  // ========================================================================
115  Range_() = default;
120  template <typename InputIterator>
121  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
125  Range_( const Base& base ) : m_base( base ) {}
129  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
130  /* constructor of empty range/sequence
131  * @param ibegin iterator to begin of empty sequence
132  */
133  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
135  ~Range_() = default;
136  // ========================================================================
138  bool empty() const { return m_base.second == m_base.first; }
140  size_t size() const { return std::distance( m_base.first, m_base.second ); }
142  iterator begin() const { return m_base.first; }
144  iterator end() const { return m_base.second; }
146  iterator cbegin() const { return m_base.first; }
148  iterator cend() const { return m_base.second; }
150  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
152  reverse_iterator rend() const { return reverse_iterator( begin() ); }
154  const_reference front() const { return *begin(); }
156  const_reference back() const { return *std::prev( end() ); }
157  // ========================================================================
159  Range_ slice( long index1, long index2 ) const {
160  // trivial cases
161  if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
162  // adjust indices
163  if ( index1 < 0 ) { index1 += size(); }
164  if ( index2 < 0 ) { index2 += size(); }
165  // check
166  if ( index1 < 0 ) { return Range_(); } // RETURN
167  if ( index2 < index1 ) { return Range_(); } // RETURN
168 
169  if ( index1 > (long)size() ) { return Range_(); } // RETURN
170  if ( index2 > (long)size() ) { index2 = size(); }
171 
172  // construct the slice
173  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
174  }
175  // ========================================================================
180  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
185  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
191  inline const_reference at( const long index ) const {
192  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
193  return ( *this )( index );
194  }
195  // ========================================================================
196  public:
197  // ========================================================================
199  template <class C, class I>
200  bool operator<( const Range_<C, I>& right ) const {
201  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
202  }
204  template <class ANOTHERCONTAINER>
205  bool operator<( const ANOTHERCONTAINER& right ) const {
206  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
207  }
208  // ========================================================================
209  public:
210  // ========================================================================
212  bool operator==( const Range_& right ) const {
213  if ( &right == this ) { return true; } // RETURN
214  return right.size() == size() && std::equal( begin(), end(), right.begin() );
215  }
217  template <class CNT, class IT>
218  bool operator==( const Range_<CNT, IT>& right ) const {
219  return right.size() == size() && std::equal( begin(), end(), right.begin() );
220  }
222  template <class ANOTHERCONTAINER>
223  bool operator==( const ANOTHERCONTAINER& right ) const {
224  return right.size() == size() && std::equal( begin(), end(), right.begin() );
225  }
226  // ========================================================================
227  public:
228  // ========================================================================
230  bool operator!() const { return empty(); }
232  explicit operator bool() const { return !empty(); }
233  // ========================================================================
234  public:
235  // ========================================================================
237  operator const Base&() const { return base(); }
239  inline const Base& base() const { return m_base; }
240  // ========================================================================
241  private:
242  // ========================================================================
243  // the base itself
244  Base m_base;
245  // ========================================================================
246  }; // end of class Range_
247  // ==========================================================================
275  template <class CONTAINER>
276  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
277  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
278  }
279  // ==========================================================================
280 } // end of namespace Gaudi
281 // ============================================================================
282 // The END
283 // ============================================================================
284 #endif // GAUDI_RANGE_H
bool empty() const
empty sequence ?
Definition: Range.h:138
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:88
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:109
Base m_base
the base itself
Definition: Range.h:244
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:146
typename T::Container _has_container_t
Definition: Range.h:48
iter_traits::value_type value_type
Definition: Range.h:104
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:108
helper structure to get container type
Definition: Range.h:46
std::iterator_traits< iterator > iter_traits
Definition: Range.h:100
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:159
helper class to simplify the dealing with ranges in Python
Definition: Range.h:60
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:150
iter_traits::reference reference
Definition: Range.h:105
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:218
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:154
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
bool operator!() const
empty sequence?
Definition: Range.h:230
T prev(T...args)
iter_traits::reference const_reference
Definition: Range.h:106
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:185
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:118
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:121
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:93
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:191
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:125
ITERATOR iterator
Definition: Range.h:95
T lexicographical_compare(T...args)
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:212
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
ITERATOR const_iterator
Definition: Range.h:96
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:144
typename CONTAINER::const_iterator Iterator
Definition: Range.h:50
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:239
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:148
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:223
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:205
Range_(iterator ibegin)
Definition: Range.h:133
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:85
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:140
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:152
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:180
AttribStringParser::Iterator begin(const AttribStringParser &parser)
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:156
#define GAUDI_API
Definition: Kernel.h:71
T equal(T...args)
Range_(const Container &cont)
constructor from the container
Definition: Range.h:129
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:142
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:49