The Gaudi Framework  master (f5098d57)
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  };
60  // =========================================================================
61  } // namespace details
62  // ==========================================================================
68  struct RangeBase_ {};
69  // ==========================================================================
92  template <class CONTAINER, class ITERATOR = typename CONTAINER::const_iterator>
93  class Range_ : public RangeBase_ {
94  // FIXME: prepare for removal of ITERATOR argument: check whether the default is _always_ used
95  static_assert( std::is_same_v<ITERATOR, typename CONTAINER::const_iterator> );
96 
97  public:
98  // ========================================================================
101  using const_iterator = typename CONTAINER::const_iterator;
103  using Base = std::pair<iterator, iterator>;
104  //
105  private:
106  //
107  typedef typename std::iterator_traits<iterator> iter_traits;
108  //
109  public:
110  //
111  typedef typename iter_traits::value_type value_type;
114  //
115  typedef std::reverse_iterator<iterator> reverse_iterator;
116  typedef std::reverse_iterator<iterator> const_reverse_iterator;
118  // ========================================================================
119  public:
120  // ========================================================================
122  Range_() = default;
127  template <typename InputIterator>
128  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
132  Range_( const Base& base ) : m_base( base ) {}
136  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
137  /* constructor of empty range/sequence
138  * @param ibegin iterator to begin of empty sequence
139  */
140  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
142  ~Range_() = default;
143  // ========================================================================
145  bool empty() const { return m_base.second == m_base.first; }
147  size_t size() const { return std::distance( m_base.first, m_base.second ); }
149  iterator begin() const { return m_base.first; }
151  iterator end() const { return m_base.second; }
153  iterator cbegin() const { return m_base.first; }
155  iterator cend() const { return m_base.second; }
157  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
159  reverse_iterator rend() const { return reverse_iterator( begin() ); }
161  const_reference front() const { return *begin(); }
163  const_reference back() const { return *std::prev( end() ); }
164  // ========================================================================
166  Range_ slice( long index1, long index2 ) const {
167  // trivial cases
168  if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
169  // adjust indices
170  if ( index1 < 0 ) { index1 += size(); }
171  if ( index2 < 0 ) { index2 += size(); }
172  // check
173  if ( index1 < 0 ) { return Range_(); } // RETURN
174  if ( index2 < index1 ) { return Range_(); } // RETURN
175 
176  if ( index1 > (long)size() ) { return Range_(); } // RETURN
177  if ( index2 > (long)size() ) { index2 = size(); }
178 
179  // construct the slice
180  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
181  }
182  // ========================================================================
187  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
192  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
198  inline const_reference at( const long index ) const {
199  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
200  return ( *this )( index );
201  }
202  // ========================================================================
203  public:
204  // ========================================================================
206  template <class C, class I>
207  bool operator<( const Range_<C, I>& right ) const {
208  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
209  }
211  template <class ANOTHERCONTAINER>
212  bool operator<( const ANOTHERCONTAINER& right ) const {
213  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
214  }
215  // ========================================================================
216  public:
217  // ========================================================================
219  bool operator==( const Range_& right ) const {
220  if ( &right == this ) { return true; } // RETURN
221  return right.size() == size() && std::equal( begin(), end(), right.begin() );
222  }
224  template <class ANOTHERCONTAINER>
225  bool operator==( const ANOTHERCONTAINER& right ) const {
226  return right.size() == size() && std::equal( begin(), end(), right.begin() );
227  }
228  // ========================================================================
229  public:
230  // ========================================================================
232  bool operator!() const { return empty(); }
234  explicit operator bool() const { return !empty(); }
235  // ========================================================================
236  public:
237  // ========================================================================
239  operator const Base&() const { return base(); }
241  inline const Base& base() const { return m_base; }
242  // ========================================================================
243  private:
244  // ========================================================================
245  // the base itself
247  // ========================================================================
248  }; // end of class Range_
249  // ==========================================================================
277  template <class CONTAINER>
278  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
279  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
280  }
281  // ==========================================================================
282 } // end of namespace Gaudi
283 // ============================================================================
284 // The END
285 // ============================================================================
286 #endif // GAUDI_RANGE_H
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::iterator
const_iterator iterator
Definition: Range.h:102
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:136
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::Container
typename Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:100
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:232
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:187
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:113
Gaudi::details::container
helper structure to get container type
Definition: Range.h:55
Gaudi::RangeBase_
Definition: Range.h:68
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:225
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:145
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:241
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:159
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:198
Gaudi::details::container::_has_container_t
typename T::Container _has_container_t
Definition: Range.h:57
Gaudi::Range_::Range_
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:128
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::const_iterator
typename CONTAINER::const_iterator const_iterator
Definition: Range.h:101
Gaudi::Range_::~Range_
~Range_()=default
destructor
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:157
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:192
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::Base
std::pair< iterator, iterator > Base
Definition: Range.h:103
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:149
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:155
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:147
details
Definition: AnyDataWrapper.h:19
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:140
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:116
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:246
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:112
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:207
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:93
Gaudi::Range_::Range_
Range_()=default
internal types
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:115
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:163
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:166
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:111
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:132
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:107
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:219
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:212
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:153
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:151
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:161
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