casacore
VectorSTLIterator.h
Go to the documentation of this file.
1 //# VectorSTLIterator.h: Random access iterator for Casacore Vectors
2 //# Copyright (C) 2004
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_VECTORSTLITERATOR_2_H
29 #define CASA_VECTORSTLITERATOR_2_H
30 
31 //# Includes
32 #include "Vector.h"
33 
34 #include <iterator>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 //# Forward Declarations
39 
40 // <summary> Casacore Vector iterator </summary>
41 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
42 // </reviewed>
43 // <synopsis>
44 // This class creates a random access STL iterator for an Casacore Vector. All
45 // the STL functionality is present (or if something missing can be easily
46 // added). <br>
47 // The following comments hold:
48 // <ul>
49 // <li> A VectorSTLIterator can be created from a <src>Vector</src> (non-STL)
50 // It is the same as using <src>Vector.begin()</src>
51 // <li> No contiguous iterator is provided. Its construction is not necessary,
52 // since <src>Vector.data()</src> is a fully functional STL iterator already.
53 // <li> This iterator is non-intrusive. Since it needs state (the 'step')
54 // nothing substantial is gained by having it included in the Vector class.
55 // The major gain would be to be able to use the standard nomenclature:
56 // <src>Vector::iterator()</src> rather than <src>VectorSTLiterator</src>
57 // <li> It would be advisable, and easy to implement, to add a template argument
58 // to the <src>Array</src> classes: <src><class T, bool isCont=true></src>. The
59 // default is contiguous, since creation is contiguous. In that case correct
60 // iterators for e.g. contiguous arrays are supplied automatically.
61 // <li> needs probably some 'const' fine tuning; and the <src>-></src> operator
62 // </ul>
63 // </synopsis>
64 
65 template <typename T, typename Alloc=std::allocator<T>>
67 : public std::iterator<std::random_access_iterator_tag, T> {
68  public:
69  typedef T value_type;
70  typedef value_type* pointer;
71  typedef const value_type* const_pointer;
75  typedef const value_type& const_reference;
76  typedef std::size_t size_type;
77  typedef ptrdiff_t difference_type;
78  // Constructors. The iterator constructor from a <src>Vector</src> is
79  // the same as if created from <src>Vector.begin()</src>. Copy
80  // constructor and assignment can be the default ones.
81  // <group>
83  : start_p(const_cast<T*>(c.data())),
84  step_p (std::max(ssize_t(1), c.steps()(0))),
85  iter_p (const_cast<T*>(c.data()))
86  {}
88  {}
90  : start_p(c.pos()),
91  step_p (std::max(ssize_t(1), c.steps()(0))),
92  iter_p (start_p)
93  {}
94  // </group>
95  // Access
96  // <group>
97  reference operator[](size_t i) { return *(start_p+i*step_p); };
98  const_reference operator[](size_t i) const {
99  return *(start_p+i*step_p); };
100  reference operator*() { return *iter_p; };
101  const_reference operator*() const { return *iter_p; };
102  pointer pos() const {return iter_p; }
103  // </group>
104  // Manipulation
105  // <group>
106  const iterator &operator++() { iter_p+=step_p; return *this; };
108  iterator t = *this; iter_p+=step_p; return t; };
109  iterator &operator--() { iter_p-=step_p; return *this; };
111  VectorSTLIterator<T, Alloc> t = *this;iter_p-=step_p; return t; };
113  iter_p+=i*step_p; return *this; };
115  iter_p-=i*step_p; return *this; };
117  VectorSTLIterator<T, Alloc> t = *this; return t+=i; };
119  VectorSTLIterator<T, Alloc> t = *this; return t-=i; };
120  // </group>
121  // Size related
122  // <group>
124  return (iter_p-x.iter_p)/step_p; };
125  // </group>
126  // Comparisons
127  // <group>
128  bool operator== (const iterator &other) const {
129  return iter_p == other.iter_p; };
130  bool operator!= (const iterator other) const {
131  return iter_p != other.iter_p; };
132  bool operator< (const iterator &other) const {
133  return iter_p < other.iter_p; };
134  bool operator== (const_pointer const pos) const {
135  return iter_p == pos; };
136  bool operator!= (const_pointer const pos) const {
137  return iter_p != pos; };
138  bool operator< (const_pointer const pos) const {
139  return iter_p < pos; };
140  // </group>
141  protected:
142  // Start (for random indexing)
144  // Distance between elements
146  // Current element pointer
148 };
149 
150 
151 } //# NAMESPACE CASACORE - END
152 
153 #endif
const_reference operator[](size_t i) const
const value_type & const_reference
reference operator[](size_t i)
Access.
iterator & operator+=(difference_type i)
difference_type step_p
Distance between elements.
bool operator!=(const iterator other) const
pointer const start_p
Start (for random indexing)
VectorSTLIterator(const typename Array< T, Alloc >::IteratorSTL &c)
difference_type operator-(const VectorSTLIterator< T, Alloc > &x) const
Size related.
iterator operator+(difference_type i) const
bool operator<(const iterator &other) const
const VectorSTLIterator< T > const_iterator
VectorSTLIterator< T > iterator
const value_type * const_pointer
VectorSTLIterator(const Vector< T, Alloc > &c)
Constructors.
const iterator & operator++()
Manipulation.
pointer iter_p
Current element pointer.
const_reference operator*() const
iterator operator-(difference_type i) const
iterator & operator-=(difference_type i)
bool operator==(const iterator &other) const
Comparisons.
const Double c
Fundamental physical constants (SI units):
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition: Complex.h:352