Essentia  2.1-beta6-dev
tnt_array3d.h
Go to the documentation of this file.
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_ARRAY3D_H
23 #define TNT_ARRAY3D_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 #include "tnt_array2d.h"
33 
34 namespace TNT
35 {
36 
37 template <class T>
38 class Array3D
39 {
40 
41 
42  private:
45  int m_;
46  int n_;
47  int g_;
48 
49 
50  public:
51 
52  typedef T value_type;
53 
54  Array3D();
55  Array3D(int m, int n, int g);
56  Array3D(int m, int n, int g, T val);
57  Array3D(int m, int n, int g, T *a);
58 
59  inline operator T***();
60  inline operator const T***();
61  inline Array3D(const Array3D &A);
62  inline Array3D & operator=(const T &a);
63  inline Array3D & operator=(const Array3D &A);
64  inline Array3D & ref(const Array3D &A);
65  Array3D copy() const;
66  Array3D & inject(const Array3D & A);
67 
68  inline T** operator[](int i);
69  inline const T* const * operator[](int i) const;
70  inline int dim1() const;
71  inline int dim2() const;
72  inline int dim3() const;
73  ~Array3D();
74 
75  /* extended interface */
76 
77  inline int ref_count(){ return data_.ref_count(); }
78  Array3D subarray(int i0, int i1, int j0, int j1,
79  int k0, int k1);
80 };
81 
82 template <class T>
83 Array3D<T>::Array3D() : data_(), v_(), m_(0), n_(0) {}
84 
85 template <class T>
86 Array3D<T>::Array3D(const Array3D<T> &A) : data_(A.data_),
87  v_(A.v_), m_(A.m_), n_(A.n_), g_(A.g_)
88 {
89 }
90 
91 
92 
93 template <class T>
94 Array3D<T>::Array3D(int m, int n, int g) : data_(m*n*g), v_(m,n),
95  m_(m), n_(n), g_(g)
96 {
97 
98  if (m>0 && n>0 && g>0)
99  {
100  T* p = & (data_[0]);
101  int ng = n_*g_;
102 
103  for (int i=0; i<m_; i++)
104  {
105  T* ping = p+ i*ng;
106  for (int j=0; j<n; j++)
107  v_[i][j] = ping + j*g_;
108  }
109  }
110 }
111 
112 
113 
114 template <class T>
115 Array3D<T>::Array3D(int m, int n, int g, T val) : data_(m*n*g, val),
116  v_(m,n), m_(m), n_(n), g_(g)
117 {
118  if (m>0 && n>0 && g>0)
119  {
120 
121  T* p = & (data_[0]);
122  int ng = n_*g_;
123 
124  for (int i=0; i<m_; i++)
125  {
126  T* ping = p+ i*ng;
127  for (int j=0; j<n; j++)
128  v_[i][j] = ping + j*g_;
129  }
130  }
131 }
132 
133 
134 
135 template <class T>
136 Array3D<T>::Array3D(int m, int n, int g, T* a) :
137  data_(m*n*g, a), v_(m,n), m_(m), n_(n), g_(g)
138 {
139 
140  if (m>0 && n>0 && g>0)
141  {
142  T* p = & (data_[0]);
143  int ng = n_*g_;
144 
145  for (int i=0; i<m_; i++)
146  {
147  T* ping = p+ i*ng;
148  for (int j=0; j<n; j++)
149  v_[i][j] = ping + j*g_;
150  }
151  }
152 }
153 
154 
155 
156 template <class T>
157 inline T** Array3D<T>::operator[](int i)
158 {
159 #ifdef TNT_BOUNDS_CHECK
160  assert(i >= 0);
161  assert(i < m_);
162 #endif
163 
164 return v_[i];
165 
166 }
167 
168 template <class T>
169 inline const T* const * Array3D<T>::operator[](int i) const
170 { return v_[i]; }
171 
172 template <class T>
174 {
175  for (int i=0; i<m_; i++)
176  for (int j=0; j<n_; j++)
177  for (int k=0; k<g_; k++)
178  v_[i][j][k] = a;
179 
180  return *this;
181 }
182 
183 template <class T>
185 {
186  Array3D A(m_, n_, g_);
187  for (int i=0; i<m_; i++)
188  for (int j=0; j<n_; j++)
189  for (int k=0; k<g_; k++)
190  A.v_[i][j][k] = v_[i][j][k];
191 
192  return A;
193 }
194 
195 
196 template <class T>
198 {
199  if (A.m_ == m_ && A.n_ == n_ && A.g_ == g_)
200 
201  for (int i=0; i<m_; i++)
202  for (int j=0; j<n_; j++)
203  for (int k=0; k<g_; k++)
204  v_[i][j][k] = A.v_[i][j][k];
205 
206  return *this;
207 }
208 
209 
210 
211 template <class T>
213 {
214  if (this != &A)
215  {
216  m_ = A.m_;
217  n_ = A.n_;
218  g_ = A.g_;
219  v_ = A.v_;
220  data_ = A.data_;
221  }
222  return *this;
223 }
224 
225 template <class T>
227 {
228  return ref(A);
229 }
230 
231 
232 template <class T>
233 inline int Array3D<T>::dim1() const { return m_; }
234 
235 template <class T>
236 inline int Array3D<T>::dim2() const { return n_; }
237 
238 template <class T>
239 inline int Array3D<T>::dim3() const { return g_; }
240 
241 
242 
243 template <class T>
245 
246 template <class T>
247 inline Array3D<T>::operator T***()
248 {
249  return v_;
250 }
251 
252 
253 template <class T>
254 inline Array3D<T>::operator const T***()
255 {
256  return v_;
257 }
258 
259 /* extended interface */
260 template <class T>
261 Array3D<T> Array3D<T>::subarray(int i0, int i1, int j0,
262  int j1, int k0, int k1)
263 {
264 
265  /* check that ranges are valid. */
266  if (!( 0 <= i0 && i0 <= i1 && i1 < m_ &&
267  0 <= j0 && j0 <= j1 && j1 < n_ &&
268  0 <= k0 && k0 <= k1 && k1 < g_))
269  return Array3D<T>(); /* null array */
270 
271 
272  Array3D<T> A;
273  A.data_ = data_;
274  A.m_ = i1-i0+1;
275  A.n_ = j1-j0+1;
276  A.g_ = k1-k0+1;
277  A.v_ = Array2D<T*>(A.m_,A.n_);
278  T* p = &(data_[0]) + i0*n_*g_ + j0*g_ + k0;
279 
280  for (int i=0; i<A.m_; i++)
281  {
282  T* ping = p + i*n_*g_;
283  for (int j=0; j<A.n_; j++)
284  A.v_[i][j] = ping + j*g_ ;
285  }
286 
287  return A;
288 }
289 
290 
291 
292 } /* namespace TNT */
293 
294 #endif
295 /* TNT_ARRAY3D_H */
296 
Definition: tnt_array1d.h:40
Definition: tnt_array3d.h:39
Array3D & inject(const Array3D &A)
Definition: tnt_array3d.h:197
Array3D & operator=(const Array3D &A)
int dim3() const
Definition: tnt_array3d.h:239
Array3D()
Definition: tnt_array3d.h:83
Array3D copy() const
Definition: tnt_array3d.h:184
T value_type
Definition: tnt_array3d.h:52
int dim1() const
Definition: tnt_array3d.h:233
Array3D & ref(const Array3D &A)
Definition: tnt_array3d.h:212
Array2D< T * > v_
Definition: tnt_array3d.h:44
int g_
Definition: tnt_array3d.h:47
T ** operator[](int i)
Definition: tnt_array3d.h:157
int ref_count()
Definition: tnt_array3d.h:77
Array3D & operator=(const T &a)
Definition: tnt_array3d.h:173
int m_
Definition: tnt_array3d.h:45
int dim2() const
Definition: tnt_array3d.h:236
Array3D subarray(int i0, int i1, int j0, int j1, int k0, int k1)
Definition: tnt_array3d.h:261
Array3D(const Array3D &A)
Array1D< T > data_
Definition: tnt_array3d.h:43
int n_
Definition: tnt_array3d.h:46
~Array3D()
Definition: tnt_array3d.h:244
Definition: tnt_array1d.h:36