| The Gaudi Framework v29r1 (5cd95fa2)

- 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 <algorithm>
9 #include <utility>
10 #include <vector>
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( const long index, const size_t size );
44 // ========================================================================
45 template <typename T>
46 struct _has_typename_container_ {
47 private:
48 template <typename T1>
49 static typename T1::Container test( int );
50 template <typename>
51 static void test( ... );
52
53 public:
54 enum { value = !std::is_void<decltype( test<T>( 0 ) )>::value };
55 };
56 template <class CONTAINER, bool>
57 struct _container;
58 template <class CONTAINER>
59 struct _container<CONTAINER, true> {
60 typedef typename CONTAINER::Container Container;
61 };
62 template <class CONTAINER>
63 struct _container<CONTAINER, false> {
64 typedef CONTAINER Container;
65 };
66 // ==========================================================================
68 template <class CONTAINER>
69 struct container {
70 typedef typename details::_container<CONTAINER, details::_has_typename_container_<CONTAINER>::value>::Container
71 Container;
72 typedef typename CONTAINER::const_iterator Iterator;
73 };
74 // =========================================================================
75 } // the end of namespace Gaudi::details
76 // ==========================================================================
82 struct RangeBase_ {
83 };
84 // ==========================================================================
107 template <class CONTAINER, class ITERATOR = typename Gaudi::details::container<CONTAINER>::Iterator>
108 class Range_ : public RangeBase_
109 {
110 public:
111 // ========================================================================
112 typedef std::pair<ITERATOR, ITERATOR> Base;
113 // ========================================================================
114 public:
115 // ========================================================================
117 typedef typename Gaudi::details::container<CONTAINER>::Container Container;
118 //
119 typedef ITERATOR iterator;
120 typedef ITERATOR const_iterator;
121 //
122 private:
123 //
124 typedef typename std::iterator_traits<iterator> iter_traits;
125 //
126 public:
127 //
128 typedef typename iter_traits::value_type value_type;
129 typedef typename iter_traits::reference reference;
130 typedef typename iter_traits::reference const_reference;
131 //
132 typedef std::reverse_iterator<iterator> reverse_iterator;
133 typedef std::reverse_iterator<iterator> const_reverse_iterator;
135 // ========================================================================
136 public:
137 // ========================================================================
139 Range_() = default;
144 Range_( iterator ibegin, iterator iend ) : m_base( ibegin, iend ) {}
148 Range_( const Base& base ) : m_base( base ) {}
152 Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
153 /* constructor of empty range/sequence
154 * @param ibegin iterator to begin of empty sequence
155 */
156 Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
158 ~Range_() = default;
159 // ========================================================================
161 bool empty() const { return m_base.second == m_base.first; }
163 size_t size() const { return std::distance( m_base.first, m_base.second ); }
165 iterator begin() const { return m_base.first; }
167 iterator end() const { return m_base.second; }
169 iterator cbegin() const { return m_base.first; }
171 iterator cend() const { return m_base.second; }
173 reverse_iterator rbegin() const { return reverse_iterator( end() ); }
175 reverse_iterator rend() const { return reverse_iterator( begin() ); }
177 const_reference front() const { return *begin(); }
179 const_reference back() const { return *std::prev( end() ); }
180 // ========================================================================
182 Range_ slice( long index1, long index2 ) const
183 {
184 // trivial cases
185 if ( empty() || index1 == index2 ) {
186 return Range_();
187 } // RETURN
188 // adjust indices
189 if ( index1 < 0 ) {
190 index1 += size();
191 }
192 if ( index2 < 0 ) {
193 index2 += size();
194 }
195 // check
196 if ( index1 < 0 ) {
197 return Range_();
198 } // RETURN
199 if ( index2 < index1 ) {
200 return Range_();
201 } // RETURN
202
203 if ( index1 > (long)size() ) {
204 return Range_();
205 } // RETURN
206 if ( index2 > (long)size() ) {
207 index2 = size();
208 }
209
210 // construct the slice
211 return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
212 }
213 // ========================================================================
218 inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
223 inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
229 inline const_reference at( const long index ) const
230 {
231 if ( index < 0 || index >= (long)size() ) {
232 Gaudi::details::rangeException( index, size() );
233 }
234 return ( *this )( index );
235 }
236 // ========================================================================
237 public:
238 // ========================================================================
240 template <class C, class I>
241 bool operator<( const Range_<C, I>& right ) const
242 {
243 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
244 }
246 template <class ANOTHERCONTAINER>
247 bool operator<( const ANOTHERCONTAINER& right ) const
248 {
249 return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
250 }
251 // ========================================================================
252 public:
253 // ========================================================================
255 bool operator==( const Range_& right ) const
256 {
257 if ( &right == this ) {
258 return true;
259 } // RETURN
260 return right.size() == size() && std::equal( begin(), end(), right.begin() );
261 }
263 template <class CNT, class IT>
264 bool operator==( const Range_<CNT, IT>& right ) const
265 {
266 return right.size() == size() && std::equal( begin(), end(), right.begin() );
267 }
269 template <class ANOTHERCONTAINER>
270 bool operator==( const ANOTHERCONTAINER& right ) const
271 {
272 return right.size() == size() && std::equal( begin(), end(), right.begin() );
273 }
274 // ========================================================================
275 public:
276 // ========================================================================
278 bool operator!() const { return empty(); }
280 explicit operator bool() const { return !empty(); }
281 // ========================================================================
282 public:
283 // ========================================================================
285 operator const Base&() const { return base(); }
287 inline const Base& base() const { return m_base; }
288 // ========================================================================
289 private:
290 // ========================================================================
291 // the base itself
292 Base m_base;
293 // ========================================================================
294 }; // end of class Range_
295 // ==========================================================================
323 template <class CONTAINER>
324 inline Range_<CONTAINER> range( const CONTAINER& cnt )
325 {
326 return Range_<CONTAINER>( cnt.begin(), cnt.end() );
327 }
328 // ==========================================================================
329 } // end of namespace Gaudi
330 // ============================================================================
331 // The END
332 // ============================================================================
333 #endif // GAUDI_RANGE_H
334 // ============================================================================
Gaudi::details::container::Container
details::_container< CONTAINER, details::_has_typename_container_< CONTAINER >::value >::Container Container
Definition: Range.h:71
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:161
Gaudi::details::_container
Definition: Range.h:57
Kernel.h
Gaudi::details::_container< CONTAINER, false >::Container
CONTAINER Container
Definition: Range.h:64
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:112
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:133
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:292
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_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:169
Gaudi::Range_::value_type
iter_traits::value_type value_type
Definition: Range.h:128
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:132
Gaudi::details::container
helper structure to get container type
Definition: Range.h:69
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:124
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:182
Gaudi::RangeBase_
helper class to simplify the dealing with ranges in Python
Definition: Range.h:82
Gaudi::details::_container< CONTAINER, true >::Container
CONTAINER::Container Container
Definition: Range.h:60
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:173
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:129
Gaudi::Range_::operator==
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:264
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:177
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:278
Gaudi::Range_::Range_
Range_(iterator ibegin, iterator iend)
Constructor.
Definition: Range.h:144
std::prev
T prev(T...args)
Gaudi::Range_::const_reference
iter_traits::reference const_reference
Definition: Range.h:130
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:223
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:121
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:117
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:229
std::iterator_traits
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:148
std::pair< ITERATOR, ITERATOR >
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:119
std::lexicographical_compare
T lexicographical_compare(T...args)
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:255
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:120
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:167
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:287
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:171
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:270
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:247
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:156
Gaudi::Range_
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:108
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:163
std::reverse_iterator
Gaudi::details::_has_typename_container_
Definition: Range.h:46
Gaudi::details::container::Iterator
CONTAINER::const_iterator Iterator
Definition: Range.h:72
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:175
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:218
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:179
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
std::equal
T equal(T...args)
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:152
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:165
Gaudi::details::_has_typename_container_::test
static T1::Container test(int)