00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef KVECTOR_IMPL_HPP
00028 #define KVECTOR_IMPL_HPP
00029
00032
00033 #include <strstream>
00034
00035 namespace Kalman {
00036
00038
00045 class KVectorContextImpl {
00046 public:
00047
00049
00056 explicit KVectorContextImpl(std::string elemDelim = " ",
00057 std::string startDelim = std::string(),
00058 std::string endDelim = std::string(),
00059 unsigned prec = 4)
00060 : elemDelim_(elemDelim), startDelim_(startDelim),
00061 endDelim_(endDelim), precision_(prec), width_(8+prec) {
00062
00063 std::string ws(" \t\n");
00064 skipElemDelim_ =
00065 ( elemDelim_.find_first_not_of(ws) != std::string::npos );
00066 skipStartDelim_ =
00067 (startDelim_.find_first_not_of(ws) != std::string::npos );
00068 skipEndDelim_ =
00069 ( endDelim_.find_first_not_of(ws) != std::string::npos );
00070 }
00071
00072 std::string elemDelim_;
00073 std::string startDelim_;
00074 std::string endDelim_;
00075 unsigned precision_;
00076 unsigned width_;
00077 bool skipElemDelim_;
00078 bool skipStartDelim_;
00079 bool skipEndDelim_;
00080 };
00081
00083
00086 extern KVectorContextImpl* currentVectorContext;
00087
00088 template<typename T, K_UINT_32 BEG, bool DBG>
00089 inline KVector<T, BEG, DBG>::KVector()
00090 : v_(0), n_(0) {}
00091
00094 template<typename T, K_UINT_32 BEG, bool DBG>
00095 inline KVector<T, BEG, DBG>::KVector(K_UINT_32 n)
00096 : vimpl_(n), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
00097
00100 template<typename T, K_UINT_32 BEG, bool DBG>
00101 inline KVector<T, BEG, DBG>::KVector(K_UINT_32 n, const T& a)
00102 : vimpl_(n, a), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
00103
00110 template<typename T, K_UINT_32 BEG, bool DBG>
00111 inline KVector<T, BEG, DBG>::KVector(K_UINT_32 n, const T* v)
00112 : vimpl_(v, v + n), v_( (n != 0) ? &vimpl_[0] - BEG : 0 ), n_(n) {}
00113
00116 template<typename T, K_UINT_32 BEG, bool DBG>
00117 inline KVector<T, BEG, DBG>::KVector(const KVector& v)
00118 : vimpl_(v.vimpl_), v_( (v.size() != 0) ? &vimpl_[0] - BEG : 0 ),
00119 n_(v.n_) {}
00120
00121 template<typename T, K_UINT_32 BEG, bool DBG>
00122 inline KVector<T, BEG, DBG>::~KVector() {}
00123
00128 template<typename T, K_UINT_32 BEG, bool DBG>
00129 inline T& KVector<T, BEG, DBG>::operator()(K_UINT_32 i) {
00130 if (DBG) {
00131 if (i < BEG || i >= n_ + BEG) {
00132 std::ostrstream oss;
00133 oss << "Trying to access element " << i << " not included in ["
00134 << BEG << ", " << n_ + BEG - 1 << "]." << '\0';
00135 throw OutOfBoundError(oss.str());
00136 }
00137 }
00138 return v_[i];
00139 }
00140
00145 template<typename T, K_UINT_32 BEG, bool DBG>
00146 inline const T& KVector<T, BEG, DBG>::operator()(K_UINT_32 i) const {
00147 if (DBG) {
00148 if (i < BEG || i >= n_ + BEG) {
00149 std::ostrstream oss;
00150 oss << "Trying to access element " << i << " not included in ["
00151 << BEG << ", " << n_ + BEG - 1 << "]." << '\0';
00152 throw OutOfBoundError(oss.str());
00153 }
00154 }
00155 return v_[i];
00156 }
00157
00160 template<typename T, K_UINT_32 BEG, bool DBG>
00161 inline K_UINT_32 KVector<T, BEG, DBG>::size() const {
00162 return n_;
00163 }
00164
00170 template<typename T, K_UINT_32 BEG, bool DBG>
00171 inline void KVector<T, BEG, DBG>::resize(K_UINT_32 n) {
00172
00173 if (n == n_) {
00174 return;
00175 }
00176
00177 vimpl_.resize(n);
00178 v_ = (n != 0) ? &vimpl_[0] - BEG : 0;
00179 n_ = n;
00180 }
00181
00184 template<typename T, K_UINT_32 BEG, bool DBG>
00185 inline KVector<T, BEG, DBG>& KVector<T, BEG, DBG>::operator=(const T& a) {
00186 if (n_ == 0) {
00187 return *this;
00188 }
00189
00190 T* ptr = &vimpl_[0];
00191 const T* end = ptr + n_;
00192
00193 while (ptr != end) {
00194 *ptr++ = a;
00195 }
00196 return *this;
00197 }
00198
00202 template<typename T, K_UINT_32 BEG, bool DBG>
00203 inline KVector<T, BEG, DBG>&
00204 KVector<T, BEG, DBG>::operator=(const KVector& v) {
00205 KVector temp(v);
00206 swap(temp);
00207 return *this;
00208 }
00209
00213 template<typename T, K_UINT_32 BEG, bool DBG>
00214 inline void KVector<T, BEG, DBG>::assign(K_UINT_32 n, const T* v) {
00215 KVector temp(n, v);
00216 swap(temp);
00217 }
00218
00224 template<typename T, K_UINT_32 BEG, bool DBG>
00225 inline void KVector<T, BEG, DBG>::swap(KVector& v) {
00226 vimpl_.swap(v.vimpl_);
00227 Util::swap(v_, v.v_);
00228 Util::swap(n_, v.n_);
00229 }
00230
00237 template<typename T, K_UINT_32 BEG, bool DBG>
00238 inline void KVector<T, BEG, DBG>::get(std::istream& is) {
00239 if (n_ == 0) {
00240 return;
00241 }
00242
00243 T* ptr = &vimpl_[0];
00244 std::string tmp;
00245 K_UINT_32 i;
00246
00247 if (currentVectorContext->skipStartDelim_) {
00248 is >> tmp;
00249 }
00250
00251 if (currentVectorContext->skipElemDelim_) {
00252 for (i = 0; i < n_-1; ++i) {
00253 is >> ptr[i] >> tmp;
00254 }
00255 is >> ptr[i];
00256 } else {
00257 for (i = 0; i < n_; ++i) {
00258 is >> ptr[i];
00259 }
00260 }
00261
00262 if (currentVectorContext->skipEndDelim_) {
00263 is >> tmp;
00264 }
00265 }
00266
00273 template<typename T, K_UINT_32 BEG, bool DBG>
00274 inline void KVector<T, BEG, DBG>::put(std::ostream& os) const {
00275 if (n_ == 0) {
00276 return;
00277 }
00278
00279 const T* ptr = &vimpl_[0];
00280 K_UINT_32 i;
00281
00282 std::ios::fmtflags f = os.setf(std::ios::scientific,
00283 std::ios::floatfield);
00284 os.setf(std::ios::showpoint);
00285 std::streamsize p = os.precision(currentVectorContext->precision_);
00286
00287 os << currentVectorContext->startDelim_;
00288 for (i = 0; i < n_ - 1; ++i) {
00289 os.width(currentVectorContext->width_);
00290 os << ptr[i] << currentVectorContext->elemDelim_;
00291 }
00292 os.width(currentVectorContext->width_);
00293 os << ptr[i] << currentVectorContext->endDelim_;
00294
00295 os.precision(p);
00296 os.flags(f);
00297 }
00298
00303 template<typename T, K_UINT_32 BEG, bool DBG>
00304 inline std::istream& operator>>(std::istream& is,
00305 KVector<T, BEG, DBG>& v) {
00306 v.get(is);
00307 return is;
00308 }
00309
00314 template<typename T, K_UINT_32 BEG, bool DBG>
00315 inline std::ostream& operator<<(std::ostream& os,
00316 const KVector<T, BEG, DBG>& v) {
00317 v.put(os);
00318 return os;
00319 }
00320
00321 }
00322
00323 #endif