The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Range.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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  // FIXME: prepare for removal of ITERATOR argument: check whether the default is _always_ used
96  static_assert( std::is_same_v<ITERATOR, typename Gaudi::details::container<CONTAINER>::Iterator> );
97 
98  public:
99  // ========================================================================
101  // ========================================================================
102  public:
103  // ========================================================================
106  //
107  typedef ITERATOR iterator;
108  typedef ITERATOR const_iterator;
109  //
110  private:
111  //
113  //
114  public:
115  //
116  typedef typename iter_traits::value_type value_type;
119  //
123  // ========================================================================
124  public:
125  // ========================================================================
127  Range_() = default;
132  template <typename InputIterator>
133  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
137  Range_( const Base& base ) : m_base( base ) {}
141  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
142  /* constructor of empty range/sequence
143  * @param ibegin iterator to begin of empty sequence
144  */
145  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
147  ~Range_() = default;
148  // ========================================================================
150  bool empty() const { return m_base.second == m_base.first; }
152  size_t size() const { return std::distance( m_base.first, m_base.second ); }
154  iterator begin() const { return m_base.first; }
156  iterator end() const { return m_base.second; }
158  iterator cbegin() const { return m_base.first; }
160  iterator cend() const { return m_base.second; }
162  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
164  reverse_iterator rend() const { return reverse_iterator( begin() ); }
166  const_reference front() const { return *begin(); }
168  const_reference back() const { return *std::prev( end() ); }
169  // ========================================================================
171  Range_ slice( long index1, long index2 ) const {
172  // trivial cases
173  if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
174  // adjust indices
175  if ( index1 < 0 ) { index1 += size(); }
176  if ( index2 < 0 ) { index2 += size(); }
177  // check
178  if ( index1 < 0 ) { return Range_(); } // RETURN
179  if ( index2 < index1 ) { return Range_(); } // RETURN
180 
181  if ( index1 > (long)size() ) { return Range_(); } // RETURN
182  if ( index2 > (long)size() ) { index2 = size(); }
183 
184  // construct the slice
185  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
186  }
187  // ========================================================================
192  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
197  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
203  inline const_reference at( const long index ) const {
204  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
205  return ( *this )( index );
206  }
207  // ========================================================================
208  public:
209  // ========================================================================
211  template <class C, class I>
212  bool operator<( const Range_<C, I>& right ) const {
213  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
214  }
216  template <class ANOTHERCONTAINER>
217  bool operator<( const ANOTHERCONTAINER& right ) const {
218  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
219  }
220  // ========================================================================
221  public:
222  // ========================================================================
224  bool operator==( const Range_& right ) const {
225  if ( &right == this ) { return true; } // RETURN
226  return right.size() == size() && std::equal( begin(), end(), right.begin() );
227  }
229  template <class ANOTHERCONTAINER>
230  bool operator==( const ANOTHERCONTAINER& right ) const {
231  return right.size() == size() && std::equal( begin(), end(), right.begin() );
232  }
233  // ========================================================================
234  public:
235  // ========================================================================
237  bool operator!() const { return empty(); }
239  explicit operator bool() const { return !empty(); }
240  // ========================================================================
241  public:
242  // ========================================================================
244  operator const Base&() const { return base(); }
246  inline const Base& base() const { return m_base; }
247  // ========================================================================
248  private:
249  // ========================================================================
250  // the base itself
252  // ========================================================================
253  }; // end of class Range_
254  // ==========================================================================
282  template <class CONTAINER>
283  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
284  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
285  }
286  // ==========================================================================
287 } // end of namespace Gaudi
288 // ============================================================================
289 // The END
290 // ============================================================================
291 #endif // GAUDI_RANGE_H
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:141
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:237
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:192
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:105
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:118
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:230
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:150
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:246
std::pair
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:164
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:203
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:133
Gaudi::Range_::~Range_
~Range_()=default
destructor
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:162
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:197
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:154
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:160
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:152
details
Definition: AnyDataWrapper.h:19
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:96
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:145
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:121
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:251
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:117
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:212
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:120
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:168
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:171
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:116
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:137
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:112
std::reverse_iterator
std::prev
T prev(T... args)
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:107
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:224
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:217
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:158
Gaudi::Range_::const_iterator
ITERATOR const_iterator
Definition: Range.h:108
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:156
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:166
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)