| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <utility>
9 #include <vector>
10 #include <algorithm>
11 #include "GaudiKernel/Kernel.h"
12 // ============================================================================
28 // ============================================================================
29 namespace Gaudi
30 {
31 namespace details
32 {
33 // ========================================================================
39 GAUDI_API void rangeException
40 ( const long index ,
41 const size_t size ) ;
42 // ========================================================================
43 }
44 // ==========================================================================
50 struct RangeBase_ {} ;
51 // ==========================================================================
74 template <class CONTAINER>
75 class Range_ : public RangeBase_
76 {
77 public:
78 // ========================================================================
79 typedef std::pair<typename CONTAINER::const_iterator,
80 typename CONTAINER::const_iterator> Base ;
81 // ========================================================================
82 public:
83 // ========================================================================
85 typedef CONTAINER Container ;
86 typedef typename Container::value_type value_type ;
87 typedef typename Container::const_iterator iterator ;
88 typedef typename Container::const_iterator const_iterator ;
89 typedef typename Container::const_reverse_iterator reverse_iterator ;
90 typedef typename Container::const_reverse_iterator const_reverse_iterator ;
91 typedef typename Container::const_reference reference ;
92 typedef typename Container::const_reference const_reference ;
94 typedef std::pair<iterator,iterator> _Base ;
95 typedef Range_<Container> _Self ;
96 // ========================================================================
97 public:
98 // ========================================================================
100 Range_() = default;
105 Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
109 Range_( const Base& base ) : m_base( base ) {}
113 Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
114 /* constructor of empty range/sequence
115 * @param ibegin iterator to begin of empty sequence
116 */
117 Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
119 ~Range_() = default;
120 // ========================================================================
122 inline bool empty () const { return m_base.second == m_base.first ; }
124 inline size_t size () const
125 { return std::distance ( m_base.first , m_base.second ) ; }
127 inline iterator begin () const { return m_base.first ; }
129 inline iterator end () const { return m_base.second ; }
131 inline reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
133 inline reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
135 inline const_reference front () const { return *begin() ; }
137 inline const_reference back () const { return *std::prev( end() ); }
138 // ========================================================================
140 inline Range_ slice( long index1 , long index2 ) const
141 {
142 // trivial cases
143 if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
144 // adjust indices
145 if ( index1 < 0 ) { index1 += size () ; }
146 if ( index2 < 0 ) { index2 += size () ; }
147 // check
148 if ( index1 < 0 ) { return Range_ () ; } // RETURN
149 if ( index2 < index1 ) { return Range_ () ; } // RETURN
150
151 if ( index1 > (long) size () ) { return Range_() ; } // RETURN
152 if ( index2 > (long) size () ) { index2 = size() ; }
153
154 // construct the slice
155 return Range_( std::next ( begin() , index1 ) ,
156 std::next ( begin() , index2 ) ) ; // RETURN
157 }
158 // ========================================================================
163 inline const_reference operator () ( const size_t index ) const
164 {
165 return *std::next ( begin() , index ) ;
166 }
171 inline const_reference operator [] ( const long index ) const
172 { return (*this)( index ) ; }
178 inline const_reference at ( const long index ) const
179 {
180 if ( index < 0 || index >= (long) size () )
181 { Gaudi::details::rangeException( index , size() ) ; }
182 return (*this) ( index );
183 }
184 // ========================================================================
185 public:
186 // ========================================================================
188 bool operator< ( const Range_& right ) const
189 {
190 return std::lexicographical_compare
191 ( begin () , end () , right.begin () , right.end () ) ;
192 }
194 bool operator< ( const Container& right ) const
195 {
196 return std::lexicographical_compare
197 ( begin () , end () , right.begin () , right.end () ) ;
198 }
199 // ========================================================================
200 public:
201 // ========================================================================
203 bool operator==( const Range_& right ) const
204 {
205 if ( &right == this ) { return true ; } // RETURN
206 return right.size() == size() &&
207 std::equal ( begin () , end () , right.begin() ) ;
208 }
210 bool operator==( const Container& right ) const
211 {
212 return right.size() == size() &&
213 std::equal ( begin () , end () , right.begin() ) ;
214 }
215 // ========================================================================
216 public:
217 // ========================================================================
219 bool operator! () const { return empty () ; }
220 // ========================================================================
221 public:
222 // ========================================================================
224 operator const Base& () const { return base () ; }
226 inline const Base& base () const { return m_base ; }
227 // ========================================================================
228 private:
229 // ========================================================================
230 // the base itself
231 Base m_base ;
232 // ========================================================================
233 }; // end of class Range_
234 // ==========================================================================
262 template <class CONTAINER>
263 inline
264 Range_<CONTAINER>
265 range ( const CONTAINER& cnt )
266 { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
267 // ==========================================================================
268 } // end of namespace Gaudi
269 // ============================================================================
270 // The END
271 // ============================================================================
272 #endif // GAUDI_RANGE_H
273 // ============================================================================
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:122
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:203
Gaudi::Range_::_Base
std::pair< iterator, iterator > _Base
internal types
Definition: Range.h:94
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:171
Kernel.h
Gaudi::Range_::Base
std::pair< typename CONTAINER::const_iterator, typename CONTAINER::const_iterator > Base
Definition: Range.h:80
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:31
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:140
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:163
Gaudi::RangeBase_
helper class to simplify the dealing with ranges in Python
Definition: Range.h:50
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:231
Gaudi::Range_::Range_
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:105
Gaudi::Range_::~Range_
~Range_()=default
destructor
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:137
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:135
Gaudi::Range_::at
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences) for out-of-range access...
Definition: Range.h:178
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:124
Gaudi::Range_::_Self
Range_< Container > _Self
Definition: Range.h:95
std::prev
T prev(T...args)
Gaudi::Range_::operator<
bool operator<(const Range_ &right) const
compare with another range
Definition: Range.h:188
Gaudi::Range_::const_reverse_iterator
Container::const_reverse_iterator const_reverse_iterator
Definition: Range.h:90
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
Definition: NamedRange.h:130
Gaudi::Range_::Container
CONTAINER Container
type for actual contained iterator
Definition: Range.h:85
std::next
T next(T...args)
Gaudi::Range_::value_type
Container::value_type value_type
Definition: Range.h:86
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:127
Gaudi::Range_::operator==
bool operator==(const Container &right) const
equality with the base container
Definition: Range.h:210
Gaudi::Range_::const_iterator
Container::const_iterator const_iterator
Definition: Range.h:88
std::pair
std::lexicographical_compare
T lexicographical_compare(T...args)
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:113
Gaudi::Range_::iterator
Container::const_iterator iterator
Definition: Range.h:87
Gaudi::Range_::reverse_iterator
Container::const_reverse_iterator reverse_iterator
Definition: Range.h:89
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:226
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:117
Gaudi::Range_
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:75
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:129
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:109
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:131
Gaudi::Range_::reference
Container::const_reference reference
Definition: Range.h:91
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
Gaudi::Range_::Range_
Range_()=default
default constructor
std::equal
T equal(T...args)
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::Range_::const_reference
Container::const_reference const_reference
Definition: Range.h:92
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:133
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:219