| The Gaudi Framework v28r2-patches (bc4ad17a)

- 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 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/Kernel.h"
15 // ============================================================================
31 // ============================================================================
32 namespace Gaudi
33 {
34 // ==========================================================================
35 namespace details
36 {
37 // ========================================================================
43 GAUDI_API void rangeException
44 ( const long index ,
45 const size_t size ) ;
46 // ========================================================================
47 template <typename T>
48 struct _has_typename_container_
49 {
50 private:
51 template <typename T1> static typename T1::Container test(int);
52 template <typename> static void test(...);
53 public:
54 enum { value = !std::is_void<decltype(test<T>(0))>::value };
55 };
56 template <class CONTAINER,bool> struct _container ;
57 template <class CONTAINER>
58 struct _container<CONTAINER,true> { typedef typename CONTAINER::Container Container ; };
59 template <class CONTAINER>
60 struct _container<CONTAINER,false> { typedef CONTAINER Container ; };
61 // ==========================================================================
63 template <class CONTAINER>
64 struct container
65 {
66 typedef typename details::_container<CONTAINER,details::_has_typename_container_<CONTAINER>::value>::Container Container ;
67 typedef typename CONTAINER::const_iterator Iterator ;
68 };
69 // =========================================================================
70 } // the end of namespace Gaudi::details
71 // ==========================================================================
77 struct RangeBase_ {} ;
78 // ==========================================================================
101 template <class CONTAINER, class ITERATOR = typename Gaudi::details::container<CONTAINER>::Iterator>
102 class Range_ : public RangeBase_
103 {
104 public:
105 // ========================================================================
106 typedef std::pair<ITERATOR,ITERATOR> Base ;
107 // ========================================================================
108 public:
109 // ========================================================================
111 typedef typename Gaudi::details::container<CONTAINER>::Container Container ;
112 //
113 typedef ITERATOR iterator ;
114 typedef ITERATOR const_iterator ;
115 //
116 private:
117 //
118 typedef typename std::iterator_traits<iterator> iter_traits ;
119 //
120 public:
121 //
122 typedef typename iter_traits::value_type value_type ;
123 typedef typename iter_traits::reference reference ;
124 typedef typename iter_traits::reference const_reference ;
125 //
126 typedef std::reverse_iterator<iterator> reverse_iterator ;
127 typedef std::reverse_iterator<iterator> const_reverse_iterator ;
129 // ========================================================================
130 public:
131 // ========================================================================
133 Range_() = default;
138 Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
142 Range_( const Base& base ) : m_base( base ) {}
146 Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
147 /* constructor of empty range/sequence
148 * @param ibegin iterator to begin of empty sequence
149 */
150 Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
152 ~Range_() = default;
153 // ========================================================================
155 bool empty () const { return m_base.second == m_base.first ; }
157 size_t size () const
158 { return std::distance ( m_base.first , m_base.second ) ; }
160 iterator begin () const { return m_base.first ; }
162 iterator end () const { return m_base.second ; }
164 iterator cbegin () const { return m_base.first ; }
166 iterator cend () const { return m_base.second ; }
168 reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
170 reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
172 const_reference front () const { return *begin() ; }
174 const_reference back () const { return *std::prev( end() ); }
175 // ========================================================================
177 Range_ slice( long index1 , long index2 ) const
178 {
179 // trivial cases
180 if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
181 // adjust indices
182 if ( index1 < 0 ) { index1 += size () ; }
183 if ( index2 < 0 ) { index2 += size () ; }
184 // check
185 if ( index1 < 0 ) { return Range_ () ; } // RETURN
186 if ( index2 < index1 ) { return Range_ () ; } // RETURN
187
188 if ( index1 > (long) size () ) { return Range_() ; } // RETURN
189 if ( index2 > (long) size () ) { index2 = size() ; }
190
191 // construct the slice
192 return Range_( std::next ( begin() , index1 ) ,
193 std::next ( begin() , index2 ) ) ; // RETURN
194 }
195 // ========================================================================
200 inline const_reference operator () ( const size_t index ) const
201 { return *std::next ( begin() , index ) ; }
206 inline const_reference operator [] ( const long index ) const
207 { return (*this)( index ) ; }
213 inline const_reference at ( const long index ) const
214 {
215 if ( index < 0 || index >= (long) size () )
216 { Gaudi::details::rangeException( index , size() ) ; }
217 return (*this) ( index );
218 }
219 // ========================================================================
220 public:
221 // ========================================================================
223 template <class C, class I>
224 bool operator< ( const Range_<C,I>& right ) const
225 {
226 return std::lexicographical_compare
227 ( begin () , end () , right.begin () , right.end () ) ;
228 }
230 template <class ANOTHERCONTAINER>
231 bool operator< ( const ANOTHERCONTAINER& right ) const
232 {
233 return std::lexicographical_compare
234 ( begin () , end () , right.begin () , right.end () ) ;
235 }
236 // ========================================================================
237 public:
238 // ========================================================================
240 bool operator==( const Range_& right ) const
241 {
242 if ( &right == this ) { return true ; } // RETURN
243 return right.size() == size() &&
244 std::equal ( begin () , end () , right.begin() ) ;
245 }
247 template <class CNT, class IT>
248 bool operator==( const Range_<CNT,IT>& right ) const
249 {
250 return right.size() == size() &&
251 std::equal ( begin () , end () , right.begin() ) ;
252 }
254 template <class ANOTHERCONTAINER>
255 bool operator==( const ANOTHERCONTAINER& right ) const
256 {
257 return right.size() == size() &&
258 std::equal ( begin () , end () , right.begin() ) ;
259 }
260 // ========================================================================
261 public:
262 // ========================================================================
264 bool operator! () const { return empty () ; }
266 explicit operator bool() const { return !empty () ; }
267 // ========================================================================
268 public:
269 // ========================================================================
271 operator const Base& () const { return base () ; }
273 inline const Base& base () const { return m_base ; }
274 // ========================================================================
275 private:
276 // ========================================================================
277 // the base itself
278 Base m_base ;
279 // ========================================================================
280 }; // end of class Range_
281 // ==========================================================================
309 template <class CONTAINER>
310 inline
311 Range_<CONTAINER>
312 range ( const CONTAINER& cnt )
313 { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
314 // ==========================================================================
315 } // end of namespace Gaudi
316 // ============================================================================
317 // The END
318 // ============================================================================
319 #endif // GAUDI_RANGE_H
320 // ============================================================================
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:155
Gaudi::details::_container
Definition: Range.h:56
Kernel.h
Gaudi::details::_container< CONTAINER, false >::Container
CONTAINER Container
Definition: Range.h:60
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:127
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:278
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_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:164
Gaudi::Range_::value_type
iter_traits::value_type value_type
Definition: Range.h:122
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:126
Gaudi::details::container
helper structure to get container type
Definition: Range.h:64
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:118
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:177
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:106
Gaudi::RangeBase_
helper class to simplify the dealing with ranges in Python
Definition: Range.h:77
Gaudi::details::_container< CONTAINER, true >::Container
CONTAINER::Container Container
Definition: Range.h:58
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:168
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:123
Gaudi::Range_::operator==
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:248
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:172
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:48
Gaudi::Range_::Range_
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:138
std::prev
T prev(T...args)
Gaudi::Range_::const_reference
iter_traits::reference const_reference
Definition: Range.h:124
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:129
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:111
std::next
T next(T...args)
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:213
std::iterator_traits
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:142
std::pair< ITERATOR, ITERATOR >
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:113
operator<
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
std::lexicographical_compare
T lexicographical_compare(T...args)
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:240
Gaudi::Functional::operator()
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
std::is_void
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
Gaudi::Range_::const_iterator
ITERATOR const_iterator
Definition: Range.h:114
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:162
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:273
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:166
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:255
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:150
Gaudi::Range_
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:102
Gaudi::details::container::Container
details::_container< CONTAINER, details::_has_typename_container_< CONTAINER >::value >::Container Container
Definition: Range.h:66
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:157
std::reverse_iterator
Gaudi::details::_has_typename_container_
Definition: Range.h:48
Gaudi::details::container::Iterator
CONTAINER::const_iterator Iterator
Definition: Range.h:67
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:170
Gaudi::details::_has_typename_container_::value
Definition: Range.h:54
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:174
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
std::equal
T equal(T...args)
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:146
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:160
Gaudi::details::_has_typename_container_::test
static T1::Container test(int)