Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
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 #pragma once
12 
13 #include <GaudiKernel/Kernel.h>
14 #include <GaudiKernel/detected.h>
15 #include <algorithm>
16 #include <utility>
17 
33 namespace Gaudi {
34  namespace details {
40  GAUDI_API void rangeException( const long index, const size_t size );
41 
43  template <class CONTAINER>
44  struct container {
45  template <typename T>
46  using _has_container_t = typename T::Container;
48  };
49  } // namespace details
50 
56  struct RangeBase_ {};
57 
80  template <class CONTAINER, class ITERATOR = typename CONTAINER::const_iterator>
81  class Range_ : public RangeBase_ {
82  // FIXME: prepare for removal of ITERATOR argument: check whether the default is _always_ used
83  static_assert( std::is_same_v<ITERATOR, typename CONTAINER::const_iterator> );
84 
85  public:
88  using const_iterator = typename CONTAINER::const_iterator;
90  using Base = std::pair<iterator, iterator>;
91 
92  private:
93  typedef typename std::iterator_traits<iterator> iter_traits;
94 
95  public:
96  typedef typename iter_traits::value_type value_type;
99 
100  typedef std::reverse_iterator<iterator> reverse_iterator;
101  typedef std::reverse_iterator<iterator> const_reverse_iterator;
102 
104  Range_() = default;
109  template <typename InputIterator>
110  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
114  Range_( const Base& base ) : m_base( base ) {}
118  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
119  /* constructor of empty range/sequence
120  * @param ibegin iterator to begin of empty sequence
121  */
122  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
123 
124  ~Range_() = default;
125 
127  bool empty() const { return m_base.second == m_base.first; }
129  size_t size() const { return std::distance( m_base.first, m_base.second ); }
131  iterator begin() const { return m_base.first; }
133  iterator end() const { return m_base.second; }
135  iterator cbegin() const { return m_base.first; }
137  iterator cend() const { return m_base.second; }
139  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
141  reverse_iterator rend() const { return reverse_iterator( begin() ); }
143  const_reference front() const { return *begin(); }
145  const_reference back() const { return *std::prev( end() ); }
146 
148  Range_ slice( long index1, long index2 ) const {
149  // trivial cases
150  if ( empty() || index1 == index2 ) { return Range_(); }
151  // adjust indices
152  if ( index1 < 0 ) { index1 += size(); }
153  if ( index2 < 0 ) { index2 += size(); }
154  // check
155  if ( index1 < 0 ) { return Range_(); }
156  if ( index2 < index1 ) { return Range_(); }
157 
158  if ( index1 > (long)size() ) { return Range_(); }
159  if ( index2 > (long)size() ) { index2 = size(); }
160 
161  // construct the slice
162  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) );
163  }
164 
169  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
174  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
180  inline const_reference at( const long index ) const {
181  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
182  return ( *this )( index );
183  }
184 
186  template <class C, class I>
187  bool operator<( const Range_<C, I>& right ) const {
188  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
189  }
191  template <class ANOTHERCONTAINER>
192  bool operator<( const ANOTHERCONTAINER& right ) const {
193  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
194  }
195 
197  bool operator==( const Range_& right ) const {
198  if ( &right == this ) { return true; }
199  return right.size() == size() && std::equal( begin(), end(), right.begin() );
200  }
202  template <class ANOTHERCONTAINER>
203  bool operator==( const ANOTHERCONTAINER& right ) const {
204  return right.size() == size() && std::equal( begin(), end(), right.begin() );
205  }
206 
208  bool operator!() const { return empty(); }
210  explicit operator bool() const { return !empty(); }
211 
213  operator const Base&() const { return base(); }
215  inline const Base& base() const { return m_base; }
216 
217  private:
219  };
220 
248  template <class CONTAINER>
249  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
250  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
251  }
252 } // namespace Gaudi
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::iterator
const_iterator iterator
Definition: Range.h:89
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:118
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::Container
typename Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:87
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:208
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:169
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:98
Gaudi::details::container
helper structure to get container type
Definition: Range.h:44
Gaudi::RangeBase_
Definition: Range.h:56
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:203
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:127
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:215
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:141
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:30
Gaudi::details::container::Container
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:47
Gaudi::Range_::at
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences)
Definition: Range.h:180
Gaudi::details::container::_has_container_t
typename T::Container _has_container_t
Definition: Range.h:46
Gaudi::Range_::Range_
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:110
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::const_iterator
typename CONTAINER::const_iterator const_iterator
Definition: Range.h:88
Gaudi::Range_::~Range_
~Range_()=default
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:139
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:174
Gaudi::Range_< CONTAINER, typename CONTAINER::const_iterator >::Base
std::pair< iterator, iterator > Base
Definition: Range.h:90
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:131
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:111
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:137
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:129
details
Definition: AnyDataWrapper.h:19
Gaudi::cpp17::detected_or_t
typename details::detector< Default, void, Op, Args... >::type detected_or_t
Definition: detected.h:49
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:122
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:101
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:218
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:97
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:187
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:81
Gaudi::Range_::Range_
Range_()=default
default constructor
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:100
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:145
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:148
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:96
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:114
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:93
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:197
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:192
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:135
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:133
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:143
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