kvector_impl.hpp
Go to the documentation of this file.
1 // This file is part of kfilter.
2 // kfilter is a C++ variable-dimension extended kalman filter library.
3 //
4 // Copyright (C) 2004 Vincent Zalzal, Sylvain Marleau
5 // Copyright (C) 2001, 2004 Richard Gourdeau
6 // Copyright (C) 2004 GRPR and DGE's Automation sector
7 // �cole Polytechnique de Montr�al
8 //
9 // Code adapted from algorithms presented in :
10 // Bierman, G. J. "Factorization Methods for Discrete Sequential
11 // Estimation", Academic Press, 1977.
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 
27 #ifndef KVECTOR_IMPL_HPP
28 #define KVECTOR_IMPL_HPP
29 
32 
33 #include KSSTREAM_HEADER
34 
35 namespace Kalman {
36 
38 
46  public:
47 
49 
56  explicit KVectorContextImpl(std::string elemDelim = " ",
57  std::string startDelim = std::string(),
58  std::string endDelim = std::string(),
59  unsigned prec = 4)
60  : elemDelim_(elemDelim), startDelim_(startDelim),
61  endDelim_(endDelim), precision_(prec), width_(8+prec) {
62 
63  std::string ws(" \t\n");
65  ( elemDelim_.find_first_not_of(ws) != std::string::npos );
67  (startDelim_.find_first_not_of(ws) != std::string::npos );
68  skipEndDelim_ =
69  ( endDelim_.find_first_not_of(ws) != std::string::npos );
70  }
71 
72  std::string elemDelim_;
73  std::string startDelim_;
74  std::string endDelim_;
75  unsigned precision_;
76  unsigned width_;
80 };
81 
83 
87 
88  template<typename T, K_UINT_32 BEG, bool DBG>
90  : v_(0), n_(0) {}
91 
94  template<typename T, K_UINT_32 BEG, bool DBG>
96  : vimpl_(n), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
97 
100  template<typename T, K_UINT_32 BEG, bool DBG>
102  : vimpl_(n, a), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
103 
110  template<typename T, K_UINT_32 BEG, bool DBG>
112  : vimpl_(v, v + n), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
113 
116  template<typename T, K_UINT_32 BEG, bool DBG>
118  : vimpl_(v.vimpl_), v_( (v.size() != 0) ? &vimpl_[0] - BEG : 0 ),
119  n_(v.n_) {}
120 
121  template<typename T, K_UINT_32 BEG, bool DBG>
123 
128  template<typename T, K_UINT_32 BEG, bool DBG>
130  if (DBG) {
131 // if (i < BEG || i >= n_ + BEG) {
132 // KOSTRINGSTREAM oss;
133 // oss << "Trying to access element " << i << " not included in ["
134 // << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
135 // throw OutOfBoundError(oss.str());
136 // }
137  if (i >= n_ + BEG) {
138  KOSTRINGSTREAM oss;
139  oss << "Trying to access element " << i << " not included in ["
140  << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
141  throw OutOfBoundError(oss.str());
142  }
143  }
144  return v_[i];
145  }
146 
151  template<typename T, K_UINT_32 BEG, bool DBG>
152  inline const T& KVector<T, BEG, DBG>::operator()(K_UINT_32 i) const {
153  if (DBG) {
154 // if (i < BEG || i >= n_ + BEG) {
155 // KOSTRINGSTREAM oss;
156 // oss << "Trying to access element " << i << " not included in ["
157 // << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
158 // throw OutOfBoundError(oss.str());
159 // }
160 
161  if (i >= n_ + BEG) {
162  KOSTRINGSTREAM oss;
163  oss << "Trying to access element " << i << " not included in ["
164  << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
165  throw OutOfBoundError(oss.str());
166  }
167  }
168  return v_[i];
169  }
170 
173  template<typename T, K_UINT_32 BEG, bool DBG>
175  return n_;
176  }
177 
183  template<typename T, K_UINT_32 BEG, bool DBG>
185 
186  if (n == n_) {
187  return;
188  }
189 
190  vimpl_.resize(n);
191  v_ = (n != 0) ? &vimpl_[0] - BEG : 0;
192  n_ = n;
193  }
194 
197  template<typename T, K_UINT_32 BEG, bool DBG>
199  if (n_ == 0) {
200  return *this;
201  }
202 
203  T* ptr = &vimpl_[0];
204  const T* end = ptr + n_;
205 
206  while (ptr != end) {
207  *ptr++ = a;
208  }
209  return *this;
210  }
211 
215  template<typename T, K_UINT_32 BEG, bool DBG>
216  inline KVector<T, BEG, DBG>&
218  KVector temp(v);
219  swap(temp);
220  return *this;
221  }
222 
226  template<typename T, K_UINT_32 BEG, bool DBG>
227  inline void KVector<T, BEG, DBG>::assign(K_UINT_32 n, const T* v) {
228  KVector temp(n, v);
229  swap(temp);
230  }
231 
237  template<typename T, K_UINT_32 BEG, bool DBG>
239  vimpl_.swap(v.vimpl_);
240  Util::swap(v_, v.v_);
241  Util::swap(n_, v.n_);
242  }
243 
250  template<typename T, K_UINT_32 BEG, bool DBG>
251  inline void KVector<T, BEG, DBG>::get(std::istream& is) {
252  if (n_ == 0) {
253  return;
254  }
255 
256  T* ptr = &vimpl_[0];
257  std::string tmp;
258  K_UINT_32 i;
259 
261  is >> tmp;
262  }
263 
265  for (i = 0; i < n_-1; ++i) {
266  is >> ptr[i] >> tmp;
267  }
268  is >> ptr[i];
269  } else {
270  for (i = 0; i < n_; ++i) {
271  is >> ptr[i];
272  }
273  }
274 
276  is >> tmp;
277  }
278  }
279 
286  template<typename T, K_UINT_32 BEG, bool DBG>
287  inline void KVector<T, BEG, DBG>::put(std::ostream& os) const {
288  if (n_ == 0) {
289  return;
290  }
291 
292  const T* ptr = &vimpl_[0];
293  K_UINT_32 i;
294 
295  std::ios::fmtflags f = os.setf(std::ios::scientific,
296  std::ios::floatfield);
297  os.setf(std::ios::showpoint);
298  std::streamsize p = os.precision(currentVectorContext->precision_);
299 
301  for (i = 0; i < n_ - 1; ++i) {
302  os.width(currentVectorContext->width_);
303  os << ptr[i] << currentVectorContext->elemDelim_;
304  }
305  os.width(currentVectorContext->width_);
306  os << ptr[i] << currentVectorContext->endDelim_;
307 
308  os.precision(p);
309  os.flags(f);
310  }
311 
316  template<typename T, K_UINT_32 BEG, bool DBG>
317  inline std::istream& operator>>(std::istream& is,
319  v.get(is);
320  return is;
321  }
322 
327  template<typename T, K_UINT_32 BEG, bool DBG>
328  inline std::ostream& operator<<(std::ostream& os,
329  const KVector<T, BEG, DBG>& v) {
330  v.put(os);
331  return os;
332  }
333 
334 }
335 
336 #endif
void put(std::ostream &os) const
Writes a vector to a stream.
bool skipStartDelim_
Must we skip a word at start of vector ?
unsigned precision_
Number of significant digits to output.
#define KEND_OF_STREAM
Definition: ktypes.hpp:52
void resize(K_UINT_32 n)
Resizes the vector. Resulting vector contents are undefined.
KVector & operator=(const T &a)
Assigns a copy of a to all elements of the vector.
Minimalist vector template class.
Definition: kvector.hpp:72
unsigned width_
Width of output field for nice alignment.
bool skipEndDelim_
Must we skip a word at end of vector ?
KVectorContextImpl(std::string elemDelim=" ", std::string startDelim=std::string(), std::string endDelim=std::string(), unsigned prec=4)
Constructor.
KVectorContextImpl * currentVectorContext
Refers to the currently selected vector printing context.
Definition: kstatics.cpp:61
void swap(T &a, T &b)
Swaps objects a and b.
Definition: ktypes.hpp:104
void get(std::istream &is)
Reads a vector from a stream.
Contains necessary informations to print a formatted KVector.
bool skipElemDelim_
Must we skip a word between elements ?
#define KOSTRINGSTREAM
Definition: ktypes.hpp:51
T * v_
Pointer to start of vimpl_ array.
Definition: kvector.hpp:151
unsigned long int K_UINT_32
Unsigned 32-bits integral type.
Definition: ktypes.hpp:68
void swap(KVector &v)
Constant-time swap function between two vectors.
T & operator()(K_UINT_32 i)
Returns the i'th element.
std::string endDelim_
Ending string after last vector element.
K_UINT_32 n_
Number of elements in vimpl_.
Definition: kvector.hpp:152
std::ostream & operator<<(std::ostream &os, const KMatrix< T, BEG, DBG > &M)
Writes a matrix to a stream.
std::string elemDelim_
Delimiter string between vector elements.
Exception class for access to out-of-bound elements.
Definition: ktypes.hpp:85
std::vector< T > vimpl_
Underlying vector implementation.
Definition: kvector.hpp:144
void assign(K_UINT_32 n, const T *v)
Copies an array of n instances of T.
K_UINT_32 size() const
Returns the vector size.
~KVector()
Destructor.
std::istream & operator>>(std::istream &is, KMatrix< T, BEG, DBG > &M)
Reads a matrix from a stream.
KVector()
Default constructor. Creates an empty vector.
std::string startDelim_
Starting string before first vector element.


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45