Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Range.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 #ifndef GAUDI_RANGE_H
12 #define GAUDI_RANGE_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <algorithm>
19 #include <utility>
20 #include <vector>
21 // ============================================================================
22 // GaudiKernel
23 // ============================================================================
24 #include "GaudiKernel/Kernel.h"
25 #include "GaudiKernel/detected.h"
26 // ============================================================================
42 // ============================================================================
43 namespace Gaudi {
44  // ==========================================================================
45  namespace details {
46  // ========================================================================
52  GAUDI_API void rangeException( const long index, const size_t size );
53  // ========================================================================
55  template <class CONTAINER>
56  struct container {
57  template <typename T>
58  using _has_container_t = typename T::Container;
60  using Iterator = typename CONTAINER::const_iterator;
61  };
62  // =========================================================================
63  } // namespace details
64  // ==========================================================================
70  struct RangeBase_ {};
71  // ==========================================================================
95  class Range_ : public RangeBase_ {
96  public:
97  // ========================================================================
99  // ========================================================================
100  public:
101  // ========================================================================
104  //
105  typedef ITERATOR iterator;
106  typedef ITERATOR const_iterator;
107  //
108  private:
109  //
111  //
112  public:
113  //
114  typedef typename iter_traits::value_type value_type;
115  typedef typename iter_traits::reference reference;
116  typedef typename iter_traits::reference const_reference;
117  //
121  // ========================================================================
122  public:
123  // ========================================================================
125  Range_() = default;
130  template <typename InputIterator>
131  Range_( InputIterator first, InputIterator last ) : m_base( first, last ) {}
135  Range_( const Base& base ) : m_base( base ) {}
139  Range_( const Container& cont ) : m_base( cont.begin(), cont.end() ) {}
140  /* constructor of empty range/sequence
141  * @param ibegin iterator to begin of empty sequence
142  */
143  Range_( iterator ibegin ) : m_base( ibegin, ibegin ) {}
145  ~Range_() = default;
146  // ========================================================================
148  bool empty() const { return m_base.second == m_base.first; }
150  size_t size() const { return std::distance( m_base.first, m_base.second ); }
152  iterator begin() const { return m_base.first; }
154  iterator end() const { return m_base.second; }
156  iterator cbegin() const { return m_base.first; }
158  iterator cend() const { return m_base.second; }
160  reverse_iterator rbegin() const { return reverse_iterator( end() ); }
162  reverse_iterator rend() const { return reverse_iterator( begin() ); }
164  const_reference front() const { return *begin(); }
166  const_reference back() const { return *std::prev( end() ); }
167  // ========================================================================
169  Range_ slice( long index1, long index2 ) const {
170  // trivial cases
171  if ( empty() || index1 == index2 ) { return Range_(); } // RETURN
172  // adjust indices
173  if ( index1 < 0 ) { index1 += size(); }
174  if ( index2 < 0 ) { index2 += size(); }
175  // check
176  if ( index1 < 0 ) { return Range_(); } // RETURN
177  if ( index2 < index1 ) { return Range_(); } // RETURN
178 
179  if ( index1 > (long)size() ) { return Range_(); } // RETURN
180  if ( index2 > (long)size() ) { index2 = size(); }
181 
182  // construct the slice
183  return Range_( std::next( begin(), index1 ), std::next( begin(), index2 ) ); // RETURN
184  }
185  // ========================================================================
190  inline const_reference operator()( const size_t index ) const { return *std::next( begin(), index ); }
195  inline const_reference operator[]( const long index ) const { return ( *this )( index ); }
201  inline const_reference at( const long index ) const {
202  if ( index < 0 || index >= (long)size() ) { Gaudi::details::rangeException( index, size() ); }
203  return ( *this )( index );
204  }
205  // ========================================================================
206  public:
207  // ========================================================================
209  template <class C, class I>
210  bool operator<( const Range_<C, I>& right ) const {
211  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
212  }
214  template <class ANOTHERCONTAINER>
215  bool operator<( const ANOTHERCONTAINER& right ) const {
216  return std::lexicographical_compare( begin(), end(), right.begin(), right.end() );
217  }
218  // ========================================================================
219  public:
220  // ========================================================================
222  bool operator==( const Range_& right ) const {
223  if ( &right == this ) { return true; } // RETURN
224  return right.size() == size() && std::equal( begin(), end(), right.begin() );
225  }
227  template <class CNT, class IT>
228  bool operator==( const Range_<CNT, IT>& right ) const {
229  return right.size() == size() && std::equal( begin(), end(), right.begin() );
230  }
232  template <class ANOTHERCONTAINER>
233  bool operator==( const ANOTHERCONTAINER& right ) const {
234  return right.size() == size() && std::equal( begin(), end(), right.begin() );
235  }
236  // ========================================================================
237  public:
238  // ========================================================================
240  bool operator!() const { return empty(); }
242  explicit operator bool() const { return !empty(); }
243  // ========================================================================
244  public:
245  // ========================================================================
247  operator const Base&() const { return base(); }
249  inline const Base& base() const { return m_base; }
250  // ========================================================================
251  private:
252  // ========================================================================
253  // the base itself
255  // ========================================================================
256  }; // end of class Range_
257  // ==========================================================================
285  template <class CONTAINER>
286  inline Range_<CONTAINER> range( const CONTAINER& cnt ) {
287  return Range_<CONTAINER>( cnt.begin(), cnt.end() );
288  }
289  // ==========================================================================
290 } // end of namespace Gaudi
291 // ============================================================================
292 // The END
293 // ============================================================================
294 #endif // GAUDI_RANGE_H
Gaudi::Range_::Range_
Range_(const Container &cont)
constructor from the container
Definition: Range.h:139
Gaudi::Range_::operator!
bool operator!() const
empty sequence?
Definition: Range.h:240
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:190
Gaudi::Range_::Container
Gaudi::details::container< CONTAINER >::Container Container
type for actual contained iterator
Definition: Range.h:103
std::equal
T equal(T... args)
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:22
Gaudi::Range_::const_reference
iter_traits::reference const_reference
Definition: Range.h:116
Gaudi::details::container
helper structure to get container type
Definition: Range.h:56
Gaudi::RangeBase_
Definition: Range.h:70
Gaudi::Range_::operator==
bool operator==(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:233
Gaudi::Range_::empty
bool empty() const
empty sequence ?
Definition: Range.h:148
Gaudi::Range_::base
const Base & base() const
conversion operator to the std::pair
Definition: Range.h:249
std::pair
Gaudi::Range_::rend
reverse_iterator rend() const
access to begin of the reversed sequence (const)
Definition: Range.h:162
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:40
Gaudi::details::container::Container
Gaudi::cpp17::detected_or_t< CONTAINER, _has_container_t, CONTAINER > Container
Definition: Range.h:59
Gaudi::Range_::at
const_reference at(const long index) const
Checked access to the elements by index (valid for all sequences)
Definition: Range.h:201
Gaudi::details::container::_has_container_t
typename T::Container _has_container_t
Definition: Range.h:58
std::distance
T distance(T... args)
Gaudi::Range_::Range_
Range_(InputIterator first, InputIterator last)
Constructor.
Definition: Range.h:131
Gaudi::Range_::~Range_
~Range_()=default
destructor
Gaudi::Range_::rbegin
reverse_iterator rbegin() const
access to begin of the reversed sequence (const)
Definition: Range.h:160
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:195
Gaudi::Range_::begin
iterator begin() const
access to begin of the sequence (const version )
Definition: Range.h:152
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:126
Gaudi::Range_::cend
iterator cend() const
access to end of the sequence (const version)
Definition: Range.h:158
Gaudi::Range_::size
size_t size() const
size of the sequence (number of elements)
Definition: Range.h:150
details
Definition: AnyDataWrapper.h:18
Gaudi::Range_::Base
std::pair< ITERATOR, ITERATOR > Base
Definition: Range.h:98
Gaudi::cpp17::detected_or_t
typename details::detector< Default, void, Op, Args... >::type detected_or_t
Definition: detected.h:50
Gaudi::Range_::Range_
Range_(iterator ibegin)
Definition: Range.h:143
std::iterator_traits
std::lexicographical_compare
T lexicographical_compare(T... args)
Gaudi::details::container::Iterator
typename CONTAINER::const_iterator Iterator
Definition: Range.h:60
Gaudi::Range_::operator==
bool operator==(const Range_< CNT, IT > &right) const
equality with another range type
Definition: Range.h:228
Gaudi::Range_::const_reverse_iterator
std::reverse_iterator< iterator > const_reverse_iterator
Definition: Range.h:119
Gaudi::Range_::m_base
Base m_base
the base itself
Definition: Range.h:254
Gaudi::Range_::reference
iter_traits::reference reference
Definition: Range.h:115
Gaudi::Range_::operator<
bool operator<(const Range_< C, I > &right) const
compare with another range
Definition: Range.h:210
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Range_
Definition: Range.h:95
Gaudi::Range_::Range_
Range_()=default
internal types
Gaudi::Range_::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Range.h:118
Gaudi::Range_::back
const_reference back() const
access for the back element (only for non-empty ranges!)
Definition: Range.h:166
Gaudi::Range_::slice
Range_ slice(long index1, long index2) const
get a "slice" of a range, in Python style
Definition: Range.h:169
Kernel.h
detected.h
Gaudi::Range_::value_type
iter_traits::value_type value_type
Definition: Range.h:114
Gaudi::Range_::Range_
Range_(const Base &base)
constructor from the pair of iterators
Definition: Range.h:135
Gaudi::Range_::iter_traits
std::iterator_traits< iterator > iter_traits
Definition: Range.h:110
std::reverse_iterator
std::prev
T prev(T... args)
Gaudi::Range_::iterator
ITERATOR iterator
Definition: Range.h:105
Gaudi::Range_::operator==
bool operator==(const Range_ &right) const
equality with another range
Definition: Range.h:222
Gaudi::Range_::operator<
bool operator<(const ANOTHERCONTAINER &right) const
compare with another container
Definition: Range.h:215
Gaudi::Range_::cbegin
iterator cbegin() const
access to begin of the sequence (const version )
Definition: Range.h:156
Gaudi::Range_::const_iterator
ITERATOR const_iterator
Definition: Range.h:106
Gaudi::Range_::end
iterator end() const
access to end of the sequence (const version)
Definition: Range.h:154
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Range_::front
const_reference front() const
access for the first element (only for non-empty ranges!)
Definition: Range.h:164
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28
std::next
T next(T... args)