Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vecadaptor.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 
34 #ifndef VECADAPTOR_H
35 #define VECADAPTOR_H
36 
37 #include <cstdlib>
38 #include <iostream>
39 #include <strstream>
40 #include <cassert>
41 
42 #include "subscript.h"
43 
44 #ifdef TNT_USE_REGIONS
45 #include "region1d.h"
46 #endif
47 
48 namespace TNT
49 {
50 
51  // see "tntreq.h" for TNT requirements for underlying vector
52  // class. This need NOT be the STL vector<> class, but a subset
53  // that provides minimal services.
54  //
55  // This is a container adaptor that provides the following services.
56  //
57  // o) adds 1-offset operator() access ([] is always 0 offset)
58  // o) adds TNT_BOUNDS_CHECK to () and []
59  // o) adds initialization from strings, e.g. "1.0 2.0 3.0";
60  // o) adds newsize(N) function (does not preserve previous values)
61  // o) adds dim() and dim(1)
62  // o) adds free() function to release memory used by vector
63  // o) adds regions, e.g. A(Index(1,10)) = ...
64  // o) add getVector() method to return adapted container
65  // o) adds simple I/O for ostreams
66 
67  template <class BBVec>
69  {
70 
71  public:
72  typedef typename BBVec::value_type T;
73  typedef T value_type;
74  typedef T element_type;
75  typedef T* pointer;
76  typedef T* iterator;
77  typedef T& reference;
78  typedef const T* const_iterator;
79  typedef const T& const_reference;
80 
81  Subscript lbound() const { return 1; }
82 
83  protected:
84  BBVec v_;
85  T* vm1_;
86 
87  public:
88 
89  Subscript size() const { return v_.size(); }
90 
91  // These were removed so that the ANSI C++ valarray class
92  // would work as a possible storage container.
93  //
94  //
95  //iterator begin() { return v_.begin();}
96  //iterator begin() { return &v_[0];}
97  //
98  //iterator end() { return v_.end(); }
99  //iterator end() { return &v_[0] + v_.size(); }
100  //
101  //const_iterator begin() const { return v_.begin();}
102  //const_iterator begin() const { return &v_[0];}
103  //
104  //const_iterator end() const { return v_.end(); }
105  //const_iterator end() const { return &v_[0] + v_.size(); }
106 
107  BBVec& getVector() { return v_; }
108  Subscript dim() const { return v_.size(); }
110  {
111 #ifdef TNT_BOUNDS_CHECK
112  BIASASSERT(i==TNT_BASE_OFFSET);
113 #endif
114  return (i==TNT_BASE_OFFSET ? v_.size() : 0 );
115  }
116  Vector_Adaptor() : v_() {};
118  {
119  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
120 
121  }
122 
123  Vector_Adaptor(Subscript N, /*const*/ char *s) : v_(N)
124  {
125  istrstream ins(s);
126  for (Subscript i=0; i<N; i++)
127  ins >> v_[i] ;
128 
129  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
130  };
131 
132  Vector_Adaptor(Subscript N, const T& value = T()) : v_(N)
133  {
134  for (Subscript i=0; i<N; i++)
135  v_[i] = value;
136 
137  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
138  }
139 
140  Vector_Adaptor(Subscript N, const T* values) : v_(N)
141  {
142  for (Subscript i=0; i<N; i++)
143  v_[i] = values[i];
144  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
145  }
146  Vector_Adaptor(const BBVec & A) : v_(A)
147  {
148  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
149  }
150 
151  // NOTE: this assumes that BBVec(0) constructor creates an
152  // null vector that does not take up space... It would be
153  // great to require that BBVec have a corresponding free()
154  // function, but in particular STL vectors do not.
155  //
157  {
158  return *this = Vector_Adaptor<BBVec>(0);
159  }
160 
162  {
163  v_ = A.v_ ;
164  vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL);
165  return *this;
166  }
167 
169  {
170  // NOTE: this is not as efficient as it could be
171  // but to retain compatiblity with STL interface
172  // we cannot assume underlying implementation
173  // has a newsize() function.
174 
175  return *this = Vector_Adaptor<BBVec>(N);
176 
177  }
178 
180  {
181  Subscript i;
182  Subscript N = v_.size();
183  for (i=0; i<N; i++)
184  v_[i] = a;
185 
186  return *this;
187  }
188 
190  {
191  if (N == size()) return *this;
192 
193  Vector_Adaptor<BBVec> tmp(N);
194  Subscript n = (N < size() ? N : size()); // min(N, size());
195  Subscript i;
196 
197  for (i=0; i<n; i++)
198  tmp[i] = v_[i];
199 
200 
201  return (*this = tmp);
202 
203  }
204 
205 
207  {
208 #ifdef TNT_BOUNDS_CHECK
209  BIASASSERT(1<=i);
210  BIASASSERT(i<=dim());
211 #endif
212  return vm1_[i];
213  }
214 
216  {
217 #ifdef TNT_BOUNDS_CHECK
218  BIASASSERT(1<=i);
219  BIASASSERT(i<=dim());
220 #endif
221  return vm1_[i];
222  }
223 
225  {
226 #ifdef TNT_BOUNDS_CHECK
227  BIASASSERT(0<=i);
228  BIASASSERT(i<dim());
229 #endif
230  return v_[i];
231  }
232 
234  {
235 #ifdef TNT_BOUNDS_CHECK
236  BIASASSERT(0<=i);
237  BIASASSERT(i<dim());
238 #endif
239  return v_[i];
240  }
241 
242 
243 #ifdef TNT_USE_REGIONS
244  // "index-aware" features, all of these are 1-based offsets
245 
246  typedef Region1D<Vector_Adaptor<BBVec> > Region;
247 
248  typedef const_Region1D< Vector_Adaptor<BBVec> > const_Region;
249 
250  Region operator()(const Index1D &I)
251  { return Region(*this, I); }
252 
253  Region operator()(const Subscript i1, Subscript i2)
254  { return Region(*this, i1, i2); }
255 
256  const_Region operator()(const Index1D &I) const
257  { return const_Region(*this, I); }
258 
259  const_Region operator()(const Subscript i1, Subscript i2) const
260  { return const_Region(*this, i1, i2); }
261 #endif
262  // TNT_USE_REGIONS
263 
264 
265  };
266 
267 #include <iostream>
268 
269  template <class BBVec>
270  std::ostream& operator<<(std::ostream &s, const Vector_Adaptor<BBVec> &A)
271  {
272  Subscript M=A.size();
273 
274  s << M << endl;
275  for (Subscript i=1; i<=M; i++)
276  s << A(i) << endl;
277  return s;
278  }
279 
280  template <class BBVec>
281  std::istream& operator>>(std::istream &s, Vector_Adaptor<BBVec> &A)
282  {
283  Subscript N;
284 
285  s >> N;
286 
287  A.resize(N);
288 
289  for (Subscript i=1; i<=N; i++)
290  s >> A(i);
291 
292  return s;
293  }
294 
295 } // namespace TNT
296 
297 #endif
Vector_Adaptor(Subscript N, char *s)
Definition: vecadaptor.h:123
std::istream & operator>>(std::istream &s, Fortran_Matrix< T > &A)
Definition: fmat.h:345
reference operator[](Subscript i)
Definition: vecadaptor.h:224
Vector_Adaptor< BBVec > & operator=(const Vector_Adaptor< BBVec > &A)
Definition: vecadaptor.h:161
const_reference operator[](Subscript i) const
Definition: vecadaptor.h:233
Vector_Adaptor< BBVec > & free()
Definition: vecadaptor.h:156
BBVec::value_type T
Definition: vecadaptor.h:72
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
Vector_Adaptor(const Vector_Adaptor< BBVec > &A)
Definition: vecadaptor.h:117
Subscript size() const
Definition: vecadaptor.h:89
Vector_Adaptor< BBVec > & operator=(const T &a)
Definition: vecadaptor.h:179
const T * const_iterator
Definition: vecadaptor.h:78
Vector_Adaptor(Subscript N, const T *values)
Definition: vecadaptor.h:140
Vector_Adaptor< BBVec > & resize(Subscript N)
Definition: vecadaptor.h:189
Vector_Adaptor(const BBVec &A)
Definition: vecadaptor.h:146
Vector_Adaptor(Subscript N, const T &value=T())
Definition: vecadaptor.h:132
Subscript dim() const
Definition: vecadaptor.h:108
Subscript lbound() const
Definition: vecadaptor.h:81
BBVec & getVector()
Definition: vecadaptor.h:107
reference operator()(Subscript i)
Definition: vecadaptor.h:206
const T & const_reference
Definition: vecadaptor.h:79
Subscript dim(Subscript i)
Definition: vecadaptor.h:109
Vector_Adaptor< BBVec > & newsize(Subscript N)
Definition: vecadaptor.h:168
const_reference operator()(Subscript i) const
Definition: vecadaptor.h:215