Re: OONSTD: Element Ordering 2D Arrays

Roldan Pozo (pozo@nist.gov)
Thu, 09 Jul 1998 10:32:32 -0400

Geoffrey M. Furnish wrote:

> Okay, in this model you suggest:
> 1) operator[] plus proxies implement 0 based indexing
> 2) operator() implements 1 based indexing
> ???
>

The Template Numerical Toolkit (www.math.nist.gov/tnt) has already done
this for a few years and it has proved rather popular with its users. It turns
out that folks still translate Fortran (1-based) algorithms into C++ by hand,
and being able to refer to the same matrix element as C[0][0] and C(1,1)
inside these codes is a real timesaver. (And, no, you don't have to call any
constructors to switch sides; and, yes, both access methods are equally efficient

as native C code.)

The 0/1 offset issue is one of those perpetual debates in numerical programming
that no side will ever win. I have argued for both camps over the years, until
I decided to just allow for both and be done with it.

> So 1) is familiar to C programmers, and 2) is familiar to Fortran
> programmers. That's cute, but has flaws:
> A) How do you represent arrays with specified bounds? The
> Fortran syntax dimension A(-4:4,-10:10).
> B) Makes for lots of extra work for the container author.

A) You don't. In practice, it's not a big deal.

B) Not really: add two operator() methods (one const and non-const).
If you are concerned with efficiency (I am) add one precomputed M-length
vector of pointers (at timeof construction) to avoid any runtime overhead.

--Roldan