Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Range.h
Go to the documentation of this file.
1 // $Id: Range.h,v 1.2 2008/11/03 11:24:47 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDI_RANGE_H
4 #define GAUDI_RANGE_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <utility>
11 #include <vector>
12 #include <algorithm>
13 #include "GaudiKernel/Kernel.h"
14 // ============================================================================
30 // ============================================================================
31 namespace Gaudi
32 {
33  namespace details
34  {
35  // ========================================================================
42  ( const long index ,
43  const size_t size ) ;
44  // ========================================================================
45  }
46  // ==========================================================================
52  struct RangeBase_ {} ;
53  // ==========================================================================
76  template <class CONTAINER>
77  class Range_ : public RangeBase_
78  {
79  public:
80  // ========================================================================
81  typedef std::pair<typename CONTAINER::const_iterator,
82  typename CONTAINER::const_iterator> Base ;
83  // ========================================================================
84  public:
85  // ========================================================================
87  typedef CONTAINER Container ;
88  typedef typename Container::value_type value_type ;
89  typedef typename Container::const_iterator iterator ;
90  typedef typename Container::const_iterator const_iterator ;
91  typedef typename Container::const_reverse_iterator reverse_iterator ;
92  typedef typename Container::const_reverse_iterator const_reverse_iterator ;
93  typedef typename Container::const_reference reference ;
94  typedef typename Container::const_reference const_reference ;
98  // ========================================================================
99  public:
100  // ========================================================================
102  Range_() : m_base( iterator() , iterator() ) {}
107  Range_( iterator ibegin , iterator iend ) : m_base ( ibegin , iend ) {}
111  Range_( const Base& base ) : m_base( base ) {}
115  Range_( const Container& cont ) : m_base( cont.begin() , cont.end() ) {}
116  /* constructor of empty range/sequence
117  * @param ibegin iterator to begin of empty sequence
118  */
119  Range_( iterator ibegin ) : m_base( ibegin , ibegin ) {}
122  // ========================================================================
124  inline bool empty () const { return m_base.second == m_base.first ; }
126  inline size_t size () const
127  { return std::distance ( m_base.first , m_base.second ) ; }
129  inline iterator begin () const { return m_base.first ; }
131  inline iterator end () const { return m_base.second ; }
133  inline reverse_iterator rbegin () const { return reverse_iterator ( end () ) ; }
135  inline reverse_iterator rend () const { return reverse_iterator ( begin () ) ; }
137  inline const_reference front () const { return *( begin () ) ; }
139  inline const_reference back () const
140  {
141  const_iterator i = end() ;
142  std::advance ( i , -1 ) ;
143  return *i ;
144  }
145  // ========================================================================
147  inline Range_ slice( long index1 , long index2 ) const
148  {
149  // trivial cases
150  if ( empty() || index1 == index2 ) { return Range_() ; } // RETURN
151  // adjust indices
152  if ( index1 < 0 ) { index1 += size () ; }
153  if ( index2 < 0 ) { index2 += size () ; }
154  // check
155  if ( index1 < 0 ) { return Range_ () ; } // RETURN
156  if ( index2 < index1 ) { return Range_ () ; } // RETURN
157 
158  if ( index1 > (long) size () ) { return Range_() ; } // RETURN
159  if ( index2 > (long) size () ) { index2 = size() ; }
160 
161  const_iterator i1 = begin() ;
162  std::advance ( i1 , index1 ) ;
163  const_iterator i2 = begin() ;
164  std::advance ( i2 , index2 ) ;
165  // construct the slice
166  return Range_( i1 , i2 ) ; // RETURN
167  }
168  // ========================================================================
173  inline const_reference operator () ( const size_t index ) const
174  {
175  const_iterator i = begin() ;
176  std::advance ( i , index ) ;
177  return *i ;
178  }
183  inline const_reference operator [] ( const long index ) const
184  { return (*this)( index ) ; }
190  inline const_reference at ( const long index ) const
191  {
192  if ( index < 0 || index >= (long) size () )
193  { Gaudi::details::rangeException( index , size() ) ; }
194  return (*this) ( index );
195  }
196  // ========================================================================
197  public:
198  // ========================================================================
200  bool operator< ( const Range_& right ) const
201  {
203  ( begin () , end () , right.begin () , right.end () ) ;
204  }
206  bool operator< ( const Container& right ) const
207  {
209  ( begin () , end () , right.begin () , right.end () ) ;
210  }
211  // ========================================================================
212  public:
213  // ========================================================================
215  bool operator==( const Range_& right ) const
216  {
217  if ( &right == this ) { return true ; } // RETURN
218  if ( right.size () != size () ) { return false ; } // RETURN
219  return std::equal ( begin () , end () , right.begin() ) ;
220  }
222  bool operator==( const Container& right ) const
223  {
224  if ( right.size () != size () ) { return false ; } // RETURN
225  return std::equal ( begin () , end () , right.begin() ) ;
226  }
227  // ========================================================================
228  public:
229  // ========================================================================
231  bool operator! () const { return empty () ; }
232  // ========================================================================
233  public:
234  // ========================================================================
236  operator const Base& () const { return base () ; }
238  inline const Base& base () const { return m_base ; }
239  // ========================================================================
240  private:
241  // ========================================================================
242  // the base itself
244  // ========================================================================
245  }; // end of class Range_
246  // ==========================================================================
274  template <class CONTAINER>
275  inline
277  range ( const CONTAINER& cnt )
278  { return Range_<CONTAINER>( cnt.begin() , cnt.end() ) ; }
279  // ==========================================================================
280 } // end of namespace Gaudi
281 // ============================================================================
282 // The END
283 // ============================================================================
284 #endif // GAUDI_RANGE_H
285 // ============================================================================

Generated at Wed Dec 4 2013 14:33:09 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004