1 | /* |
---|
2 | * |
---|
3 | * Template Numerical Toolkit (TNT): Three-dimensional Fortran numerical array |
---|
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_FORTRAN_ARRAY3D_H |
---|
23 | #define TNT_FORTRAN_ARRAY3D_H |
---|
24 | |
---|
25 | #include <cstdlib> |
---|
26 | #include <iostream> |
---|
27 | #ifdef TNT_BOUNDS_CHECK |
---|
28 | #include <assert.h> |
---|
29 | #endif |
---|
30 | #include "tnt_i_refvec.h" |
---|
31 | |
---|
32 | namespace TNT |
---|
33 | { |
---|
34 | |
---|
35 | template <class T> |
---|
36 | class Fortran_Array3D |
---|
37 | { |
---|
38 | |
---|
39 | |
---|
40 | private: |
---|
41 | |
---|
42 | |
---|
43 | i_refvec<T> v_; |
---|
44 | int m_; |
---|
45 | int n_; |
---|
46 | int k_; |
---|
47 | T* data_; |
---|
48 | |
---|
49 | public: |
---|
50 | |
---|
51 | typedef T value_type; |
---|
52 | |
---|
53 | Fortran_Array3D(); |
---|
54 | Fortran_Array3D(int m, int n, int k); |
---|
55 | Fortran_Array3D(int m, int n, int k, T *a); |
---|
56 | Fortran_Array3D(int m, int n, int k, const T &a); |
---|
57 | inline Fortran_Array3D(const Fortran_Array3D &A); |
---|
58 | inline Fortran_Array3D & operator=(const T &a); |
---|
59 | inline Fortran_Array3D & operator=(const Fortran_Array3D &A); |
---|
60 | inline Fortran_Array3D & ref(const Fortran_Array3D &A); |
---|
61 | Fortran_Array3D copy() const; |
---|
62 | Fortran_Array3D & inject(const Fortran_Array3D & A); |
---|
63 | inline T& operator()(int i, int j, int k); |
---|
64 | inline const T& operator()(int i, int j, int k) const ; |
---|
65 | inline int dim1() const; |
---|
66 | inline int dim2() const; |
---|
67 | inline int dim3() const; |
---|
68 | inline int ref_count() const; |
---|
69 | ~Fortran_Array3D(); |
---|
70 | |
---|
71 | |
---|
72 | }; |
---|
73 | |
---|
74 | template <class T> |
---|
75 | Fortran_Array3D<T>::Fortran_Array3D() : v_(), m_(0), n_(0), k_(0), data_(0) {} |
---|
76 | |
---|
77 | |
---|
78 | template <class T> |
---|
79 | Fortran_Array3D<T>::Fortran_Array3D(const Fortran_Array3D<T> &A) : |
---|
80 | v_(A.v_), m_(A.m_), n_(A.n_), k_(A.k_), data_(A.data_) {} |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | template <class T> |
---|
85 | Fortran_Array3D<T>::Fortran_Array3D(int m, int n, int k) : |
---|
86 | v_(m*n*k), m_(m), n_(n), k_(k), data_(v_.begin()) {} |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | template <class T> |
---|
91 | Fortran_Array3D<T>::Fortran_Array3D(int m, int n, int k, const T &val) : |
---|
92 | v_(m*n*k), m_(m), n_(n), k_(k), data_(v_.begin()) |
---|
93 | { |
---|
94 | for (T* p = data_; p < data_ + m*n*k; p++) |
---|
95 | *p = val; |
---|
96 | } |
---|
97 | |
---|
98 | template <class T> |
---|
99 | Fortran_Array3D<T>::Fortran_Array3D(int m, int n, int k, T *a) : |
---|
100 | v_(a), m_(m), n_(n), k_(k), data_(v_.begin()) {} |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | template <class T> |
---|
106 | inline T& Fortran_Array3D<T>::operator()(int i, int j, int k) |
---|
107 | { |
---|
108 | #ifdef TNT_BOUNDS_CHECK |
---|
109 | assert(i >= 1); |
---|
110 | assert(i <= m_); |
---|
111 | assert(j >= 1); |
---|
112 | assert(j <= n_); |
---|
113 | assert(k >= 1); |
---|
114 | assert(k <= k_); |
---|
115 | #endif |
---|
116 | |
---|
117 | return data_[(k-1)*m_*n_ + (j-1) * m_ + i-1]; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | template <class T> |
---|
122 | inline const T& Fortran_Array3D<T>::operator()(int i, int j, int k) const |
---|
123 | { |
---|
124 | #ifdef TNT_BOUNDS_CHECK |
---|
125 | assert(i >= 1); |
---|
126 | assert(i <= m_); |
---|
127 | assert(j >= 1); |
---|
128 | assert(j <= n_); |
---|
129 | assert(k >= 1); |
---|
130 | assert(k <= k_); |
---|
131 | #endif |
---|
132 | |
---|
133 | return data_[(k-1)*m_*n_ + (j-1) * m_ + i-1]; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | template <class T> |
---|
138 | Fortran_Array3D<T> & Fortran_Array3D<T>::operator=(const T &a) |
---|
139 | { |
---|
140 | |
---|
141 | T *end = data_ + m_*n_*k_; |
---|
142 | |
---|
143 | for (T *p=data_; p != end; *p++ = a); |
---|
144 | |
---|
145 | return *this; |
---|
146 | } |
---|
147 | |
---|
148 | template <class T> |
---|
149 | Fortran_Array3D<T> Fortran_Array3D<T>::copy() const |
---|
150 | { |
---|
151 | |
---|
152 | Fortran_Array3D B(m_, n_, k_); |
---|
153 | B.inject(*this); |
---|
154 | return B; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | template <class T> |
---|
160 | Fortran_Array3D<T> & Fortran_Array3D<T>::inject(const Fortran_Array3D &A) |
---|
161 | { |
---|
162 | |
---|
163 | if (m_ == A.m_ && n_ == A.n_ && k_ == A.k_) |
---|
164 | { |
---|
165 | T *p = data_; |
---|
166 | T *end = data_ + m_*n_*k_; |
---|
167 | const T* q = A.data_; |
---|
168 | for (; p < end; *p++ = *q++); |
---|
169 | } |
---|
170 | return *this; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | template <class T> |
---|
177 | Fortran_Array3D<T> & Fortran_Array3D<T>::ref(const Fortran_Array3D<T> &A) |
---|
178 | { |
---|
179 | |
---|
180 | if (this != &A) |
---|
181 | { |
---|
182 | v_ = A.v_; |
---|
183 | m_ = A.m_; |
---|
184 | n_ = A.n_; |
---|
185 | k_ = A.k_; |
---|
186 | data_ = A.data_; |
---|
187 | } |
---|
188 | return *this; |
---|
189 | } |
---|
190 | |
---|
191 | template <class T> |
---|
192 | Fortran_Array3D<T> & Fortran_Array3D<T>::operator=(const Fortran_Array3D<T> &A) |
---|
193 | { |
---|
194 | return ref(A); |
---|
195 | } |
---|
196 | |
---|
197 | template <class T> |
---|
198 | inline int Fortran_Array3D<T>::dim1() const { return m_; } |
---|
199 | |
---|
200 | template <class T> |
---|
201 | inline int Fortran_Array3D<T>::dim2() const { return n_; } |
---|
202 | |
---|
203 | template <class T> |
---|
204 | inline int Fortran_Array3D<T>::dim3() const { return k_; } |
---|
205 | |
---|
206 | |
---|
207 | template <class T> |
---|
208 | inline int Fortran_Array3D<T>::ref_count() const |
---|
209 | { |
---|
210 | return v_.ref_count(); |
---|
211 | } |
---|
212 | |
---|
213 | template <class T> |
---|
214 | Fortran_Array3D<T>::~Fortran_Array3D() |
---|
215 | { |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | } /* namespace TNT */ |
---|
220 | |
---|
221 | #endif |
---|
222 | /* TNT_FORTRAN_ARRAY3D_H */ |
---|
223 | |
---|