My Project
Loading...
Searching...
No Matches
tnt_array2d.h
1/*
2*
3* Template Numerical Toolkit (TNT)
4*
5* Mathematical and Computational Sciences Division
6* National Institute of Technology,
7* Gaithersburg, MD USA
8*
9*
10* This software was developed at the National Institute of Standards and
11* Technology (NIST) by employees of the Federal Government in the course
12* of their official duties. Pursuant to title 17 Section 105 of the
13* United States Code, this software is not subject to copyright protection
14* and is in the public domain. NIST assumes no responsibility whatsoever for
15* its use by other parties, and makes no guarantees, expressed or implied,
16* about its quality, reliability, or any other characteristic.
17*
18*/
19
20
21
22#ifndef TNT_ARRAY2D_H
23#define TNT_ARRAY2D_H
24
25#include <cstdlib>
26#include <iostream>
27#ifdef TNT_BOUNDS_CHECK
28#include <assert.h>
29#endif
30
31#include "tnt_array1d.h"
32
33namespace TNT
34{
35
36
63template <class T>
64class Array2D
65{
66
67
68 private:
69
70
71
72 Array1D<T> data_;
73 Array1D<T*> v_;
74 int m_;
75 int n_;
76
77 public:
78
79
80
94 typedef T value_type;
95
102
103
111 Array2D(int m, int n);
112
113
125 Array2D(int m, int n, T *a);
126
127
128
139 Array2D(int m, int n, const T &val);
140
141
148 inline Array2D(const Array2D &A);
149
150
157 inline operator T**();
158
166 inline operator const T**() const;
167
168
174 inline Array2D & operator=(const T &val);
175
176
183 inline Array2D & operator=(const Array2D &A);
184
185
186 inline Array2D & ref(const Array2D &A);
187 Array2D copy() const;
188 Array2D & inject(const Array2D & A);
189 inline T* operator[](int i);
190 inline const T* operator[](int i) const;
191 inline int dim1() const;
192 inline int dim2() const;
193 ~Array2D();
194
195 /* extended interface (not part of the standard) */
196
197
198 inline int ref_count();
199 inline int ref_count_data();
200 inline int ref_count_dim1();
201 Array2D subarray(int i0, int i1, int j0, int j1);
202
203};
204
205
217template <class T>
218Array2D<T>::Array2D() : data_(), v_(), m_(0), n_(0) {}
219
220
221template <class T>
222Array2D<T>::Array2D(const Array2D<T> &A) : data_(A.data_), v_(A.v_),
223 m_(A.m_), n_(A.n_) {}
224
225
226
227
228template <class T>
229Array2D<T>::Array2D(int m, int n) : data_(m*n), v_(m), m_(m), n_(n)
230{
231 if (m>0 && n>0)
232 {
233 T* p = &(data_[0]);
234 for (int i=0; i<m; i++)
235 {
236 v_[i] = p;
237 p += n;
238 }
239 }
240}
241
242
243
244template <class T>
245Array2D<T>::Array2D(int m, int n, const T &val) : data_(m*n), v_(m),
246 m_(m), n_(n)
247{
248 if (m>0 && n>0)
249 {
250 data_ = val;
251 T* p = &(data_[0]);
252 for (int i=0; i<m; i++)
253 {
254 v_[i] = p;
255 p += n;
256 }
257 }
258}
259
260template <class T>
261Array2D<T>::Array2D(int m, int n, T *a) : data_(m*n, a), v_(m), m_(m), n_(n)
262{
263 if (m>0 && n>0)
264 {
265 T* p = &(data_[0]);
266
267 for (int i=0; i<m; i++)
268 {
269 v_[i] = p;
270 p += n;
271 }
272 }
273}
274
275
276template <class T>
277inline T* Array2D<T>::operator[](int i)
278{
279#ifdef TNT_BOUNDS_CHECK
280 assert(i >= 0);
281 assert(i < m_);
282#endif
283
284return v_[i];
285
286}
287
288
289template <class T>
290inline const T* Array2D<T>::operator[](int i) const
291{
292#ifdef TNT_BOUNDS_CHECK
293 assert(i >= 0);
294 assert(i < m_);
295#endif
296
297return v_[i];
298
299}
300
301template <class T>
303{
304 /* non-optimzied, but will work with subarrays in future verions */
305
306 for (int i=0; i<m_; i++)
307 for (int j=0; j<n_; j++)
308 v_[i][j] = a;
309 return *this;
310}
311
312
313
314
315template <class T>
317{
318 Array2D A(m_, n_);
319
320 for (int i=0; i<m_; i++)
321 for (int j=0; j<n_; j++)
322 A[i][j] = v_[i][j];
323
324
325 return A;
326}
327
328
329template <class T>
330Array2D<T> & Array2D<T>::inject(const Array2D &A)
331{
332 if (A.m_ == m_ && A.n_ == n_)
333 {
334 for (int i=0; i<m_; i++)
335 for (int j=0; j<n_; j++)
336 v_[i][j] = A[i][j];
337 }
338 return *this;
339}
340
341
342
343
344template <class T>
345Array2D<T> & Array2D<T>::ref(const Array2D<T> &A)
346{
347 if (this != &A)
348 {
349 v_ = A.v_;
350 data_ = A.data_;
351 m_ = A.m_;
352 n_ = A.n_;
353
354 }
355 return *this;
356}
357
358
359
360template <class T>
362{
363 return ref(A);
364}
365
366template <class T>
367inline int Array2D<T>::dim1() const { return m_; }
368
369template <class T>
370inline int Array2D<T>::dim2() const { return n_; }
371
372
373template <class T>
374Array2D<T>::~Array2D() {}
375
376
377
378
379template <class T>
381{
382 return &(v_[0]);
383}
384template <class T>
385inline Array2D<T>::operator const T**() const
386{
387 return static_cast<const T**>(&(v_[0]));
388}
389
390/* ............... extended interface ............... */
398template <class T>
399Array2D<T> Array2D<T>::subarray(int i0, int i1, int j0, int j1)
400{
401 Array2D<T> A;
402 int m = i1-i0+1;
403 int n = j1-j0+1;
404
405 /* if either length is zero or negative, this is an invalide
406 subarray. return a null view.
407 */
408 if (m<1 || n<1)
409 return A;
410
411 A.data_ = data_;
412 A.m_ = m;
413 A.n_ = n;
414 A.v_ = Array1D<T*>(m);
415 T* p = &(data_[0]) + i0 * n_ + j0;
416 for (int i=0; i<m; i++)
417 {
418 A.v_[i] = p + i*n_;
419
420 }
421 return A;
422}
423
424template <class T>
425inline int Array2D<T>::ref_count()
426{
427 return ref_count_data();
428}
429
430
431
432template <class T>
433inline int Array2D<T>::ref_count_data()
434{
435 return data_.ref_count();
436}
437
438template <class T>
439inline int Array2D<T>::ref_count_dim1()
440{
441 return v_.ref_count();
442}
443
444
445
446
447} /* namespace TNT */
448
449#endif
450/* TNT_ARRAY2D_H */
451
Definition tnt_array1d.h:40
Definition tnt_array2d.h:65
Array2D(const Array2D &A)
Definition tnt_array2d.h:222
T value_type
Definition tnt_array2d.h:94
Array2D & operator=(const Array2D &A)
Definition tnt_array2d.h:361
Array2D(int m, int n, T *a)
Definition tnt_array2d.h:261
Array2D(int m, int n)
Definition tnt_array2d.h:229
Array2D(int m, int n, const T &val)
Definition tnt_array2d.h:245
Array2D()
Definition tnt_array2d.h:218
Array2D & operator=(const T &val)
Definition tnt_array2d.h:302
Array2D subarray(int i0, int i1, int j0, int j1)
Definition tnt_array2d.h:399
Definition tnt_array1d.h:36