vbl_array_3d.hxx
Go to the documentation of this file.
1 // This is core/vbl/vbl_array_3d.hxx
2 #ifndef vbl_array_3d_hxx_
3 #define vbl_array_3d_hxx_
4 
5 //:
6 // \file
7 
8 #include <iostream>
9 #include "vbl_array_3d.h"
10 #include <cassert>
11 
12 //--------------------------------------------------------------
13 //
14 // Constructor and Destructor Functions
15 //
16 //--------------------------------------------------------------
17 
18 //: Constructor utility.
19 // This allocates a 3D array which can be referenced using the form myarray[a][b][c].
20 // Useful in C although maybe superfluous here as access is via a get function anyway.
21 template <class T>
23 {
24  row1_count_ = n1;
25  row2_count_ = n2;
26  row3_count_ = n3;
27 
28  // If any of the dimensions are 0, don't allocate memory, just return.
29  if ((n1 * n2 * n3)==0) {
30  element_ = nullptr;
31  return;
32  }
33 
34  // allocate the memory for the first level pointers.
35  element_ = new T** [n1];
36 
37  // set the first level pointers and allocate the memory for the second level pointers.
38  {
39  element_[0] = new T* [n1 * n2];
40  for (size_type row1_index = 0; row1_index < n1; row1_index++)
41  element_ [row1_index] = element_[0] + n2 * row1_index;
42  }
43 
44  T* array_ptr = new T [n1*n2*n3];
45 
46  // set the second level pointers.
47  for (size_type row1_index = 0; row1_index < n1; row1_index++)
48  for (size_type row2_index = 0; row2_index < n2; row2_index++) {
49  element_ [row1_index][row2_index] = array_ptr;
50  array_ptr += n3;
51  }
52 }
53 
54 template <class T>
56 {
57  if (element_) {
58  // remove the actual members.
59  delete [] element_ [0][0];
60 
61  // remove the second level pointers.
62  delete [] element_ [0];
63 
64  // remove the first level pointers.
65  delete [] element_;
66  }
67 }
68 
69 template <class T>
71 {
72  if (n1 == row1_count_ && n2 == row2_count_ && n3 == row3_count_)
73  return;
74  destruct();
75  construct(n1, n2, n3);
76 }
77 
78 //: Fill from static array of Ts.
79 // The final index fills fastest, so if we consider the tensor as a set of
80 // matrices (M[i])[j][k] then the matrices are filled in the usual C order.
81 template <class T>
82 void vbl_array_3d<T>::set(T const* p)
83 {
84  for (size_type row1_index = 0; row1_index < row1_count_; row1_index++)
85  for (size_type row2_index = 0; row2_index < row2_count_; row2_index++)
86  for (size_type row3_index = 0; row3_index < row3_count_; row3_index++)
87  element_ [row1_index][row2_index][row3_index] = *p++;
88 }
89 
90 //: Get into array
91 template <class T>
92 void vbl_array_3d<T>::get(T* p) const
93 {
94  for (size_type row1_index = 0; row1_index < row1_count_; row1_index++)
95  for (size_type row2_index = 0; row2_index < row2_count_; row2_index++)
96  for (size_type row3_index = 0; row3_index < row3_count_; row3_index++)
97  *p++ = element_ [row1_index][row2_index][row3_index];
98 }
99 
100 //: Fill with constant
101 template <class T>
102 void vbl_array_3d<T>::fill(T const& value)
103 {
104  size_type n = row1_count_ * row2_count_ * row3_count_;
105  T* d = data_block();
106  T* e = d + n;
107  while (d < e)
108  *d++ = value;
109 }
110 
111 //--------------------------------------------------------------------------------
112 
113 #ifdef _MSC_VER
114 # include <vcl_msvc_warnings.h>
115 #endif
116 
117 template <class T>
118 std::ostream & operator<<(std::ostream& os, vbl_array_3d<T> const& A)
119 {
120  typedef typename vbl_array_3d<T>::size_type size_type;
121  os << "vbl_array_3d [";
122  for (size_type i=0; i<A.get_row1_count(); ++i) {
123  os << std::endl << " <" << i << '>';
124  for (size_type j=0; j<A.get_row2_count(); ++j) {
125  os << std::endl << " ";
126  for (size_type k=0; k<A.get_row3_count(); ++k) {
127  os << ' ' << A(i,j,k);
128  }
129  }
130  }
131  os << "\n ]" << std::endl;
132  return os;
133 }
134 
135 template <class T>
136 std::istream & operator>>(std::istream& is, vbl_array_3d<T>& A)
137 {
138  typedef typename vbl_array_3d<T>::size_type size_type;
139  for (size_type i=0; i<A.get_row1_count(); ++i)
140  for (size_type j=0; j<A.get_row2_count(); ++j)
141  for (size_type k=0; k<A.get_row3_count(); ++k)
142  is >> A(i,j,k);
143  return is;
144 }
145 
146 //--------------------------------------------------------------------------------
147 
148 #undef VBL_ARRAY_3D_INSTANTIATE
149 #define VBL_ARRAY_3D_INSTANTIATE(T) template class vbl_array_3d<T >
150 
151 #undef VBL_ARRAY_3D_IO_INSTANTIATE
152 #define VBL_ARRAY_3D_IO_INSTANTIATE(T) \
153 template std::ostream & operator<<(std::ostream &,vbl_array_3d<T > const &); \
154 template std::istream & operator>>(std::istream &,vbl_array_3d<T > &)
155 
156 #endif // vbl_array_3d_hxx_
void resize(size_type n1, size_type n2, size_type n3)
std::ostream & operator<<(std::ostream &os, vbl_array_3d< T > const &A)
void set(T const *array)
Fill from static array of Ts.
Contains class for templated 3d array.
Templated 3-dimensional array.
Definition: vbl_array_3d.h:38
void fill(T const &value)
Fill with constant.
void construct(size_type, size_type, size_type)
Constructor utility.
size_type get_row1_count() const
Definition: vbl_array_3d.h:127
void get(T *array) const
Get into array.
std::size_t size_type
Definition: vbl_array_3d.h:41