| The Gaudi Framework v32r1 (f65d50dc)

Range.h
Go to the documentation of this file.
1 #ifndef GAUDI_RANGE_H
2 #define GAUDI_RANGE_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <algorithm>
9 #include <utility>
10 #include <vector>
11 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/detected.h"
16 // ============================================================================
32 // ============================================================================
33 namespace Gaudi {
34 // ==========================================================================
35 namespace details {
36 // ========================================================================
42 GAUDI_API void rangeException( const long index, const size_t size );
43 // ========================================================================
45 template <class CONTAINER>
46 struct container {
47 template <typename T>
48 using _has_container_t = typename T::Container;
49 using Container = Gaudi::cpp17::detected_or_t<CONTAINER, _has_container_t, CONTAINER>;
50 using Iterator = typename CONTAINER::const_iterator;
51 };
52 // =========================================================================
53 } // namespace details
54 // ==========================================================================
60 struct RangeBase_ {};
61 // ==========================================================================
84 template <class CONTAINER, class ITERATOR = typename Gaudi::details::container<CONTAINER>::Iterator>
85 class Range_ : public RangeBase_ {
86 public:
87 // ========================================================================
88 typedef std::pair<ITERATOR, ITERATOR> Base;
89 // ========================================================================
90 public:
91 // ========================================================================
93 typedef typename Gaudi::details::container<CONTAINER>::Container Container;
94 //
95 typedef ITERATOR iterator;
96 typedef ITERATOR const_iterator;
97 //
98 private:
99 //
100 typedef typename std::iterator_traits<iterator> iter_traits;
101 //
102 public:
103 //
104 typedef typename iter_traits::value_type value_type;
105 typedef typename iter_traits::reference reference;
106 typedef typename iter_traits::reference const_reference;
107 //
108 typedef std::reverse_iterator<iterator> reverse_iterator;
109 typedef std::reverse_iterator<iterator> const_reverse_iterator;
111 // ========================================================================
112 public:
113 // ========================================================================
115 Range_() = default;
120 template <typename InputIterator>
121 Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
125 Range_( const Base& base ) : m_base( base ) {}
129 Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
130 /* constructor of empty range/sequence
131 * @param ibegin iterator to begin of empty sequence
132 */
133 Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
135 ~Range_() = default;
136 // ========================================================================
138 bool empty() const { return m_base.second == m_base.first; }
140 size_t size() const { return std::distance( m_base.first, m_base.second ); }
142 iterator begin() const { return m_base.first; }
144 iterator end() const { return m_base.second; }
146 iterator cbegin() const { return m_base.first; }
148 iterator cend() const { return m_base.second; }
150 reverse_iterator rbegin() const { return reverse_iterator( end() ); }
152 reverse_iterator rend() const { return reverse_iterator( begin() ); }
154 const_reference front() const { return *begin(); }
156 const_reference back() const { return *std::prev( end() ); }
157 // ========================================================================
159 Range_ slice( long index1, long index2 ) const {
160 // trivial cases
161 if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
162 // adjust indices
163 if ( index1 < 0 ) { index1 += size(); }
164 if ( index2 < 0 ) { index2 += size(); }
165 // check
166 if ( index1 < 0 ) { return Range_(); } // RETURN
167 if ( index2 < index1 ) { return Range_(); } // RETURN
168
169 if ( index1 > (long)size() ) { return Range_(); } // RETURN
170 if ( index2 > (long)size() ) { index2 = size(); }
171
172 // construct the slice
173 return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
174 }
175 // ========================================================================
180 inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
185 inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
191 inline const_reference at( const long index ) const {
192 if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
193 return ( *this )( index );
194 }
195 // ========================================================================
196 public:
197 // ========================================================================
199 template <class C, class I>
200 bool operator<( const Range_<C, I>& right ) const {
201 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
202 }
204 template <class ANOTHERCONTAINER>
205 bool operator<( const ANOTHERCONTAINER& right ) const {
206 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
207 }
208 // ========================================================================
209 public:
210 // ========================================================================
212 bool operator==( const Range_& right ) const {
213 if ( &right == this ) { return true; } // RETURN
214 return right.size() == size() && std::equal( begin(), end(), right.begin() );
215 }
217 template <class CNT, class IT>
218 bool operator==( const Range_<CNT, IT>& right ) const {
219 return right.size() == size() && std::equal( begin(), end(), right.begin() );
220 }
222 template <class ANOTHERCONTAINER>
223 bool operator==( const ANOTHERCONTAINER& right ) const {
224 return right.size() == size() && std::equal( begin(), end(), right.begin() );
225 }
226 // ========================================================================
227 public:
228 // ========================================================================
230 bool operator!() const { return empty(); }
232 explicit operator bool() const { return !empty(); }
233 // ========================================================================
234 public:
235 // ========================================================================
237 operator const Base&() const { return base(); }
239 inline const Base& base() const { return m_base; }
240 // ========================================================================
241 private:
242 // ========================================================================
243 // the base itself
244 Base m_base;
245 // ========================================================================
246 }; // end of class Range_
247 // ==========================================================================
275 template <class CONTAINER>
276 inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
277 return Range_<CONTAINER>( cnt.begin(), cnt.end() );
278 }
279 // ==========================================================================
280 } // end of namespace Gaudi
281 // ============================================================================
282 // The END
283 // ============================================================================
284 #endif // GAUDI_RANGE_H
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:14
detected.h
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:148
Kernel.h
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:88
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:109
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:244
std::distance
T distance(T... args)
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::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:205
Gaudi::Range_::operator==
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:218
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:180
Gaudi::details::container::_has_container_t
typename T::Container _has_container_t
Definition: Range.h:48
Gaudi::Range_::value_type
iter_traits::value_type value_type
Definition: Range.h:104
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:108
Gaudi::details::container
helper structure to get container type
Definition: Range.h:46
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:100
Gaudi::RangeBase_
helper class to simplify the dealing with ranges in Python
Definition: Range.h:60
Gaudi::Range_::at
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences)
Definition: Range.h:191
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:105
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:230
std::prev
T prev(T... args)
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:200
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:239
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:185
Gaudi::Range_::const_reference
iter_traits::reference const_reference
Definition: Range.h:106
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:144
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:118
details
Definition: AnyDataWrapper.h:10
Gaudi::Range_::Range_
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:121
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:152
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:93
std::next
T next(T... args)
Gaudi::Range_::Range_
Range_()=default
internal types
std::iterator_traits
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:212
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:223
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:156
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:125
std::pair< ITERATOR, ITERATOR >
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:95
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:140
std::lexicographical_compare
T lexicographical_compare(T... args)
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
Gaudi::Range_::const_iterator
ITERATOR const_iterator
Definition: Range.h:96
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:154
Gaudi::details::container::Iterator
typename CONTAINER::const_iterator Iterator
Definition: Range.h:50
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:138
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:159
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:133
Gaudi::Range_
Useful class for representation of "sequence" of the objects through the range of valid iterators.
Definition: Range.h:85
std::reverse_iterator
Gaudi::Range_::~Range_
~Range_()=default
destructor
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
std::equal
T equal(T... args)
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:129
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:142
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:146
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:150
Gaudi::details::container::Container
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:49