VectorBase.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_BASE_HPP
45 #define GNSSTK_VECTOR_BASE_HPP
46 
47 #include <valarray>
48 #include "Exception.hpp"
49 
50 #include "MathBase.hpp"
51 #include "gnsstk_export.h"
52 
53 namespace gnsstk
54 {
55 
58  NEW_EXCEPTION_CLASS(VectorException, gnsstk::Exception);
59 
61 
62 
63  /*
64  * There were two overriding philosophies to the vector and
65  * matrix classes:
66  *
67  * The concept of "const" and "reference" (i.e. changable)
68  * vector and matrix types both exist, so that any Const* type
69  * could not be altered while Ref* types can. This allowed one
70  * to add the slice classes that let you use a subset of a
71  * vector and modify (or not if it's const) the original vector.
72  * Furthermore, it allowed slice and non-slice classes to
73  * interoperate through the ConstVectorBase or ConstMatrixBase
74  * classes, so, for example, operator* only needs to be written
75  * in terms of ConstVectorBase to work correctly with Vector,
76  * VectorSlice and ConstVectorSlice.
77  *
78  * Remember that a slice MUST refer to a vector or matrix; you
79  * cannot have a slice independent of a base vector or matrix.
80  *
81  * In the future:
82  *
83  * - Change the math operators to template expressions.
84  * - Add general slices and diagonal matrix slices.
85  * - Make range checking more consistent.
86  * - Make operator= and copy constructors consistent between const and
87  * non-const versions.
88  * - Reevaluate the need for default slice constructors...?
89  * - find a way for LUD and SVD to use the template type of the
90  * parameters rather than specified when the object is
91  * created.
92  * - come up with a policy for when zeroize() will be used before results
93  * are returned.
94  *
95  * @warning MSVC cant deal with cmath header.
96  * Changes to accomidate this may break complex!
97  */
98 
104  template <class T, class BaseClass>
106  {
107  public:
109  explicit ConstVectorBase() {}
110 
112  size_t size() const
113  { return static_cast<const BaseClass*>(this)->size(); }
115  T operator[] (size_t i) const
116  { return constVectorRef(i); }
118  T operator() (size_t i) const
119  { return constVectorRef(i); }
120 
121  protected:
125  inline T constVectorRef(size_t i) const
126  {
127  const BaseClass& b = static_cast<const BaseClass&>(*this);
128 #ifdef RANGECHECK
129  if (i >= b.size())
130  {
131  VectorException e("Invalid ConstVectorBase index");
132  GNSSTK_THROW(e);
133  }
134 #endif
135  return b[i];
136  }
137  };
138 
143  {
144  public:
147  GNSSTK_EXPORT static double zeroTolerance;
148  };
149 
154  template <class T, class BaseClass>
155  class RefVectorBase : public ConstVectorBase<T, BaseClass>,
156  public RefVectorBaseHelper
157  {
158  public:
160  explicit RefVectorBase() {}
162  T& operator[] (size_t i)
163  { return vecRef(i); }
165  T& operator() (size_t i)
166  { return vecRef(i); }
169  BaseClass& zeroize()
170  {
171  BaseClass& me = static_cast<BaseClass&>(*this);
172  size_t i;
173  for (i = 0; i < me.size(); i++)
174  if (ABS(me[i]) < zeroTolerance)
175  me[i] = T(0);
176  return me;
177  }
178 
179 #define VecBaseArrayAssignMacroDontCheckRange(func) \
180  BaseClass& me = static_cast<BaseClass&>(*this); \
181  size_t i; for (i=0; i < me.size(); i++) { \
182  me[i] func x[i]; \
183  } \
184  return me;
185 
186 #ifdef RANGECHECK
187 #define VecBaseArrayAssignMacro(func) \
188  BaseClass& me = static_cast<BaseClass&>(*this); \
189  if (x.size() != me.size()) \
190  { \
191  VectorException e("Unequal lengths for vectors"); \
192  GNSSTK_THROW(e); \
193  } \
194  size_t i; for (i=0; i < me.size(); i++) me[i] func x[i]; \
195  return me;
196 #else
197 #define VecBaseArrayAssignMacro(func) \
198  VecBaseArrayAssignMacroDontCheckRange(func)
199 #endif
200 
201 #define VecBaseAtomicAssignMacro(func) \
202  BaseClass& me = static_cast<BaseClass&>(*this); \
203  size_t i; for (i=0; i < me.size(); i++) me[i] func x; \
204  return me;
205 
206 #define VecBaseNewAssignOperator(funcName, op) \
207  \
208  template <class E> BaseClass& funcName(const ConstVectorBase<T, E>& x) \
209  { VecBaseArrayAssignMacro(op) } \
210  \
211  BaseClass& funcName(const std::valarray<T>& x) \
212  { VecBaseArrayAssignMacro(op) } \
213  \
214  BaseClass& funcName(const T* x) \
215  { VecBaseArrayAssignMacroDontCheckRange(op) } \
216  \
217  BaseClass& funcName(T x) \
218  { VecBaseAtomicAssignMacro(op) }
219 
225  VecBaseNewAssignOperator(assignFrom, =);
226  VecBaseNewAssignOperator(operator+=, +=);
227  VecBaseNewAssignOperator(operator-=, -=);
228  VecBaseNewAssignOperator(operator*=, *=);
229  VecBaseNewAssignOperator(operator/=, /=);
230 
231  // // unary minus: multiplies each element of this vector by -1.
232  //BaseClass& operator-()
233  // {
234  // const T x=T(-1);
235  // VecBaseAtomicAssignMacro(*=);
236  // }
237  // unary minus must not return an l-value
238 
240  const BaseClass operator-() const
241  {
242  const T x=T(-1);
243  BaseClass me = static_cast<BaseClass>(*this);
244  size_t i;
245  for (i=0; i < me.size(); i++) me(i) *= x;
246  return me;
247  }
248 
249  protected:
253  inline T& vecRef(size_t i)
254  {
255  BaseClass& b = static_cast<BaseClass&>(*this);
256 #ifdef RANGECHECK
257  if (i >= b.size())
258  {
259  VectorException e("Invalid VectorBase index");
260  GNSSTK_THROW(e);
261  }
262 #endif
263  return b[i];
264  }
265  };
266 
270  template <class BaseClass>
272  {
273  public:
275  explicit VectorSliceBase() {}
276 
278  size_t size() const
279  { return static_cast<const BaseClass*>(this)->size(); }
281  size_t start() const
282  { return static_cast<const BaseClass*>(this)->start(); }
285  size_t stride() const
286  { return static_cast<const BaseClass*>(this)->stride(); }
287 
288  protected:
292  inline void vecSliceCheck(size_t sourceSize) const
293  {
294 #ifdef RANGECHECK
295  // sanity checks...
296  if ( (start() >= sourceSize) ||
297  ((start() + (size() - 1) * stride()) >= sourceSize) )
298  {
299  VectorException e("Invalid range for slice");
300  GNSSTK_THROW(e);
301  }
302 #endif
303  }
304  };
305 
310  template <class T, class BaseClass>
311  class ConstVectorSliceBase : public VectorSliceBase<BaseClass>,
312  public ConstVectorBase<T, BaseClass>
313  {
314  public:
315  explicit ConstVectorSliceBase() {}
316  };
317 
322  template <class T, class BaseClass>
323  class RefVectorSliceBase : public VectorSliceBase<BaseClass>,
324  public RefVectorBase<T, BaseClass>
325  {
326  public:
327  explicit RefVectorSliceBase() {}
328  };
329 
331 
332 } // namespace gnsstk
333 
334 #include "VectorBaseOperators.hpp"
335 
336 #endif //GNSSTK_VECTOR_BASE_HPP
gnsstk::RefVectorBase::operator-
const BaseClass operator-() const
unary minus: multiplies each element in this matrix by -1.
Definition: VectorBase.hpp:240
gnsstk::RefVectorBase::vecRef
T & vecRef(size_t i)
Definition: VectorBase.hpp:253
gnsstk::RefVectorBase::zeroize
BaseClass & zeroize()
Definition: VectorBase.hpp:169
gnsstk::RefVectorBase::VecBaseNewAssignOperator
VecBaseNewAssignOperator(assignFrom,=)
gnsstk::RefVectorBase
Definition: VectorBase.hpp:155
gnsstk::RefVectorBase::operator[]
T & operator[](size_t i)
returns a modifiable version of the element at index i.
Definition: VectorBase.hpp:162
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::NEW_EXCEPTION_CLASS
NEW_EXCEPTION_CLASS(FileSpecException, gnsstk::Exception)
gnsstk::ConstVectorBase::constVectorRef
T constVectorRef(size_t i) const
Definition: VectorBase.hpp:125
gnsstk::RefVectorBaseHelper
Definition: VectorBase.hpp:142
VectorBaseOperators.hpp
gnsstk::VectorSliceBase::start
size_t start() const
the start index in the BaseClass vector for this slice.
Definition: VectorBase.hpp:281
gnsstk::VectorSliceBase::stride
size_t stride() const
Definition: VectorBase.hpp:285
gnsstk::ConstVectorSliceBase
Definition: VectorBase.hpp:311
gnsstk::ConstVectorBase::size
size_t size() const
Returns the size of the base class.
Definition: VectorBase.hpp:112
ABS
#define ABS(x)
Definition: MathBase.hpp:73
gnsstk::RefVectorSliceBase
Definition: VectorBase.hpp:323
gnsstk::ConstVectorBase::operator[]
T operator[](size_t i) const
returns the element at index i
Definition: VectorBase.hpp:115
gnsstk::VectorSliceBase::vecSliceCheck
void vecSliceCheck(size_t sourceSize) const
Definition: VectorBase.hpp:292
gnsstk::ConstVectorBase
Definition: VectorBase.hpp:105
Exception.hpp
gnsstk::VectorSliceBase
Definition: VectorBase.hpp:271
gnsstk::ConstVectorBase::ConstVectorBase
ConstVectorBase()
Constructor.
Definition: VectorBase.hpp:109
gnsstk::RefVectorSliceBase::RefVectorSliceBase
RefVectorSliceBase()
Definition: VectorBase.hpp:327
gnsstk::RefVectorBase::RefVectorBase
RefVectorBase()
constructor
Definition: VectorBase.hpp:160
gnsstk::RefVectorBase::operator()
T & operator()(size_t i)
returns a modifiable version of the element at index i.
Definition: VectorBase.hpp:165
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::VectorSliceBase::size
size_t size() const
the number of elements in the slice.
Definition: VectorBase.hpp:278
gnsstk::RefVectorBaseHelper::zeroTolerance
static GNSSTK_EXPORT double zeroTolerance
Definition: VectorBase.hpp:147
MathBase.hpp
gnsstk::ConstVectorSliceBase::ConstVectorSliceBase
ConstVectorSliceBase()
Definition: VectorBase.hpp:315
gnsstk::ConstVectorBase::operator()
T operator()(size_t i) const
returns the element at index i
Definition: VectorBase.hpp:118
gnsstk::VectorSliceBase::VectorSliceBase
VectorSliceBase()
constructor
Definition: VectorBase.hpp:275


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