aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/array.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/jtutil/array.hh')
-rw-r--r--src/jtutil/array.hh38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/jtutil/array.hh b/src/jtutil/array.hh
index 0d674bd..fc2060d 100644
--- a/src/jtutil/array.hh
+++ b/src/jtutil/array.hh
@@ -9,8 +9,8 @@
//
// prerequisites:
-// <assert.h>
-// <stdio.h> // only used for NULL
+// <cassert>
+// <cstddef> // for NULL
// "jt/stdc.h"
// "jt/util++.h" // for jtutil::how_many_in_range()
//
@@ -20,15 +20,13 @@
// contiguous arrays, parameterized by the data type (most commonly float
// or double, but could also be bool, int, long double, ...). The
// underlying storage can be either supplied by the caller, or allocated
-// by new[].
+// by new[]. In the latter case, arbitrary strides are also possible.
//
// These arrays cannot be copied or passed to functions by value; use
// pass by reference instead. (This is a feature, not a bug: passing
// large arrays by value would be ++slow, so we don't want to run the
// risk of this happing accidentally.)
//
-// FIXME: Maybe these should be in namespace jtutil::?
-//
//
// Stroustrup ("The C++ Programming Language", 3rd edition, appendix C.7)
@@ -58,6 +56,8 @@
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array1d
{
@@ -114,9 +114,12 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array2d
{
@@ -138,7 +141,7 @@ public:
// FIXME: should we also provide the reverse mapping, i.e.
// subscript --> (i,j) ?
int subscript_unchecked(int i, int j) const
- { return offset_ + stride_i_*i + j; }
+ { return offset_ + stride_i_*i + stride_j_*j; }
int subscript(int i, int j) const
{
assert( is_valid_subscript(i,j) );
@@ -147,7 +150,7 @@ public:
// source line, so an assert() failure message can
// pinpoint *which* index is bad
assert(posn >= 0);
- assert(posn <= N_array_);
+ assert(posn <= max_subscript_);
return posn;
}
@@ -161,15 +164,19 @@ public:
// get access to internal 0-origin 1D storage array
// (low-level, dangerous, use with caution!)
- int N_array() const { return N_array_; }
+ // ... semantics of N_array() may not be what you want
+ // if strides specify noncontiguous storage
+ int N_array() const { return max_subscript_+1; }
fp* get_array() const { return array_; }
// constructor, destructor
- // constructor initializes all array elements to fp(0.0)
+ // ... constructor initializes all array elements to fp(0.0)
+ // ... omitted strides default to C storage order
array2d(int min_i_in, int max_i_in,
int min_j_in, int max_j_in,
- fp *array_in = NULL); // caller-provided storage array
+ fp *array_in = NULL, // caller-provided storage array
// if non-NULL
+ int stride_i_in = 0, int stride_j_in = 0);
~array2d();
private:
@@ -188,20 +195,23 @@ private:
// subscripting info
// n.b. put this next in class so it should be in the same
// cpu cache line as array_ ==> faster array access
- int offset_, stride_i_;
+ int offset_, stride_i_, stride_j_;
// min/max array bounds
- int N_array_;
const int min_i_, max_i_;
const int min_j_, max_j_;
+ int max_subscript_;
// n.b. put this at end of class since performance doesn't matter
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array3d
{
@@ -290,9 +300,12 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array4d
{
@@ -393,3 +406,4 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::