casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef CASA_VECTORSTLITERATOR_2_H
27#define CASA_VECTORSTLITERATOR_2_H
28
29// # Includes
30#include "Vector.h"
31
32#include <iterator>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37
38// <summary> Casacore Vector iterator </summary>
39// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
40// </reviewed>
41// <synopsis>
42// This class creates a random access STL iterator for an Casacore Vector. All
43// the STL functionality is present (or if something missing can be easily
44// added). <br>
45// The following comments hold:
46// <ul>
47// <li> A VectorSTLIterator can be created from a <src>Vector</src> (non-STL)
48// It is the same as using <src>Vector.begin()</src>
49// <li> No contiguous iterator is provided. Its construction is not necessary,
50// since <src>Vector.data()</src> is a fully functional STL iterator already.
51// <li> This iterator is non-intrusive. Since it needs state (the 'step')
52// nothing substantial is gained by having it included in the Vector class.
53// The major gain would be to be able to use the standard nomenclature:
54// <src>Vector::iterator()</src> rather than <src>VectorSTLiterator</src>
55// <li> It would be advisable, and easy to implement, to add a template argument
56// to the <src>Array</src> classes: <src><class T, bool isCont=true></src>. The
57// default is contiguous, since creation is contiguous. In that case correct
58// iterators for e.g. contiguous arrays are supplied automatically.
59// <li> needs probably some 'const' fine tuning; and the <src>-></src> operator
60// </ul>
61// </synopsis>
62
63template <typename T>
65 public:
66 // iterator traits
67 using iterator_category = std::random_access_iterator_tag;
68
69 typedef T value_type;
71 typedef const value_type *const_pointer;
76 typedef std::size_t size_type;
77 typedef ptrdiff_t difference_type;
78
79 // Constructors. The iterator constructor from a <src>Vector</src> is
80 // the same as if created from <src>Vector.begin()</src>. Copy
81 // constructor and assignment can be the default ones.
82 // <group>
83 explicit VectorSTLIterator(const Vector<T> &c)
84 : start_p(const_cast<T *>(c.data())),
85 step_p(std::max(ssize_t(1), c.steps()(0))),
86 iter_p(const_cast<T *>(c.data())) {}
89 : start_p(c.pos()), step_p(std::max(ssize_t(1), c.steps()(0))), iter_p(start_p) {}
90 // </group>
91 // Access
92 // <group>
93 reference operator[](size_t i) { return *(start_p + i * step_p); };
94 const_reference operator[](size_t i) const { return *(start_p + i * step_p); };
95 reference operator*() { return *iter_p; };
96 const_reference operator*() const { return *iter_p; };
97 pointer pos() const { return iter_p; }
98 // </group>
99 // Manipulation
100 // <group>
102 iter_p += step_p;
103 return *this;
104 };
106 iterator t = *this;
107 iter_p += step_p;
108 return t;
109 };
111 iter_p -= step_p;
112 return *this;
113 };
115 VectorSTLIterator<T> t = *this;
116 iter_p -= step_p;
117 return t;
118 };
120 iter_p += i * step_p;
121 return *this;
122 };
124 iter_p -= i * step_p;
125 return *this;
126 };
128 VectorSTLIterator<T> t = *this;
129 return t += i;
130 };
132 VectorSTLIterator<T> t = *this;
133 return t -= i;
134 };
135 // </group>
136 // Size related
137 // <group>
139 return (iter_p - x.iter_p) / step_p;
140 };
141 // </group>
142 // Comparisons
143 // <group>
144 bool operator==(const iterator &other) const { return iter_p == other.iter_p; };
145 bool operator!=(const iterator other) const { return iter_p != other.iter_p; };
146 bool operator<(const iterator &other) const { return iter_p < other.iter_p; };
147 bool operator==(const_pointer const pos) const { return iter_p == pos; };
148 bool operator!=(const_pointer const pos) const { return iter_p != pos; };
149 bool operator<(const_pointer const pos) const { return iter_p < pos; };
150 // </group>
151 protected:
152 // Start (for random indexing)
154 // Distance between elements
156 // Current element pointer
158};
159
160} // namespace casacore
161
162#endif
reference operator[](size_t i)
Access.
const iterator & operator++()
Manipulation.
const value_type * const_pointer
bool operator==(const iterator &other) const
Comparisons.
iterator operator-(difference_type i) const
iterator & operator-=(difference_type i)
bool operator!=(const iterator other) const
bool operator!=(const_pointer const pos) const
difference_type operator-(const VectorSTLIterator< T > &x) const
Size related.
const value_type & const_reference
bool operator==(const_pointer const pos) const
iterator & operator+=(difference_type i)
VectorSTLIterator(const Vector< T > &c)
Constructors.
std::random_access_iterator_tag iterator_category
iterator traits
const_reference operator[](size_t i) const
pointer iter_p
Current element pointer.
difference_type step_p
Distance between elements.
const VectorSTLIterator< T > const_iterator
iterator operator+(difference_type i) const
VectorSTLIterator< T > iterator
bool operator<(const iterator &other) const
pointer const start_p
Start (for random indexing).
const_reference operator*() const
VectorSTLIterator(const typename Array< T >::IteratorSTL &c)
bool operator<(const_pointer const pos) const
For temporary backward namespace compatibility, use casa as alias for casacore.
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:344