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