Vector.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef GNSSTK_VECTOR_HPP
45 #define GNSSTK_VECTOR_HPP
46 
47 #include <limits>
48 #include <vector>
49 #include "VectorBase.hpp"
50 
51 namespace gnsstk
52 {
54 
55 
56  // forward declaration
57  template <class T> class VectorSlice;
58 
66  template <class T>
67  class Vector : public RefVectorBase<T, Vector<T> >
68  {
69  public:
71  typedef T value_type;
73  typedef T& reference;
75  typedef const T& const_reference;
77  typedef T* iterator;
79  typedef const T* const_iterator;
80 
82  Vector() : v(NULL), s(0)
83  {}
85  Vector(size_t siz) : v(NULL), s(siz)
86  {
87  if (siz>0)
88  {
89  v = new T[siz];
90  if(!v) {
91  VectorException e("Vector(size_t) failed to allocate");
92  GNSSTK_THROW(e);
93  }
94  }
95  }
100  Vector(size_t siz, const T defaultValue) : v(NULL), s(siz)
101  {
102  if (siz>0)
103  {
104  v = new T[siz];
105  if(!v) {
106  VectorException e("Vector<T>(size_t, const T) failed to allocate");
107  GNSSTK_THROW(e);
108  }
109  this->assignFrom(defaultValue);
110  }
111  }
115  template <class E>
117  {
118  if (r.size()>0)
119  {
120  v = new T[r.size()];
121  if(!v) {
122  VectorException e("Vector<T>(ConstVectorBase) failed to allocate");
123  GNSSTK_THROW(e);
124  }
125  this->assignFrom(r);
126  }
127  }
131  Vector(const Vector& r) : v(NULL), s(r.s)
132  {
133  if (r.s>0)
134  {
135  v = new T[r.s];
136  if(!v) {
137  VectorException e("Vector(Vector) failed to allocate");
138  GNSSTK_THROW(e);
139  }
140  this->assignFrom(r);
141  }
142  }
146  Vector(const std::valarray<T>& r) : v(NULL), s(r.size())
147  {
148  if (r.size())
149  {
150  v = new T[r.size()];
151  if(!v) {
152  VectorException e("Vector(valarray) failed to allocate");
153  GNSSTK_THROW(e);
154  }
155  this->assignFrom(r);
156  }
157  }
158 
160  template <class E>
162  size_t top,
163  size_t num) : v(NULL), s(0)
164  {
165  // sanity checks...
166  if ( top >= vec.size() ||
167  top + num > vec.size())
168  {
169  VectorException e("Invalid dimensions or size for Vector(VectorBase)");
170  GNSSTK_THROW(e);
171  }
172  if (num>0)
173  {
174  v = new T[num];
175  if(!v) {
176  VectorException e("Vector(subvector) failed to allocate");
177  GNSSTK_THROW(e);
178  }
179  size_t i;
180  for(i = 0; i < num; i++)
181  v[i] = vec(top+i);
182  s = num;
183  }
184  }
185 
188  {
189  if (v) delete [] v;
190  }
191 
193  iterator begin() { return v; }
195  const_iterator begin() const { return v; }
197  iterator end() { return v + s; }
199  const_iterator end() const { return v + s; }
201  value_type front() { return v[s-1]; }
203  const_reference front() const { return v[s-1];}
205  bool empty() const { return size() == 0; }
207  size_t size() const {return s; }
209  size_t max_size() const { return std::numeric_limits<size_t>().max(); }
210 
212  T& operator[] (size_t i)
213  { return v[i]; }
215  T operator[] (size_t i) const
216  { return v[i]; }
218  T& operator() (size_t i)
219  { return v[i]; }
221  T operator() (size_t i) const
222  { return v[i]; }
223 
225  VectorSlice<T> operator[] (const std::slice& sli)
226  { return VectorSlice<T>(*this, sli); }
227 
230  { resize(x.s); return this->assignFrom(x); }
231 
233  template <class E>
235  { resize(x.size()); return this->assignFrom(x); }
236 
238  Vector& operator=(const std::valarray<T>& x)
239  { resize(x.size()); return this->assignFrom(x); }
241  Vector& operator=(const T x)
242  { return this->assignFrom(x); }
244  Vector& operator=(const T* x)
245  { return this->assignFrom(x); }
246 
248  inline Vector& operator=(const std::vector<T>& x)
249  {
250  size_t i;
251  size_t vs = x.size();
252  (*this).resize(vs);
253 
254  for (i = 0; i < vs; i++)
255  (*this)[i] = x[i];
256 
257  return (*this);
258  }
259 
262  Vector& resize(const size_t index)
263  {
264  if (index > s)
265  {
266  if (v)
267  delete [] v;
268  v = new T[index];
269  if(!v) {
270  VectorException e("Vector.resize(size_t) failed to allocate");
271  GNSSTK_THROW(e);
272  }
273  }
274  s = index;
275  return *this;
276  }
277 
279  Vector& resize(const size_t index, const T defaultValue)
280  {
281  resize(index);
282  for(size_t i = 0; i < s; i++)
283  v[i] = defaultValue;
284  return *this;
285  }
286 
287  // convert the gnsstk vector to std vector
288  std::vector<T> toStdVector()
289  {
290  std::vector<T> std_vec;
291  for(size_t i = 0; i < s; i++)
292  std_vec.push_back(v[i] );
293  return std_vec;
294  }
295 
296  inline Vector& operator<<(const Vector& b)
297  {
298  size_t i;
299  size_t vs = this->size();
300  size_t bs = b.size();
301  size_t rows = vs + bs;
302  Vector<T> toReturn(rows);
303 
304  for (i = 0; i < vs; i++)
305  toReturn[i] = (*this)[i];
306 
307  for (i = 0; i < bs; i++)
308  toReturn[i+vs] = b[i];
309 
310  (*this) = toReturn;
311 
312  return (*this);
313  }
314 
316  inline Vector& operator<<(const T &b)
317  {
318  return (*this) << Vector(1,b);
319  }
320 
322  inline Vector operator&&(const Vector &b)
323  {
324  size_t i;
325  size_t vs = this->size();
326  size_t bs = b.size();
327  size_t rows = vs + bs;
328  Vector<T> toReturn(rows);
329 
330  for (i = 0; i < vs; i++)
331  toReturn[i] = (*this)[i];
332 
333  for (i = 0; i < bs; i++)
334  toReturn[i+vs] = b[i];
335 
336  return toReturn;
337  }
338 
340  inline Vector operator&&(const T &b)
341  {
342  size_t i;
343  size_t vs = this->size();
344  size_t rows = vs + 1;
345  Vector<T> toReturn(rows);
346 
347  for (i = 0; i < vs; i++)
348  toReturn[i] = (*this)[i];
349 
350  toReturn[rows - 1] = b;
351 
352  return toReturn;
353  }
354 
355  private:
356 
357  // a good optimizer will remove this function call
358  // if RANGECHECK isn't defined. remember that
359  // range checking affects EVERY operation
360  inline bool rangeCheck(const size_t index) const
361  {
362 #ifdef RANGECHECK
363  return (index < s);
364 #else
365  return true;
366 #endif
367  }
368 
370  T* v;
372  size_t s;
373  };
374  // end class Vector<T>
375 
381  template <class T>
382  class VectorSlice : public RefVectorSliceBase<T, VectorSlice<T> >
383  {
384  public:
387  : v(NULL), s(std::slice(0,0,0))
388  {}
389 
392  : v(&vv), s(std::slice(0,vv.size(),1))
393  {}
394 
396  VectorSlice(Vector<T>& vv, const std::slice& ss)
397  : v(&vv), s(ss)
398  { this->vecSliceCheck(vv.size()); }
399 
401  template <class V>
403  { return this->assignFrom(x); }
404 
406  VectorSlice& operator=(const std::valarray<T>& x)
407  { return this->assignFrom(x); }
408 
410  VectorSlice& operator=(const T x)
411  { return this->assignFrom(x); }
412 
414  VectorSlice& operator=(const T* x)
415  { return this->assignFrom(x); }
416 
418  T& operator[] (size_t i)
419  { return (*v)[start() + i * stride()]; }
421  T operator[] (size_t i) const
422  { return (*v)[start() + i * stride()]; }
424  T& operator() (size_t i)
425  { return (*v)[start() + i * stride()]; }
427  T operator() (size_t i) const
428  { return (*v)[start() + i * stride()]; }
429 
431  inline size_t size() const { return s.size(); }
433  inline size_t start() const { return s.start(); }
436  inline size_t stride() const { return s.stride(); }
437  private:
441  std::slice s;
442  };
443 
447  template <class T>
448  class ConstVectorSlice : public ConstVectorSliceBase<T, ConstVectorSlice<T> >
449  {
450  public:
453  : v(NULL), s(std::slice(0,0,0))
454  { }
455 
458  : v(&vv), s(std::slice(0,vv.size(),1))
459  { }
460 
462  ConstVectorSlice(const Vector<T>& vv, const std::slice& ss)
463  : v(&vv), s(ss)
464  { vecSliceCheck(vv.size()); }
465 
467  T operator[] (size_t i) const
468  { return (*v)[start() + i * stride()]; }
470  T operator() (size_t i) const
471  { return (*v)[start() + i * stride()]; }
472 
474  inline size_t size() const { return s.size(); }
476  inline size_t start() const { return s.start(); }
479  inline size_t stride() const { return s.stride(); }
480 
481  private:
483  const Vector<T>* v;
485  std::slice s;
486  };
487 
489 
490 } // namespace
491 
492 #include "VectorOperators.hpp"
493 
494 #endif
gnsstk::ConstVectorSlice::ConstVectorSlice
ConstVectorSlice(const Vector< T > &vv, const std::slice &ss)
Uses the given slice and vector.
Definition: Vector.hpp:462
gnsstk::ConstVectorSlice::size
size_t size() const
returns the number of elements in the slice
Definition: Vector.hpp:474
gnsstk::Vector::iterator
T * iterator
STL iterator type.
Definition: Vector.hpp:77
gnsstk::Vector::end
iterator end()
STL iterator end.
Definition: Vector.hpp:197
gnsstk::VectorSlice::VectorSlice
VectorSlice()
Default constructor.
Definition: Vector.hpp:386
gnsstk::Vector::end
const_iterator end() const
STL const iterator end.
Definition: Vector.hpp:199
VectorOperators.hpp
gnsstk::ConstVectorSlice::operator[]
T operator[](size_t i) const
Returns a const version of the i'th slice element.
Definition: Vector.hpp:467
gnsstk::VectorSlice
Definition: Vector.hpp:57
gnsstk::VectorSlice::operator=
VectorSlice & operator=(const ConstVectorBase< T, V > &x)
Assign the elements of this slice from another vector.
Definition: Vector.hpp:402
gnsstk::VectorSlice::operator=
VectorSlice & operator=(const T *x)
Assign (*this).size() elements from x to (*this).
Definition: Vector.hpp:414
gnsstk::ConstVectorSlice::start
size_t start() const
returns the index in the vector of the first element.
Definition: Vector.hpp:476
gnsstk::Vector::operator()
T & operator()(size_t i)
Non-const operator ()
Definition: Vector.hpp:218
gnsstk::ConstVectorSlice::operator()
T operator()(size_t i) const
Returns a const version of the i'th slice element.
Definition: Vector.hpp:470
NULL
#define NULL
Definition: getopt1.c:64
gnsstk::Vector::~Vector
~Vector()
Destructor.
Definition: Vector.hpp:187
gnsstk::RefVectorBase
Definition: VectorBase.hpp:155
gnsstk::Vector::value_type
T value_type
STL value type.
Definition: Vector.hpp:71
gnsstk::VectorSlice::v
Vector< T > * v
the vector used as a source for the slice
Definition: Vector.hpp:439
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Vector::resize
Vector & resize(const size_t index)
Definition: Vector.hpp:262
gnsstk::Vector::operator=
Vector & operator=(const Vector &x)
*this will be resized if it isn't as large as x.
Definition: Vector.hpp:229
gnsstk::Vector::begin
const_iterator begin() const
STL const iterator begin.
Definition: Vector.hpp:195
gnsstk::Vector::Vector
Vector(const std::valarray< T > &r)
Definition: Vector.hpp:146
gnsstk::Vector::const_reference
const typedef T & const_reference
STL const reference type.
Definition: Vector.hpp:75
gnsstk::Vector::s
size_t s
The size of the vector.
Definition: Vector.hpp:372
gnsstk::Vector::operator&&
Vector operator&&(const Vector &b)
Returns the concatenation of this Vector and Vector b.
Definition: Vector.hpp:322
gnsstk::VectorSlice::s
std::slice s
the slice specification.
Definition: Vector.hpp:441
gnsstk::VectorSlice::stride
size_t stride() const
Definition: Vector.hpp:436
gnsstk::Vector::rangeCheck
bool rangeCheck(const size_t index) const
Definition: Vector.hpp:360
gnsstk::Vector::front
value_type front()
STL front.
Definition: Vector.hpp:201
gnsstk::Vector::operator=
Vector & operator=(const std::valarray< T > &x)
*this will be resized if it isn't as large as x.
Definition: Vector.hpp:238
gnsstk::ConstVectorSlice::stride
size_t stride() const
Definition: Vector.hpp:479
gnsstk::Vector::resize
Vector & resize(const size_t index, const T defaultValue)
resize with new default value
Definition: Vector.hpp:279
gnsstk::Vector::toStdVector
std::vector< T > toStdVector()
Definition: Vector.hpp:288
gnsstk::Vector::operator&&
Vector operator&&(const T &b)
Returns the concatenation of this Vector and a scalar of type T.
Definition: Vector.hpp:340
gnsstk::VectorSlice::operator[]
T & operator[](size_t i)
Returns the modifiable i'th element of the slice.
Definition: Vector.hpp:418
gnsstk::VectorSlice::start
size_t start() const
returns the index in the vector of the first element.
Definition: Vector.hpp:433
gnsstk::Vector::operator=
Vector & operator=(const T *x)
Only (*this).size() elements will be assigned.
Definition: Vector.hpp:244
gnsstk::Vector::operator<<
Vector & operator<<(const T &b)
Returns the concatenation of this Vector and a scalar of type T.
Definition: Vector.hpp:316
gnsstk::ConstVectorSlice::ConstVectorSlice
ConstVectorSlice()
default constructor
Definition: Vector.hpp:452
gnsstk::Vector::Vector
Vector(size_t siz, const T defaultValue)
Definition: Vector.hpp:100
gnsstk::Vector::Vector
Vector(const ConstVectorBase< T, E > &r)
Definition: Vector.hpp:116
gnsstk::VectorSlice::size
size_t size() const
returns the number of elements in the slice
Definition: Vector.hpp:431
gnsstk::VectorSlice::operator=
VectorSlice & operator=(const T x)
Assign all the elements of this slice to x.
Definition: Vector.hpp:410
gnsstk::ConstVectorSlice::s
std::slice s
the slice specification.
Definition: Vector.hpp:485
gnsstk::Vector::Vector
Vector(const ConstVectorBase< T, E > &vec, size_t top, size_t num)
subvector constructor
Definition: Vector.hpp:161
gnsstk::Vector::operator=
Vector & operator=(const T x)
Only (*this).size() elements will be assigned.
Definition: Vector.hpp:241
gnsstk::Vector
Definition: Vector.hpp:67
gnsstk::ConstVectorSliceBase
Definition: VectorBase.hpp:311
gnsstk::Vector::Vector
Vector()
Default constructor.
Definition: Vector.hpp:82
gnsstk::ConstVectorBase::size
size_t size() const
Returns the size of the base class.
Definition: VectorBase.hpp:112
gnsstk::VectorSlice::operator=
VectorSlice & operator=(const std::valarray< T > &x)
Assign the elements of this slice from a valarray.
Definition: Vector.hpp:406
gnsstk::RefVectorSliceBase
Definition: VectorBase.hpp:323
gnsstk::Vector::operator[]
T & operator[](size_t i)
Non-const operator [].
Definition: Vector.hpp:212
gnsstk::Vector::empty
bool empty() const
STL empty.
Definition: Vector.hpp:205
gnsstk::Vector::reference
T & reference
STL reference type.
Definition: Vector.hpp:73
gnsstk::VectorSliceBase< ConstVectorSlice< T > >::vecSliceCheck
void vecSliceCheck(size_t sourceSize) const
Definition: VectorBase.hpp:292
gnsstk::Vector::operator<<
Vector & operator<<(const Vector &b)
Definition: Vector.hpp:296
gnsstk::ConstVectorBase
Definition: VectorBase.hpp:105
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
gnsstk::ConstVectorSlice
Definition: Vector.hpp:448
std
Definition: Angle.hpp:142
gnsstk::Vector::Vector
Vector(size_t siz)
Constructor given an initial size.
Definition: Vector.hpp:85
gnsstk::Vector::Vector
Vector(const Vector &r)
Definition: Vector.hpp:131
gnsstk::Vector::operator=
Vector & operator=(const ConstVectorBase< T, E > &x)
*this will be resized if it isn't as large as x.
Definition: Vector.hpp:234
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::Vector::max_size
size_t max_size() const
STL max_size.
Definition: Vector.hpp:209
gnsstk::VectorSlice::operator()
T & operator()(size_t i)
Returns the modifiable i'th element of the slice.
Definition: Vector.hpp:424
gnsstk::Vector::front
const_reference front() const
STL const front.
Definition: Vector.hpp:203
gnsstk::Vector::const_iterator
const typedef T * const_iterator
STL const iterator type.
Definition: Vector.hpp:79
gnsstk::ConstVectorSlice::v
const Vector< T > * v
Vectortor used as a source for this slice.
Definition: Vector.hpp:483
gnsstk::Vector::operator=
Vector & operator=(const std::vector< T > &x)
*this will be cleared and resized as necessary
Definition: Vector.hpp:248
gnsstk::ConstVectorSlice::ConstVectorSlice
ConstVectorSlice(const Vector< T > &vv)
Makes a slice of the whole vector.
Definition: Vector.hpp:457
gnsstk::VectorSlice::VectorSlice
VectorSlice(Vector< T > &vv, const std::slice &ss)
Makes a slice of the vector with the given std::slice.
Definition: Vector.hpp:396
gnsstk::VectorSlice::VectorSlice
VectorSlice(Vector< T > &vv)
Makes a slice of the whole vector.
Definition: Vector.hpp:391
gnsstk::Vector::begin
iterator begin()
STL iterator begin.
Definition: Vector.hpp:193
gnsstk::Vector::v
T * v
The vector.
Definition: Vector.hpp:370
VectorBase.hpp


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:42