aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/array.hh
blob: cc3349d0887c842e5c39801ac51a87c335381392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// array.hh -- array template classes
// $Id$
//
// array1d - 1D array template
// array2d - 2D array template
// array3d - 3D array template
// array4d - 4D array template
//

//
// prerequisites:
//    <assert.h>
//    "jt/stdc.h"
//    "jt/util++.h"	// for jtutil::how_many_in_range()
//

//
// The templates defined in this file represent n-dimensional row-major
// contiguous arrays, parameterized by the data type (most commonly float
// or double, but could also be bool, int, long double, ...).  These
// arrays cannot be copied or passed to functions by value; use pass
// by reference instead.
//
// Stroustrup ("The C++ Programming Language", 3rd edition, appendix C.7)
// suggests the use of STL vectors of STL vectors to provide multidimensional
// arrays.  However, those "arrays" aren't contiguous in memory, so the
// compiler can't do strength reduction on any but the last subscript
// when the arrays are accessed in a loop.  In contrast, the arrays
// defined here are contiguous, and all subscripts can (should, if the
// compiler is good) be strength-reduced.
//
// The STL valarray templates offer a superset of the functionality of
// these templates, but they've only recently been provided with gcc;
// at some time in the future I may migrate these classes to become
// valarray wrappers.
//

#ifndef NDEBUG
//
// Full bounds checking is done on all array accesses.
//
#endif

//******************************************************************************

namespace jtutil
	  {
template <class fpt>
class	array1d
	{
public:
	// array info
	int min_i() const { return min_i_; }
	int max_i() const { return max_i_; }
	int N_i() const { return jtutil::how_many_in_range(min_i_, max_i_); }
	bool is_valid_i(int i) const { return (i >= min_i_) && (i <= max_i_); }
	bool is_valid_subscript(int i) const { return is_valid_i(i); }

	// normal-use access functions
	// ... rvalue
	fpt operator()(int i) const
		{
		assert( is_valid_subscript(i) );
		return array_[i - min_i_];
		}
	// ... lvalue
	fpt& operator()(int i)
		{
		assert( is_valid_subscript(i) );
		return array_[i - min_i_];
		}

	// get access to internal 0-origin 1D storage array
	// (low-level, dangerous, use with caution!)
	int N_array() const { return N_i(); }
	fpt* get_array() const { return array_; }

	// constructor, destructor
	// newly-constructed array elements are initialized to fpt(0)
	array1d(int min_i_in, int max_i_in);
	~array1d();

private:
	// we forbid copying and passing by value
	// by declaring the copy constructor and assignment operator
	// private, but never defining them
	array1d(const array1d<fpt>& rhs);
	array1d<fpt>& operator=(const array1d<fpt>& rhs);

private:
	// note we declare the array pointer first in the class
	// ==> it's probably at 0 offset
	// ==> we may get slightly faster array access
	fpt* array_;		// --> new-allocated 1D storage array

	// min/max array bounds
	const int min_i_, max_i_;
	};
	  };	// close namespace jtutil::

//******************************************************************************

namespace jtutil
	  {
template <class fpt>
class	array2d
	{
public:
	// array info
	int min_i() const { return min_i_; }
	int max_i() const { return max_i_; }
	int min_j() const { return min_j_; }
	int max_j() const { return max_j_; }
	int N_i() const { return jtutil::how_many_in_range(min_i_, max_i_); }
	int N_j() const { return jtutil::how_many_in_range(min_j_, max_j_); }
	bool is_valid_i(int i) const { return (i >= min_i_) && (i <= max_i_); }
	bool is_valid_j(int j) const { return (j >= min_j_) && (j <= max_j_); }
	bool is_valid_subscript(int i, int j) const
		{ return is_valid_i(i) && is_valid_j(j); }

	// subscripting functions
	// (low-level, dangerous, use with caution!)
	// 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; }
	int subscript(int i, int j) const
		{
		assert( is_valid_subscript(i,j) );
		const int posn = subscript_unchecked(i,j);
		// we want each assert() here to be on a separate source line,
		// so assert() failure message can pinpoint which index is bad
		assert(posn >= 0);
		assert(posn <= N_array_);
		return posn;
		}

	// normal-use access functions
	// ... rvalue
	fpt  operator()(int i, int j) const
		{ return array_[ subscript(i,j) ]; }
	// ... lvalue
	fpt& operator()(int i, int j)
		{ return array_[ subscript(i,j) ]; }

	// get access to internal 0-origin 1D storage array
	// (low-level, dangerous, use with caution!)
	int N_array() const { return N_array_; }
	fpt* get_array() const { return array_; }

	// constructor, destructor
	// newly-constructed array elements are initialized to fpt(0)
	array2d(int min_i_in, int max_i_in,
		int min_j_in, int max_j_in);
	~array2d();

private:
	// we forbid copying and passing by value
	// by declaring the copy constructor and assignment operator
	// private, but never defining them
	array2d(const array2d<fpt>& rhs);
	array2d<fpt>& operator=(const array2d<fpt>& rhs);

private:
	// note we declare the array pointer first in the class
	// ==> it's probably at 0 offset
	// ==> we may get slightly faster array access
	fpt* array_;		// --> new-allocated 1D storage array
	int N_array_;

	// subscripting info
	int offset_, stride_i_;

	// min/max array bounds
	const int min_i_, max_i_;
	const int min_j_, max_j_;
	};
	  };	// close namespace jtutil::

//******************************************************************************

namespace jtutil
	  {
template <class fpt>
class	array3d
	{
public:
	// array info
	int min_i() const { return min_i_; }
	int max_i() const { return max_i_; }
	int min_j() const { return min_j_; }
	int max_j() const { return max_j_; }
	int min_k() const { return min_k_; }
	int max_k() const { return max_k_; }
	int N_i() const { return jtutil::how_many_in_range(min_i_, max_i_); }
	int N_j() const { return jtutil::how_many_in_range(min_j_, max_j_); }
	int N_k() const { return jtutil::how_many_in_range(min_k_, max_k_); }
	bool is_valid_i(int i) const { return (i >= min_i_) && (i <= max_i_); }
	bool is_valid_j(int j) const { return (j >= min_j_) && (j <= max_j_); }
	bool is_valid_k(int k) const { return (k >= min_k_) && (k <= max_k_); }
	bool is_valid_subscript(int i, int j, int k) const
		{ return is_valid_i(i) && is_valid_j(j) && is_valid_k(k); }

	// subscripting functions
	// (low-level, dangerous, use with caution!)
	// FIXME: should we also provide the reverse mapping, i.e.
	//        subscript --> (i,j,k) ?
	int subscript_unchecked(int i, int j, int k) const
		{ return offset_ + stride_i_*i + stride_j_*j + k; }
	int subscript(int i, int j, int k) const
		{
		assert( is_valid_subscript(i,j,k) );
		const int posn = subscript_unchecked(i,j,k);
		// we want each assert() here to be on a separate source line,
		// so assert() failure message can pinpoint which index is bad
		assert(posn >= 0);
		assert(posn <= N_array_);
		return posn;
		}

	// normal-use access functions
	// ... rvalue
	fpt  operator()(int i, int j, int k) const
		{ return array_[ subscript(i,j,k) ]; }
	// ... lvalue
	fpt& operator()(int i, int j, int k)
		{ return array_[ subscript(i,j,k) ]; }

	// get access to internal 0-origin 1D storage array
	// (low-level, dangerous, use with caution!)
	int N_array() const { return N_array_; }
	fpt* get_array() const { return array_; }

	// constructor, destructor
	// newly-constructed array elements are initialized to fpt(0)
	array3d(int min_i_in, int max_i_in,
		int min_j_in, int max_j_in,
		int min_k_in, int max_k_in);
	~array3d();

private:
	// we forbid copying and passing by value
	// by declaring the copy constructor and assignment operator
	// private, but never defining them
	array3d(const array3d<fpt>& rhs);
	array3d<fpt>& operator=(const array3d<fpt>& rhs);

private:
	// note we declare the array pointer first in the class
	// ==> it's probably at 0 offset
	// ==> we may get slightly faster array access
	fpt* array_;		// --> new-allocated 1D storage array
	int N_array_;

	// subscripting info
	int offset_, stride_i_, stride_j_;

	// min/max array bounds
	const int min_i_, max_i_;
	const int min_j_, max_j_;
	const int min_k_, max_k_;
	};
	  };	// close namespace jtutil::

//******************************************************************************

namespace jtutil
	  {
template <class fpt>
class	array4d
	{
public:
	// array info
	int min_i() const { return min_i_; }
	int max_i() const { return max_i_; }
	int min_j() const { return min_j_; }
	int max_j() const { return max_j_; }
	int min_k() const { return min_k_; }
	int max_k() const { return max_k_; }
	int min_l() const { return min_l_; }
	int max_l() const { return max_l_; }
	int N_i() const { return jtutil::how_many_in_range(min_i_, max_i_); }
	int N_j() const { return jtutil::how_many_in_range(min_j_, max_j_); }
	int N_k() const { return jtutil::how_many_in_range(min_k_, max_k_); }
	int N_l() const { return jtutil::how_many_in_range(min_l_, max_l_); }
	bool is_valid_i(int i) const { return (i >= min_i_) && (i <= max_i_); }
	bool is_valid_j(int j) const { return (j >= min_j_) && (j <= max_j_); }
	bool is_valid_k(int k) const { return (k >= min_k_) && (k <= max_k_); }
	bool is_valid_l(int l) const { return (l >= min_l_) && (l <= max_l_); }
	bool is_valid_subscript(int i, int j, int k, int l)
		const
		{
		return    is_valid_i(i) && is_valid_j(j)
		       && is_valid_k(k) && is_valid_l(l);
		}

	// subscripting functions
	// (low-level, dangerous, use with caution!)
	// FIXME: should we also provide the reverse mapping, i.e.
	//        subscript --> (i,j,k,l) ?
	int subscript_unchecked(int i, int j, int k, int l) const
		{
		return offset_ + stride_i_*i + stride_j_*j + stride_k_*k + l;
		}
	int subscript(int i, int j, int k, int l) const
		{
		assert( is_valid_subscript(i,j,k,l) );
		const int posn = subscript_unchecked(i,j,k,l);
		// we want each assert() here to be on a separate source line,
		// so assert() failure message can pinpoint which index is bad
		assert(posn >= 0);
		assert(posn <= N_array_);
		return posn;
		}

	// normal-use access functions
	// ... rvalue
	fpt  operator()(int i, int j, int k, int l) const
		{ return array_[ subscript(i,j,k,l) ]; }
	// ... lvalue
	fpt& operator()(int i, int j, int k, int l)
		{ return array_[ subscript(i,j,k,l) ]; }

	// get access to internal 0-origin 1D storage array
	// (low-level, dangerous, use with caution!)
	int N_array() const { return N_array_; }
	fpt* get_array() const { return array_; }

	// constructor, destructor
	// newly-constructed array elements are initialized to fpt(0)
	array4d(int min_i_in, int max_i_in,
		int min_j_in, int max_j_in,
		int min_k_in, int max_k_in,
		int min_l_in, int max_l_in);
	~array4d();

private:
	// we forbid copying and passing by value
	// by declaring the copy constructor and assignment operator
	// private, but never defining them
	array4d(const array4d<fpt>& rhs);
	array4d<fpt>& operator=(const array4d<fpt>& rhs);

private:
	// note we declare the array pointer first in the class
	// ==> it's probably at 0 offset
	// ==> we may get slightly faster array access
	fpt* array_;		// --> new-allocated 1D storage array
	int N_array_;

	// subscripting info
	int offset_, stride_i_, stride_j_, stride_k_;

	// min/max array bounds
	const int min_i_, max_i_;
	const int min_j_, max_j_;
	const int min_k_, max_k_;
	const int min_l_, max_l_;
	};
	  };	// close namespace jtutil::