Essentia  2.1-beta6-dev
tnt_fortran_array1d.h
Go to the documentation of this file.
1 /*
2 *
3 * Template Numerical Toolkit (TNT)
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_ARRAY1D_H
23 #define TNT_FORTRAN_ARRAY1D_H
24 
25 #include <cstdlib>
26 #include <iostream>
27 
28 #ifdef TNT_BOUNDS_CHECK
29 #include <assert.h>
30 #endif
31 
32 
33 #include "tnt_i_refvec.h"
34 
35 namespace TNT
36 {
37 
38 template <class T>
40 {
41 
42  private:
43 
45  int n_;
46  T* data_; /* this normally points to v_.begin(), but
47  * could also point to a portion (subvector)
48  * of v_.
49  */
50 
51  void initialize_(int n);
52  void copy_(T* p, const T* q, int len) const;
53  void set_(T* begin, T* end, const T& val);
54 
55 
56  public:
57 
58  typedef T value_type;
59 
60 
62  explicit Fortran_Array1D(int n);
63  Fortran_Array1D(int n, const T &a);
64  Fortran_Array1D(int n, T *a);
65  inline Fortran_Array1D(const Fortran_Array1D &A);
66  inline Fortran_Array1D & operator=(const T &a);
68  inline Fortran_Array1D & ref(const Fortran_Array1D &A);
69  Fortran_Array1D copy() const;
71  inline T& operator()(int i);
72  inline const T& operator()(int i) const;
73  inline int dim1() const;
74  inline int dim() const;
76 
77 
78  /* ... extended interface ... */
79 
80  inline int ref_count() const;
81  inline Fortran_Array1D<T> subarray(int i0, int i1);
82 
83 };
84 
85 
86 
87 
88 template <class T>
89 Fortran_Array1D<T>::Fortran_Array1D() : v_(), n_(0), data_(0) {}
90 
91 template <class T>
92 Fortran_Array1D<T>::Fortran_Array1D(const Fortran_Array1D<T> &A) : v_(A.v_), n_(A.n_),
93  data_(A.data_)
94 {
95 #ifdef TNT_DEBUG
96  std::cout << "Created Fortran_Array1D(const Fortran_Array1D<T> &A) \n";
97 #endif
98 
99 }
100 
101 
102 template <class T>
103 Fortran_Array1D<T>::Fortran_Array1D(int n) : v_(n), n_(n), data_(v_.begin())
104 {
105 #ifdef TNT_DEBUG
106  std::cout << "Created Fortran_Array1D(int n) \n";
107 #endif
108 }
109 
110 template <class T>
111 Fortran_Array1D<T>::Fortran_Array1D(int n, const T &val) : v_(n), n_(n), data_(v_.begin())
112 {
113 #ifdef TNT_DEBUG
114  std::cout << "Created Fortran_Array1D(int n, const T& val) \n";
115 #endif
116  set_(data_, data_+ n, val);
117 
118 }
119 
120 template <class T>
121 Fortran_Array1D<T>::Fortran_Array1D(int n, T *a) : v_(a), n_(n) , data_(v_.begin())
122 {
123 #ifdef TNT_DEBUG
124  std::cout << "Created Fortran_Array1D(int n, T* a) \n";
125 #endif
126 }
127 
128 template <class T>
130 {
131 #ifdef TNT_BOUNDS_CHECK
132  assert(i>= 1);
133  assert(i <= n_);
134 #endif
135  return data_[i-1];
136 }
137 
138 template <class T>
139 inline const T& Fortran_Array1D<T>::operator()(int i) const
140 {
141 #ifdef TNT_BOUNDS_CHECK
142  assert(i>= 1);
143  assert(i <= n_);
144 #endif
145  return data_[i-1];
146 }
147 
148 
149 
150 
151 template <class T>
153 {
154  set_(data_, data_+n_, a);
155  return *this;
156 }
157 
158 template <class T>
160 {
161  Fortran_Array1D A( n_);
162  copy_(A.data_, data_, n_);
163 
164  return A;
165 }
166 
167 
168 template <class T>
170 {
171  if (A.n_ == n_)
172  copy_(data_, A.data_, n_);
173 
174  return *this;
175 }
176 
177 
178 
179 
180 
181 template <class T>
183 {
184  if (this != &A)
185  {
186  v_ = A.v_; /* operator= handles the reference counting. */
187  n_ = A.n_;
188  data_ = A.data_;
189 
190  }
191  return *this;
192 }
193 
194 template <class T>
196 {
197  return ref(A);
198 }
199 
200 template <class T>
201 inline int Fortran_Array1D<T>::dim1() const { return n_; }
202 
203 template <class T>
204 inline int Fortran_Array1D<T>::dim() const { return n_; }
205 
206 template <class T>
208 
209 
210 /* ............................ exented interface ......................*/
211 
212 template <class T>
214 {
215  return v_.ref_count();
216 }
217 
218 template <class T>
220 {
221 #ifdef TNT_DEBUG
222  std::cout << "entered subarray. \n";
223 #endif
224  if (((i0 > 0) && (i1 < n_)) || (i0 <= i1))
225  {
226  Fortran_Array1D<T> X(*this); /* create a new instance of this array. */
227  X.n_ = i1-i0+1;
228  X.data_ += i0;
229 
230  return X;
231  }
232  else
233  {
234 #ifdef TNT_DEBUG
235  std::cout << "subarray: null return.\n";
236 #endif
237  return Fortran_Array1D<T>();
238  }
239 }
240 
241 
242 /* private internal functions */
243 
244 
245 template <class T>
246 void Fortran_Array1D<T>::set_(T* begin, T* end, const T& a)
247 {
248  for (T* p=begin; p<end; p++)
249  *p = a;
250 
251 }
252 
253 template <class T>
254 void Fortran_Array1D<T>::copy_(T* p, const T* q, int len) const
255 {
256  T *end = p + len;
257  while (p<end )
258  *p++ = *q++;
259 
260 }
261 
262 
263 } /* namespace TNT */
264 
265 #endif
266 /* TNT_FORTRAN_ARRAY1D_H */
Definition: tnt_fortran_array1d.h:40
Fortran_Array1D & operator=(const Fortran_Array1D &A)
Fortran_Array1D< T > subarray(int i0, int i1)
Definition: tnt_fortran_array1d.h:219
T value_type
Definition: tnt_fortran_array1d.h:58
void copy_(T *p, const T *q, int len) const
Definition: tnt_fortran_array1d.h:254
Fortran_Array1D & inject(const Fortran_Array1D &A)
Definition: tnt_fortran_array1d.h:169
int dim1() const
Definition: tnt_fortran_array1d.h:201
Fortran_Array1D & ref(const Fortran_Array1D &A)
Definition: tnt_fortran_array1d.h:182
Fortran_Array1D()
Definition: tnt_fortran_array1d.h:89
~Fortran_Array1D()
Definition: tnt_fortran_array1d.h:207
T & operator()(int i)
Definition: tnt_fortran_array1d.h:129
i_refvec< T > v_
Definition: tnt_fortran_array1d.h:44
Fortran_Array1D & operator=(const T &a)
Definition: tnt_fortran_array1d.h:152
T * data_
Definition: tnt_fortran_array1d.h:46
Fortran_Array1D copy() const
Definition: tnt_fortran_array1d.h:159
int dim() const
Definition: tnt_fortran_array1d.h:204
void set_(T *begin, T *end, const T &val)
Definition: tnt_fortran_array1d.h:246
Fortran_Array1D(const Fortran_Array1D &A)
int n_
Definition: tnt_fortran_array1d.h:45
void initialize_(int n)
int ref_count() const
Definition: tnt_fortran_array1d.h:213
Definition: tnt_i_refvec.h:56
Definition: tnt_array1d.h:36