casacore
Loading...
Searching...
No Matches
VectorIter.h
Go to the documentation of this file.
1//# VectorIter.h: Iterate a vector cursor through another array
2//# Copyright (C) 1993,1994,1995,1999
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_VECTORITER_2_H
29#define CASA_VECTORITER_2_H
30
31#include "ArrayIter.h"
32#include "Vector.h"
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36//
37// <summary> Iterate an Vector cursor through another Array. </summary>
38// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
39// </reviewed>
40//
41// VectorIterator steps a Vector (the "cursor") through an array for the
42// given axis.
43// The cursor "refers" to storage in the array, so that changing the
44// values in the cursor changes values in the original array.
45//
46// This class is derived from ArrayIterator; basically it only adds
47// the vector() member function which allows you to access the cursor
48// as a Vector.
49//
50// <note role=tip>
51// The origin of the cursor, i.e. the subarray that moves through the
52// larger array, is always zero.
53// </note>
54//
55// In this example we sum all the elements of an array; of course we already
56// have the "sum" function in ArrayMath.h that we should use instead.
57//
58// <srcblock>
59// Array<float> af;
60// // set af
61// VectorIterator vi(af);
62// float sum = 0.0;
63// size_t n = vi.vector().nelements();
64// while (! vi.pastEnd()) {
65// for (int i=0; i < n; i++) { // N.B.; cursor always 0 based.
66// sum += vi.vector()(i);
67// }
68// vi.next();
69// }
70// </srcblock>
71
72template<typename T, typename Alloc=std::allocator<T>>
73class VectorIterator : public ArrayIterator<T, Alloc>
74{
75public:
76 // Iterate by vector cursors through array "a".
77 // The vector cursor is taken for the given axis.
78 explicit VectorIterator(Array<T, Alloc> &a, size_t axis=0);
79
80 // Return a Vector at the current position.
81 Vector<T, Alloc> &vector() {return *(Vector<T, Alloc> *)this->ap_p.get();}
82
83private:
84 // Not implemented.
86 // Not implemented.
88};
89
90//
91// <summary> Iterate a Vector cursor through another Array. </summary>
92// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
93// </reviewed>
94//
95// ReadOnlyVectorIterator behaves exactly like VectorIterator (cf.) only
96// it should be used on const Arrays.
97//
98// <note role=tip> Note that the R/O VectorIterator is not derived from R/O
99// ArrayIterator.
100// </note>
101
102template<typename T, typename Alloc=std::allocator<T>> class ReadOnlyVectorIterator
103{
104public:
105 // <group>
106 explicit ReadOnlyVectorIterator(const Array<T, Alloc> &a, size_t axis=0)
107 : vi(const_cast<Array<T>&>(a), axis) {}
108
109 void next() {vi.next();}
110 void reset() {vi.origin();}
111 void origin() {vi.origin();}
112
113 const Array<T, Alloc> &array() {return vi.array();}
114 const Vector<T, Alloc> &vector() {return vi.vector();}
115
116 bool atStart() const {return vi.atStart();}
117 bool pastEnd() const {return vi.pastEnd();}
118 const IPosition &pos() const {return vi.pos();}
119 IPosition endPos() const {return vi.endPos();}
120 size_t ndim() const {return vi.ndim();}
121 // </group>
122private:
123 // Not implemented.
125 // Not implemented.
127
129};
130
131
132} //# NAMESPACE CASACORE - END
133
134#include "VectorIter.tcc"
135
136#endif
std::unique_ptr< Array< T, Alloc > > ap_p
The cursor.
Definition ArrayIter.h:122
Iterate a Vector cursor through another Array.
Definition VectorIter.h:103
VectorIterator< T, Alloc > vi
Definition VectorIter.h:128
const IPosition & pos() const
Definition VectorIter.h:118
ReadOnlyVectorIterator< T, Alloc > & operator=(const ReadOnlyVectorIterator< T, Alloc > &)=delete
Not implemented.
const Array< T, Alloc > & array()
Definition VectorIter.h:113
ReadOnlyVectorIterator(const Array< T, Alloc > &a, size_t axis=0)
Definition VectorIter.h:106
const Vector< T, Alloc > & vector()
Definition VectorIter.h:114
ReadOnlyVectorIterator(const ReadOnlyVectorIterator< T, Alloc > &)=delete
Not implemented.
VectorIterator(Array< T, Alloc > &a, size_t axis=0)
Iterate by vector cursors through array "a".
Vector< T, Alloc > & vector()
Return a Vector at the current position.
Definition VectorIter.h:81
VectorIterator< T, Alloc > & operator=(const VectorIterator< T, Alloc > &)=delete
Not implemented.
VectorIterator(const VectorIterator< T, Alloc > &)=delete
Not implemented.
this file contains all the compiler specific defines
Definition mainpage.dox:28