The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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>
15#include <algorithm>
16#include <utility>
17
33namespace 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;
97 typedef typename iter_traits::reference reference;
98 typedef typename iter_traits::reference const_reference;
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; }
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 }
190
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 }
201
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
#define GAUDI_API
Definition Kernel.h:49
Useful class for representation of "sequence" of the objects through the range of valid iterators.
Definition Range.h:81
typename T::const_iterator const_iterator
Definition Range.h:88
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition Range.h:203
typename Gaudi::details::container< T >::Container Container
Definition Range.h:87
Range_(InputIterator first, InputIterator last)
Constructor.
Definition Range.h:110
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences)
Definition Range.h:180
bool operator!() const
empty sequence?
Definition Range.h:208
const_iterator iterator
Definition Range.h:89
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition Range.h:148
iter_traits::value_type value_type
Definition Range.h:96
bool empty() const
empty sequence ?
Definition Range.h:127
Range_(iterator ibegin)
Definition Range.h:122
~Range_()=default
std::reverse_iterator< iterator > const_reverse_iterator
Definition Range.h:101
iterator begin() const
Definition Range.h:131
Range_()=default
default constructor
bool operator==(const Range_ &right) const
equality with another range
Definition Range.h:197
std::reverse_iterator< iterator > reverse_iterator
Definition Range.h:100
Range_(const Container &cont)
constructor from the container
Definition Range.h:118
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition Range.h:145
iterator end() const
Definition Range.h:133
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition Range.h:192
iter_traits::reference reference
Definition Range.h:97
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
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition Range.h:187
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition Range.h:141
std::iterator_traits< iterator > iter_traits
Definition Range.h:93
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
Range_(const Base &base)
constructor from the pair of iterators
Definition Range.h:114
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition Range.h:143
std::pair< iterator, iterator > Base
Definition Range.h:90
iterator cend() const
access to end of the sequence (const version)
Definition Range.h:137
const Base & base() const
Definition Range.h:215
iterator cbegin() const
access to begin of the sequence (const version )
Definition Range.h:135
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition Range.h:139
size_t size() const
size of the sequence (number of elements)
Definition Range.h:129
iter_traits::reference const_reference
Definition Range.h:98
typename details::detector< Default, void, Op, Args... >::type detected_or_t
Definition detected.h:49
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
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition NamedRange.h:111
helper class to simplify the dealing with ranges in Python
Definition Range.h:56
helper structure to get container type
Definition Range.h:44
typename T::Container _has_container_t
Definition Range.h:46
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition Range.h:47