casacore
Loading...
Searching...
No Matches
Slice.h
Go to the documentation of this file.
1// # Slice.h: Define a (start,length,increment) along an axis
2// # Copyright (C) 1993,1994,1995,1997
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_SLICE_2_H
27#define CASA_SLICE_2_H
28
29#include <cassert>
30#include <unistd.h> //# for ssize_t
31
32#include "ArrayFwd.h"
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations.
37class Slicer;
38class IPosition;
39
40// <summary> define a (start,length,increment) along an axis </summary>
41// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
42// </reviewed>
43//
44// <synopsis>
45// A "slice" (aka Section) is a a regular sub-Array (and ultimately sub-Image)
46// that is defined by defining a (start,length,increment) for each axis in
47// the array. That is, the output array's axis is of size "length", and the
48// elements are sampled by stepping along the input array in strides of
49// "increment".
50// <note role=warning>
51// The "length" is the length of the OUTPUT array, the output length
52// is NOT divided by the increment/stride.
53// </note>
54// If increment is not defined, then it defaults to one.
55// (Increment, if defined, must be >= 1). If length
56// is not defined, then it defaults to a length of one also (i.e. just the pixel
57// "start"). If start is also undefined, then all pixels along this axis are
58// chosen. This class deprecates the "_" (IndexRange) class, which had a failed
59// syntax and used (start,end,increment) which is generally less convenient.
60// Some simple examples follow:
61// <srcblock>
62// Vector<int> vi(100); // Vector of length 100;
63// //...
64// // Copy odd values onto even values
65// vi(Slice(0,50,2)) = vi(Slice(1,50,2));
66//
67// Matrix<float> mf(100,50), smallMf;
68// smallMf.reference(mf(Slice(0,10,10), Slice(0,5,10)));
69// // smallMF is now a "dezoomed" (every 10th pix)
70// // refference to mf. Of course we could also
71// // make it a copy by using assignment; e.g:
72//
73// smallMf.resize(0,0); // Make it so it will "size to fit"
74// smallMf = mf(Slice(0,10,10), Slice(0,5,10));
75// </srcblock>
76// As shown above, normally Slices will normally be used as temporaries,
77// but they may also be put into variables if desired (the default
78// copy constructors and assignment operators suffice for this class).
79//
80// While it will be unusual for a user to want this, a zero-length slice
81// is allowable.
82//
83// Another way to produce a slice from any of the Array classes is to use
84// SomeArray(blc,trc,inc) where blc,trc,inc are IPositions. This is described
85// in the documentation for Array<T>.
86// </synopsis>
87
88class Slice {
89 public:
90 // The entire range of indices on the axis is desired.
91 Slice();
92 // Create a Slice with a given start, length, and increment. The latter
93 // two default to one if not given.
94 Slice(size_t Start, size_t Length = 1, size_t Inc = 1);
95 // Create a Slice with a given start, end or length, and increment.
96 // If <src>endIsLength=false</src>, end is interpreted as length.
97 Slice(size_t Start, size_t End, size_t Inc, bool endIsLength);
98 // Was the entire range of indices on this axis selected?
99 bool all() const;
100 // Report the selected starting position. If all() is true,
101 // start=len=inc=0 is set.
102 size_t start() const;
103 // Report the defined length. If all() is true, start=len=inc=0 is set.
104 size_t length() const;
105 // Report the defined increment. If all() is true, start=len=inc=0 is set.
106 size_t inc() const;
107 // Attempt to report the last element of the slice. If all() is
108 // true, end() returns -1 (which is less than start(), which returns
109 // zero in that case).
110 size_t end() const;
111
112 // Check a vector of slices.
113 // If a vector of an axis is empty or missing, it is replaced by a Slice
114 // representing the entire axis.
115 // It checks if the Slices do not exceed the array shape.
116 // It returns the shape of the combined slices and fills the Slicer
117 // for the first array part defined by the slices.
119 const IPosition& shape);
120
121 private:
122 // # Inc of <0 is used as a private flag to mean that the whole axis is
123 // # selected. Users are given a size_t in their interface, so they cannot
124 // # set it to this. Chose Inc rather than length since it's more likely
125 // # that we'd need all bits of length than of inc. The "p" in the names
126 // # stands for private to avoid it colliding with the accessor names.
127 // # incp < 0 is chosen as the flag since the user can set inc to be zero
128 // # although that is an error that can be caught if AIPS_DEBUG is defined).
129 size_t startp;
130 ssize_t incp;
131 size_t lengthp;
132};
133
134inline Slice::Slice() : startp(0), incp(-1), lengthp(0) {
135 // Nothing
136}
137
138inline Slice::Slice(size_t Start, size_t Length, size_t Inc)
139 : startp(Start), incp(Inc), lengthp(Length) {
140#if defined(AIPS_DEBUG)
141 assert(incp > 0);
142#endif
143}
144
145inline Slice::Slice(size_t Start, size_t End, size_t Inc, bool endIsLength)
146 : startp(Start), incp(Inc), lengthp(endIsLength ? End : 1 + (End - Start) / Inc) {
147#if defined(AIPS_DEBUG)
148 if (!endIsLength) {
149 assert(End >= Start);
150 }
151 assert(incp > 0);
152#endif
153}
154
155inline bool Slice::all() const { return incp < 0; }
156
157inline size_t Slice::start() const { return startp; }
158
159inline size_t Slice::length() const { return lengthp; }
160
161inline size_t Slice::inc() const {
162 if (all()) {
163 return 0;
164 } else {
165 return incp;
166 }
167}
168
169inline size_t Slice::end() const {
170 // return -1 if all()
171 return startp + (lengthp - 1) * incp;
172}
173
174} // namespace casacore
175
176#endif
ssize_t incp
Definition Slice.h:130
size_t inc() const
Report the defined increment.
Definition Slice.h:161
size_t lengthp
Definition Slice.h:131
size_t end() const
Attempt to report the last element of the slice.
Definition Slice.h:169
static IPosition checkSlices(Vector< Vector< Slice > > &slices, Slicer &first, const IPosition &shape)
Check a vector of slices.
size_t startp
Definition Slice.h:129
size_t start() const
Report the selected starting position.
Definition Slice.h:157
bool all() const
Was the entire range of indices on this axis selected?
Definition Slice.h:155
Slice()
The entire range of indices on the axis is desired.
Definition Slice.h:134
size_t length() const
Report the defined length.
Definition Slice.h:159
struct Node * first
Definition malloc.h:325
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.