PLaSK library
Loading...
Searching...
No Matches
tnt_fortran_array3d.h
Go to the documentation of this file.
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
32namespace TNT
33{
34
35template <class T>
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
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;
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;
70
71
72};
73
74template <class T>
75Fortran_Array3D<T>::Fortran_Array3D() : v_(), m_(0), n_(0), k_(0), data_(0) {}
76
77
78template <class T>
80 v_(A.v_), m_(A.m_), n_(A.n_), k_(A.k_), data_(A.data_) {}
81
82
83
84template <class T>
86 v_(m*n*k), m_(m), n_(n), k_(k), data_(v_.begin()) {}
87
88
89
90template <class T>
91Fortran_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
98template <class T>
99Fortran_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
105template <class T>
106inline 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
121template <class T>
122inline 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
137template <class T>
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
148template <class T>
150{
151
152 Fortran_Array3D B(m_, n_, k_);
153 B.inject(*this);
154 return B;
155
156}
157
158
159template <class T>
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
176template <class T>
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
191template <class T>
196
197template <class T>
198inline int Fortran_Array3D<T>::dim1() const { return m_; }
199
200template <class T>
201inline int Fortran_Array3D<T>::dim2() const { return n_; }
202
203template <class T>
204inline int Fortran_Array3D<T>::dim3() const { return k_; }
205
206
207template <class T>
209{
210 return v_.ref_count();
211}
212
213template <class T>
217
218
219} /* namespace TNT */
220
221#endif
222/* TNT_FORTRAN_ARRAY3D_H */
223