PLaSK library
Loading...
Searching...
No Matches
tnt_fortran_array2d.h
Go to the documentation of this file.
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
34namespace TNT
35{
36
37template <class T>
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
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;
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;
72
73 /* extended interface */
74
75 inline int ref_count() const;
76
77};
78
79template <class T>
80Fortran_Array2D<T>::Fortran_Array2D() : v_(), m_(0), n_(0), data_(0) {}
81
82
83template <class T>
85 m_(A.m_), n_(A.n_), data_(A.data_) {}
86
87
88
89template <class T>
90Fortran_Array2D<T>::Fortran_Array2D(int m, int n) : v_(m*n), m_(m), n_(n),
91 data_(v_.begin()) {}
92
93template <class T>
94Fortran_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
101template <class T>
102Fortran_Array2D<T>::Fortran_Array2D(int m, int n, T *a) : v_(a),
103 m_(m), n_(n), data_(v_.begin()) {}
104
105
106
107
108template <class T>
109inline 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
122template <class T>
123inline 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
137template <class T>
139{
140 set_(data_, data_+m_*n_, a);
141 return *this;
142}
143
144template <class T>
146{
147
148 Fortran_Array2D B(m_,n_);
149
150 B.inject(*this);
151 return B;
152}
153
154
155template <class T>
157{
158 if (m_ == A.m_ && n_ == A.n_)
159 copy_(data_, A.data_, m_*n_);
160
161 return *this;
162}
163
164
165
166template <class T>
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
179template <class T>
184
185template <class T>
186inline int Fortran_Array2D<T>::dim1() const { return m_; }
187
188template <class T>
189inline int Fortran_Array2D<T>::dim2() const { return n_; }
190
191
192template <class T>
196
197template <class T>
198inline int Fortran_Array2D<T>::ref_count() const { return v_.ref_count(); }
199
200
201
202
203template <class T>
204void 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
211template <class T>
212void 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