casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: 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_SLICE_H
29 #define CASA_SLICE_H
30 
31 #include <casacore/casa/aips.h>
32 #include <unistd.h> //# for ssize_t
33 
34 #if defined(AIPS_DEBUG)
36 #endif
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 //# Forward Declarations.
41 class Slicer;
42 class IPosition;
43 template<class T> class Vector;
44 
45 // <summary> define a (start,length,increment) along an axis </summary>
46 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
47 // </reviewed>
48 //
49 // <synopsis>
50 // A "slice" (aka Section) is a a regular sub-Array (and ultimately sub-Image)
51 // that is defined by defining a (start,length,increment) for each axis in
52 // the array. That is, the output array's axis is of size "length", and the
53 // elements are sampled by stepping along the input array in strides of
54 // "increment".
55 // <note role=warning>
56 // The "length" is the length of the OUTPUT array, the output length
57 // is NOT divided by the increment/stride.
58 // </note>
59 // If increment is not defined, then it defaults to one.
60 // (Increment, if defined, must be >= 1). If length
61 // is not defined, then it defaults to a length of one also (i.e. just the pixel
62 // "start"). If start is also undefined, then all pixels along this axis are
63 // chosen. This class deprecates the "_" (IndexRange) class, which had a failed
64 // syntax and used (start,end,increment) which is generally less convenient.
65 // Some simple examples follow:
66 // <srcblock>
67 // Vector<Int> vi(100); // Vector of length 100;
68 // //...
69 // // Copy odd values onto even values
70 // vi(Slice(0,50,2)) = vi(Slice(1,50,2));
71 //
72 // Matrix<float> mf(100,50), smallMf;
73 // smallMf.reference(mf(Slice(0,10,10), Slice(0,5,10)));
74 // // smallMF is now a "dezoomed" (every 10th pix)
75 // // refference to mf. Of course we could also
76 // // make it a copy by using assignment; e.g:
77 //
78 // smallMf.resize(0,0); // Make it so it will "size to fit"
79 // smallMf = mf(Slice(0,10,10), Slice(0,5,10));
80 // </srcblock>
81 // As shown above, normally Slices will normally be used as temporaries,
82 // but they may also be put into variables if desired (the default
83 // copy constructors and assignment operators suffice for this class).
84 //
85 // While it will be unusual for a user to want this, a zero-length slice
86 // is allowable.
87 //
88 // Another way to produce a slice from any of the Array classes is to use
89 // SomeArray(blc,trc,inc) where blc,trc,inc are IPositions. This is described
90 // in the documentation for Array<T>.
91 // </synopsis>
92 
93 class Slice
94 {
95 public:
96  // The entire range of indices on the axis is desired.
97  Slice();
98  // Create a Slice with a given start, length, and increment. The latter
99  // two default to one if not given.
100  Slice(size_t Start, size_t Length=1, size_t Inc=1);
101  // Create a Slice with a given start, end or length, and increment.
102  // If <src>endIsLength=False</src>, end is interpreted as length.
103  Slice(size_t Start, size_t End, size_t Inc, Bool endIsLength);
104  // Was the entire range of indices on this axis selected?
105  Bool all() const;
106  // Report the selected starting position. If all() is true,
107  // start=len=inc=0 is set.
108  size_t start() const;
109  // Report the defined length. If all() is true, start=len=inc=0 is set.
110  size_t length() const;
111  // Report the defined increment. If all() is true, start=len=inc=0 is set.
112  size_t inc() const;
113  // Attempt to report the last element of the slice. If all() is
114  // True, end() returns -1 (which is less than start(), which returns
115  // zero in that case).
116  size_t end() const;
117 
118  // Check a vector of slices.
119  // If a vector of an axis is empty or missing, it is replaced by a Slice
120  // representing the entire axis.
121  // It checks if the Slices do not exceed the array shape.
122  // It returns the shape of the combined slices and fills the Slicer
123  // for the first array part defined by the slices.
125  const IPosition& shape);
126 
127 private:
128  //# Inc of <0 is used as a private flag to mean that the whole axis is
129  //# selected. Users are given a uInt in their interface, so they cannot
130  //# set it to this. Chose Inc rather than length since it's more likely
131  //# that we'd need all bits of length than of inc. The "p" in the names
132  //# stands for private to avoid it colliding with the accessor names.
133  //# incp < 0 is chosen as the flag since the user can set inc to be zero
134  //# although that is an error that can be caught if AIPS_DEBUG is defined).
135  size_t startp;
136  ssize_t incp;
137  size_t lengthp;
138 };
139 
140 inline Slice::Slice() : startp(0), incp(-1), lengthp(0)
141 {
142  // Nothing
143 }
144 
145 inline
146 Slice::Slice(size_t Start, size_t Length, size_t Inc)
147  : startp(Start), incp(Inc), lengthp(Length)
148 {
149 #if defined(AIPS_DEBUG)
150  DebugAssert(incp > 0, AipsError);
151 #endif
152 }
153 
154 inline
155 Slice::Slice(size_t Start, size_t End, size_t Inc, Bool endIsLength)
156  : startp(Start), incp(Inc), lengthp(endIsLength ? End : 1+(End-Start)/Inc)
157 {
158 #if defined(AIPS_DEBUG)
159  if (! endIsLength) {
160  DebugAssert(End >= Start, AipsError);
161  }
162  DebugAssert(incp > 0, AipsError);
163 #endif
164 }
165 
166 inline Bool Slice::all() const
167 {
168  return incp<0;
169 }
170 
171 inline size_t Slice::start() const
172 {
173  return startp;
174 }
175 
176 inline size_t Slice::length() const
177 {
178  return lengthp;
179 }
180 
181 inline size_t Slice::inc() const
182 {
183  if (all()) {
184  return 0;
185  } else {
186  return incp;
187  }
188 }
189 
190 inline size_t Slice::end() const
191 {
192  // return -1 if all()
193  return startp + (lengthp-1)*incp;
194 }
195 
196 
197 } //# NAMESPACE CASACORE - END
198 
199 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
A 1-D Specialization of the Array class.
size_t startp
Definition: Slice.h:135
std::vector< double > Vector
Definition: ds9context.h:24
Bool all() const
Was the entire range of indices on this axis selected?
Definition: Slice.h:166
struct Node * first
Definition: malloc.h:330
size_t lengthp
Definition: Slice.h:137
ssize_t incp
Definition: Slice.h:136
define a (start,length,increment) along an axis
Definition: Slice.h:93
size_t end() const
Attempt to report the last element of the slice.
Definition: Slice.h:190
#define DebugAssert(expr, exception)
Definition: Assert.h:185
size_t inc() const
Report the defined increment.
Definition: Slice.h:181
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape...
Definition: ExprNode.h:1944
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
Base class for all Casacore library errors.
Definition: Error.h:134
size_t start() const
Report the selected starting position.
Definition: Slice.h:171
static IPosition checkSlices(Vector< Vector< Slice > > &slices, Slicer &first, const IPosition &shape)
Check a vector of slices.
Slice()
The entire range of indices on the axis is desired.
Definition: Slice.h:140
size_t length() const
Report the defined length.
Definition: Slice.h:176
Class for those physical parameters having dimensions of Length [L].
Definition: ATMLength.h:44
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42