My Project
Loading...
Searching...
No Matches
tnt_array1d.h
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_ARRAY1D_H
23#define TNT_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
35namespace TNT
36{
37
38template <class T>
39class Array1D
40{
41
42 private:
43
44 /* ... */
45 i_refvec<T> v_;
46 int n_;
47 T* data_; /* this normally points to v_.begin(), but
48 * could also point to a portion (subvector)
49 * of v_.
50 */
51
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
61 Array1D();
62 explicit Array1D(int n);
63 Array1D(int n, const T &a);
64 Array1D(int n, T *a);
65 inline Array1D(const Array1D &A);
66 inline operator T*();
67 inline operator const T*();
68 inline Array1D & operator=(const T &a);
69 inline Array1D & operator=(const Array1D &A);
70 inline Array1D & ref(const Array1D &A);
71 Array1D copy() const;
72 Array1D & inject(const Array1D & A);
73 inline T& operator[](int i);
74 inline T& operator()(int i);
75 inline const T& operator[](int i) const;
76 inline const T& operator()(int i) const;
77 inline int dim1() const;
78 inline int dim() const;
79 ~Array1D();
80
81
82 /* ... extended interface ... */
83
84 inline int ref_count() const;
85 inline Array1D<T> subarray(int i0, int i1);
86
87};
88
89
90
91
92template <class T>
93Array1D<T>::Array1D() : v_(), n_(0), data_(0) {}
94
95template <class T>
96Array1D<T>::Array1D(const Array1D<T> &A) : v_(A.v_), n_(A.n_),
97 data_(A.data_)
98{
99#ifdef TNT_DEBUG
100 std::cout << "Created Array1D(const Array1D<T> &A) \n";
101#endif
102
103}
104
105template <class T>
106Array1D<T>::Array1D(int n) : v_(n), n_(n), data_(v_.begin())
107{
108#ifdef TNT_DEBUG
109 std::cout << "Created Array1D(int n) \n";
110#endif
111}
112
113template <class T>
114Array1D<T>::Array1D(int n, const T &val) : v_(n), n_(n), data_(v_.begin())
115{
116#ifdef TNT_DEBUG
117 std::cout << "Created Array1D(int n, const T& val) \n";
118#endif
119 set_(data_, data_+ n, val);
120
121}
122
123template <class T>
124Array1D<T>::Array1D(int n, T *a) : v_(a), n_(n) , data_(v_.begin())
125{
126#ifdef TNT_DEBUG
127 std::cout << "Created Array1D(int n, T* a) \n";
128#endif
129}
130
131template <class T>
132inline Array1D<T>::operator T*()
133{
134 return &(v_[0]);
135}
136
137
138template <class T>
139inline Array1D<T>::operator const T*()
140{
141 return &(v_[0]);
142}
143
144
145
146template <class T>
147inline T& Array1D<T>::operator[](int i)
148{
149#ifdef TNT_BOUNDS_CHECK
150 assert(i>= 0);
151 assert(i < n_);
152#endif
153 return data_[i];
154}
155
156template <class T>
157inline const T& Array1D<T>::operator[](int i) const
158{
159#ifdef TNT_BOUNDS_CHECK
160 assert(i>= 0);
161 assert(i < n_);
162#endif
163 return data_[i];
164}
165
166
167
168template <class T>
169inline T& Array1D<T>::operator()(int i)
170{
171#ifdef TNT_BOUNDS_CHECK
172 assert(i>= 0);
173 assert(i < n_);
174#endif
175 return data_[i-1];
176}
177
178template <class T>
179inline const T& Array1D<T>::operator()(int i) const
180{
181#ifdef TNT_BOUNDS_CHECK
182 assert(i>= 0);
183 assert(i < n_);
184#endif
185 return data_[i-1];
186}
187
188
189
190
191template <class T>
192Array1D<T> & Array1D<T>::operator=(const T &a)
193{
194 set_(data_, data_+n_, a);
195 return *this;
196}
197
198template <class T>
199Array1D<T> Array1D<T>::copy() const
200{
201 Array1D A( n_);
202 copy_(A.data_, data_, n_);
203
204 return A;
205}
206
207
208template <class T>
209Array1D<T> & Array1D<T>::inject(const Array1D &A)
210{
211 if (A.n_ == n_)
212 copy_(data_, A.data_, n_);
213
214 return *this;
215}
216
217
218
219
220
221template <class T>
222Array1D<T> & Array1D<T>::ref(const Array1D<T> &A)
223{
224 if (this != &A)
225 {
226 v_ = A.v_; /* operator= handles the reference counting. */
227 n_ = A.n_;
228 data_ = A.data_;
229
230 }
231 return *this;
232}
233
234template <class T>
235Array1D<T> & Array1D<T>::operator=(const Array1D<T> &A)
236{
237 return ref(A);
238}
239
240template <class T>
241inline int Array1D<T>::dim1() const { return n_; }
242
243template <class T>
244inline int Array1D<T>::dim() const { return n_; }
245
246template <class T>
247Array1D<T>::~Array1D() {}
248
249
250/* ............................ exented interface ......................*/
251
252template <class T>
253inline int Array1D<T>::ref_count() const
254{
255 return v_.ref_count();
256}
257
258template <class T>
259inline Array1D<T> Array1D<T>::subarray(int i0, int i1)
260{
261 if ((i0 >= 0) && (i1 < n_) || (i0 <= i1))
262 {
263 Array1D<T> X(*this); /* create a new instance of this array. */
264 X.n_ = i1-i0+1;
265 X.data_ += i0;
266
267 return X;
268 }
269 else
270 {
271 return Array1D<T>();
272 }
273}
274
275
276/* private internal functions */
277
278
279template <class T>
280void Array1D<T>::set_(T* begin, T* end, const T& a)
281{
282 for (T* p=begin; p<end; p++)
283 *p = a;
284
285}
286
287template <class T>
288void Array1D<T>::copy_(T* p, const T* q, int len) const
289{
290 T *end = p + len;
291 while (p<end )
292 *p++ = *q++;
293
294}
295
296
297} /* namespace TNT */
298
299#endif
300/* TNT_ARRAY1D_H */
301
Definition tnt_array1d.h:40
Definition tnt_i_refvec.h:56
Definition tnt_array1d.h:36