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{
90public:
91 // The entire range of indices on the axis is desired.
92 Slice();
93 // Create a Slice with a given start, length, and increment. The latter
94 // two default to one if not given.
95 Slice(size_t Start, size_t Length=1, size_t Inc=1);
96 // Create a Slice with a given start, end or length, and increment.
97 // If <src>endIsLength=false</src>, end is interpreted as length.
98 Slice(size_t Start, size_t End, size_t Inc, bool endIsLength);
99 // Was the entire range of indices on this axis selected?
100 bool all() const;
101 // Report the selected starting position. If all() is true,
102 // start=len=inc=0 is set.
103 size_t start() const;
104 // Report the defined length. If all() is true, start=len=inc=0 is set.
105 size_t length() const;
106 // Report the defined increment. If all() is true, start=len=inc=0 is set.
107 size_t inc() const;
108 // Attempt to report the last element of the slice. If all() is
109 // true, end() returns -1 (which is less than start(), which returns
110 // zero in that case).
111 size_t end() const;
112
113 // Check a vector of slices.
114 // If a vector of an axis is empty or missing, it is replaced by a Slice
115 // representing the entire axis.
116 // It checks if the Slices do not exceed the array shape.
117 // It returns the shape of the combined slices and fills the Slicer
118 // for the first array part defined by the slices.
120 const IPosition& shape);
121
122private:
123 //# Inc of <0 is used as a private flag to mean that the whole axis is
124 //# selected. Users are given a size_t in their interface, so they cannot
125 //# set it to this. Chose Inc rather than length since it's more likely
126 //# that we'd need all bits of length than of inc. The "p" in the names
127 //# stands for private to avoid it colliding with the accessor names.
128 //# incp < 0 is chosen as the flag since the user can set inc to be zero
129 //# although that is an error that can be caught if AIPS_DEBUG is defined).
130 size_t startp;
131 ssize_t incp;
132 size_t lengthp;
133};
134
135inline Slice::Slice() : startp(0), incp(-1), lengthp(0)
136{
137 // Nothing
138}
139
140inline
141Slice::Slice(size_t Start, size_t Length, size_t Inc)
142 : startp(Start), incp(Inc), lengthp(Length)
143{
144#if defined(AIPS_DEBUG)
145 assert(incp > 0);
146#endif
147}
148
149inline
150Slice::Slice(size_t Start, size_t End, size_t Inc, bool endIsLength)
151 : startp(Start), incp(Inc), lengthp(endIsLength ? End : 1+(End-Start)/Inc)
152{
153#if defined(AIPS_DEBUG)
154 if (! endIsLength) {
155 assert(End >= Start);
156 }
157 assert(incp > 0);
158#endif
159}
160
161inline bool Slice::all() const
162{
163 return incp<0;
164}
165
166inline size_t Slice::start() const
167{
168 return startp;
169}
170
171inline size_t Slice::length() const
172{
173 return lengthp;
174}
175
176inline size_t Slice::inc() const
177{
178 if (all()) {
179 return 0;
180 } else {
181 return incp;
182 }
183}
184
185inline size_t Slice::end() const
186{
187 // return -1 if all()
188 return startp + (lengthp-1)*incp;
189}
190
191
192} //# NAMESPACE CASACORE - END
193
194#endif
ssize_t incp
Definition Slice.h:131
size_t inc() const
Report the defined increment.
Definition Slice.h:176
size_t lengthp
Definition Slice.h:132
size_t end() const
Attempt to report the last element of the slice.
Definition Slice.h:185
static IPosition checkSlices(Vector< Vector< Slice > > &slices, Slicer &first, const IPosition &shape)
Check a vector of slices.
size_t startp
Definition Slice.h:130
size_t start() const
Report the selected starting position.
Definition Slice.h:166
bool all() const
Was the entire range of indices on this axis selected?
Definition Slice.h:161
Slice()
The entire range of indices on the axis is desired.
Definition Slice.h:135
size_t length() const
Report the defined length.
Definition Slice.h:171
struct Node * first
Definition malloc.h:328
this file contains all the compiler specific defines
Definition mainpage.dox:28
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991