Index.h
Classes
- Index -- Interconvert between 0 and 1 based relative indexing. (full description)
Interface
- Public Members
- Index(Int zeroRelValue=0) : local_value_p(zeroRelValue)
- Index(uInt zeroRelValue) : local_value_p(zeroRelValue)
- Index(const Index &other) : local_value_p(other.local_value_p)
- Index &operator=(const Index &other)
- ~Index()
- Int operator()() const
- Int zeroRelativeValue() const
- Int oneRelativeValue() const
- static void convertVector(Vector<Int> &out, const Vector<Index> &in, Bool outValuesAreLocal=True)
- static void convertVector(Vector<Index> &out, const Vector<Int> &in, Bool inValuesAreLocal=True)
- static void convertIPosition(IPosition &out, const Vector<Index> &in, Bool outValuesAreLocal=True)
- static void convertIPosition(Vector<Index> &out, const IPosition &in, Bool inValuesAreLocal=True)
Review Status
- Reviewed By:
- David Barnes
- Date Reviewed:
- 1998/12/01
- Programs:
- Tests:
Synopsis
This class is used to help reduce the chance of error when dealing with
index values which are generated where indexing is 1-based (Glish), and
used where indexing is 0-based (C++). Typical examples of such indices
include:
- The position of a pixel in an image or an array (use
Vector<Index>).
- The row number of a table (use an Index scalar).
In general, consistent use of Index for all such locations should greatly
reduce the likelihood of off by one errors. The Index will generally appear
as the argument to a member function which is available to the Glish user
through the Tasking system, however it is not coupled to the Tasking system
in any way.
Of course you should only use Index for 0 or 1 relative locations. Actual
"counts" should remain integers.
Example
Suppose we want to work on a Table row which is specified by a user:
void SomeClass::someFunc(Table &table, Index row)
{
ScalarColumn<Float> col(table, "col");
Float x = col(row()); // Index::operator() returns 0-relative value
}
Suppose we wanted to perform some operation on a subsection of an image
defined by a blc and trc which is to be specified by the user. We should
code the function as follows:
void SomeClass::someFunc(ImageInterface<Float> &inout,
const Vector<Index> &blc,
const Vector<Index> &trc)
{
// Turn blc,trc into IPositions
IPosition iblc, itrc;
Index::convertIPosition(iblc, blc);
Index::convertIPosition(itrc, trc);
... perform computation ...
}
Motivation
Encapsulate the indexing difference between Glish (1-based) and
C++ (0-based).
To Do
Member Description
Index(Int zeroRelValue=0) : local_value_p(zeroRelValue)
Index(uInt zeroRelValue) : local_value_p(zeroRelValue)
Construct an index from a zero-relative value. The default is zero, i.e.
the first position of the container.
Index(const Index &other) : local_value_p(other.local_value_p)
Index &operator=(const Index &other)
Make a copy of other.
Return the local, i.e. zero-relative, value.
Return the canonical value, i.e. zeroRelativeValue()+1.
static void convertVector(Vector<Int> &out, const Vector<Index> &in, Bool outValuesAreLocal=True)
static void convertVector(Vector<Index> &out, const Vector<Int> &in, Bool inValuesAreLocal=True)
static void convertIPosition(IPosition &out, const Vector<Index> &in, Bool outValuesAreLocal=True)
static void convertIPosition(Vector<Index> &out, const IPosition &in, Bool inValuesAreLocal=True)
Interconvert between Vector<Index> and IPosition as well as
Vector<Index> and Vector<Int>. If necessary, the
output values are resized. The default is that the
IPosition/Vector<Int> values are in local (0-relative) format,
however this may be changed. Generally this default will only be changed
by the Tasking system itself.