The Gaudi Framework  master (37c0b60a)
Range.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDI_RANGE_H
12 #define GAUDI_RANGE_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <algorithm>
19 #include <utility>
20 // ============================================================================
21 // GaudiKernel
22 // ============================================================================
23 #include <GaudiKernel/Kernel.h>
24 #include <GaudiKernel/detected.h>
25 // ============================================================================
41 // ============================================================================
42 namespace Gaudi {
43  // ==========================================================================
44  namespace details {
45  // ========================================================================
51  GAUDI_API void rangeException( const long index, const size_t size );
52  // ========================================================================
54  template <class CONTAINER>
55  struct container {
56  template <typename T>
57  using _has_container_t = typename T::Container;
59  using Iterator = typename CONTAINER::const_iterator;
60  };
61  // =========================================================================
62  } // namespace details
63  // ==========================================================================
69  struct RangeBase_ {};
70  // ==========================================================================
94  class Range_ : public RangeBase_ {
95  public:
96  // ========================================================================
98  // ========================================================================
99  public:
100  // ========================================================================
103  //
104  typedef ITERATOR iterator;
105  typedef ITERATOR const_iterator;
106  //
107  private:
108  //
110  //
111  public:
112  //
113  typedef typename iter_traits::value_type value_type;
116  //
120  // ========================================================================
121  public:
122  // ========================================================================
124  Range_() = default;
129  template <typename InputIterator>
130  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
134  Range_( const Base& base ) : m_base( base ) {}
138  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
139  /* constructor of empty range/sequence
140  * @param ibegin iterator to begin of empty sequence
141  */
142  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
144  ~Range_() = default;
145  // ========================================================================
147  bool empty() const { return m_base.second == m_base.first; }
149  size_t size() const { return std::distance( m_base.first, m_base.second ); }
151  iterator begin() const { return m_base.first; }
153  iterator end() const { return m_base.second; }
155  iterator cbegin() const { return m_base.first; }
157  iterator cend() const { return m_base.second; }
159  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
161  reverse_iterator rend() const { return reverse_iterator( begin() ); }
163  const_reference front() const { return *begin(); }
165  const_reference back() const { return *std::prev( end() ); }
166  // ========================================================================
168  Range_ slice( long index1, long index2 ) const {
169  // trivial cases
170  if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
171  // adjust indices
172  if ( index1 < 0 ) { index1 += size(); }
173  if ( index2 < 0 ) { index2 += size(); }
174  // check
175  if ( index1 < 0 ) { return Range_(); } // RETURN
176  if ( index2 < index1 ) { return Range_(); } // RETURN
177 
178  if ( index1 > (long)size() ) { return Range_(); } // RETURN
179  if ( index2 > (long)size() ) { index2 = size(); }
180 
181  // construct the slice
182  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
183  }
184  // ========================================================================
189  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
194  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
200  inline const_reference at( const long index ) const {
201  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
202  return ( *this )( index );
203  }
204  // ========================================================================
205  public:
206  // ========================================================================
208  template <class C, class I>
209  bool operator<( const Range_<C, I>& right ) const {
210  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
211  }
213  template <class ANOTHERCONTAINER>
214  bool operator<( const ANOTHERCONTAINER& right ) const {
215  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
216  }
217  // ========================================================================
218  public:
219  // ========================================================================
221  bool operator==( const Range_& right ) const {
222  if ( &right == this ) { return true; } // RETURN
223  return right.size() == size() && std::equal( begin(), end(), right.begin() );
224  }
226  template <class ANOTHERCONTAINER>
227  bool operator==( const ANOTHERCONTAINER& right ) const {
228  return right.size() == size() && std::equal( begin(), end(), right.begin() );
229  }
230  // ========================================================================
231  public:
232  // ========================================================================
234  bool operator!() const { return empty(); }
236  explicit operator bool() const { return !empty(); }
237  // ========================================================================
238  public:
239  // ========================================================================
241  operator const Base&() const { return base(); }
243  inline const Base& base() const { return m_base; }
244  // ========================================================================
245  private:
246  // ========================================================================
247  // the base itself
249  // ========================================================================
250  }; // end of class Range_
251  // ==========================================================================
279  template <class CONTAINER>
280  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
281  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
282  }
283  // ==========================================================================
284 } // end of namespace Gaudi
285 // ============================================================================
286 // The END
287 // ============================================================================
288 #endif // GAUDI_RANGE_H
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:138
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:234
Gaudi::Range_::operator()
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:189
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:102
std::equal
T equal(T... args)
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
Gaudi::Range_::const_reference
iter_traits::reference const_reference
Definition: Range.h:115
Gaudi::details::container
helper structure to get container type
Definition: Range.h:55
Gaudi::RangeBase_
Definition: Range.h:69
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:227
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:147
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:243
std::pair
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:161
Gaudi::details::rangeException
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:40
Gaudi::details::container::Container
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:58
Gaudi::Range_::at
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences)
Definition: Range.h:200
Gaudi::details::container::_has_container_t
typename T::Container _has_container_t
Definition: Range.h:57
std::distance
T distance(T... args)
Gaudi::Range_::Range_
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:130
Gaudi::Range_::~Range_
~Range_()=default
destructor
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:159
Gaudi::Range_::operator[]
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition: Range.h:194
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:151
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:128
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:157
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:149
details
Definition: AnyDataWrapper.h:19
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:97
Gaudi::cpp17::detected_or_t
typename details::detector< Default, void, Op, Args... >::type detected_or_t
Definition: detected.h:50
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:142
std::iterator_traits
std::lexicographical_compare
T lexicographical_compare(T... args)
Gaudi::details::container::Iterator
typename CONTAINER::const_iterator Iterator
Definition: Range.h:59
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:118
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:248
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:114
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:209
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Range_
Definition: Range.h:94
Gaudi::Range_::Range_
Range_()=default
internal types
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:117
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:165
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:168
Kernel.h
detected.h
fixtures.reference
Generator[dict, None, None] reference(request, Optional[Path] reference_path)
Definition: fixtures.py:211
Gaudi::Range_::value_type
iter_traits::value_type value_type
Definition: Range.h:113
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:134
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:109
std::reverse_iterator
std::prev
T prev(T... args)
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:104
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:221
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:214
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:155
Gaudi::Range_::const_iterator
ITERATOR const_iterator
Definition: Range.h:105
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:153
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:163
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28
std::next
T next(T... args)