The Gaudi Framework  master (5a34980b)
Loading...
Searching...
No Matches
Range.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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>
81 class Range_ : public RangeBase_ {
82
83 public:
86 using const_iterator = typename CONTAINER::const_iterator;
88 using Base = std::pair<iterator, iterator>;
89
90 private:
91 typedef typename std::iterator_traits<iterator> iter_traits;
92
93 public:
94 typedef typename iter_traits::value_type value_type;
95 typedef typename iter_traits::reference reference;
96 typedef typename iter_traits::reference const_reference;
97
98 typedef std::reverse_iterator<iterator> reverse_iterator;
99 typedef std::reverse_iterator<iterator> const_reverse_iterator;
100
102 Range_() = default;
107 template <typename InputIterator>
108 Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
112 Range_( const Base& base ) : m_base( base ) {}
116 Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
117 /* constructor of empty range/sequence
118 * @param ibegin iterator to begin of empty sequence
119 */
120 Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
121
122 ~Range_() = default;
123
125 bool empty() const { return m_base.second == m_base.first; }
127 size_t size() const { return std::distance( m_base.first, m_base.second ); }
129 iterator begin() const { return m_base.first; }
131 iterator end() const { return m_base.second; }
133 iterator cbegin() const { return m_base.first; }
135 iterator cend() const { return m_base.second; }
141 const_reference front() const { return *begin(); }
143 const_reference back() const { return *std::prev( end() ); }
144
146 Range_ slice( long index1, long index2 ) const {
147 // trivial cases
148 if ( empty() || index1 == index2 ) { return Range_(); }
149 // adjust indices
150 if ( index1 < 0 ) { index1 += size(); }
151 if ( index2 < 0 ) { index2 += size(); }
152 // check
153 if ( index1 < 0 ) { return Range_(); }
154 if ( index2 < index1 ) { return Range_(); }
155
156 if ( index1 > (long)size() ) { return Range_(); }
157 if ( index2 > (long)size() ) { index2 = size(); }
158
159 // construct the slice
160 return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) );
161 }
162
167 inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
172 inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
178 inline const_reference at( const long index ) const {
179 if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
180 return ( *this )( index );
181 }
182
184 template <class C>
185 bool operator<( const Range_<C>& right ) const {
186 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
187 }
188
189 template <class ANOTHERCONTAINER>
190 bool operator<( const ANOTHERCONTAINER& right ) const {
191 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
192 }
193
195 bool operator==( const Range_& right ) const {
196 if ( &right == this ) { return true; }
197 return right.size() == size() && std::equal( begin(), end(), right.begin() );
198 }
199
200 template <class ANOTHERCONTAINER>
201 bool operator==( const ANOTHERCONTAINER& right ) const {
202 return right.size() == size() && std::equal( begin(), end(), right.begin() );
203 }
204
206 bool operator!() const { return empty(); }
208 explicit operator bool() const { return !empty(); }
209
211 operator const Base&() const { return base(); }
213 inline const Base& base() const { return m_base; }
214
215 private:
217 };
218
246 template <class CONTAINER>
247 inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
248 return Range_<CONTAINER>( cnt.begin(), cnt.end() );
249 }
250} // 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
std::reverse_iterator< iterator > const_reverse_iterator
Definition Range.h:99
iterator cbegin() const
access to begin of the sequence (const version )
Definition Range.h:133
Range_(const Base &base)
constructor from the pair of iterators
Definition Range.h:112
std::iterator_traits< iterator > iter_traits
Definition Range.h:91
Range_(const Container &cont)
constructor from the container
Definition Range.h:116
iter_traits::reference const_reference
Definition Range.h:96
Range_()=default
default constructor
bool operator==(const Range_ &right) const
equality with another range
Definition Range.h:195
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:167
typename T::const_iterator const_iterator
Definition Range.h:86
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition Range.h:190
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition Range.h:143
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition Range.h:146
size_t size() const
size of the sequence (number of elements)
Definition Range.h:127
typename Gaudi::details::container< T >::Container Container
Definition Range.h:85
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition Range.h:139
const Base & base() const
Definition Range.h:213
iter_traits::reference reference
Definition Range.h:95
Range_(InputIterator first, InputIterator last)
Constructor.
Definition Range.h:108
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition Range.h:201
iterator begin() const
Definition Range.h:129
iterator end() const
Definition Range.h:131
iter_traits::value_type value_type
Definition Range.h:94
bool empty() const
empty sequence ?
Definition Range.h:125
bool operator!() const
empty sequence?
Definition Range.h:206
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences).
Definition Range.h:178
const_iterator iterator
Definition Range.h:87
const_reference operator[](const long index) const
non-checked access to the elements by index (valid only for non-empty sequences)
Definition Range.h:172
Range_(iterator ibegin)
Definition Range.h:120
std::reverse_iterator< iterator > reverse_iterator
Definition Range.h:98
std::pair< iterator, iterator > Base
Definition Range.h:88
bool operator<(const Range_< C > &right) const
compare with another range
Definition Range.h:185
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition Range.h:137
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition Range.h:141
iterator cend() const
access to end of the sequence (const version)
Definition Range.h:135
~Range_()=default
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