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