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 KSSTREAM_HEADER
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
00132
00133
00134
00135
00136
00137 if (i >= n_ + BEG) {
00138 KOSTRINGSTREAM oss;
00139 oss << "Trying to access element " << i << " not included in ["
00140 << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
00141 throw OutOfBoundError(oss.str());
00142 }
00143 }
00144 return v_[i];
00145 }
00146
00151 template<typename T, K_UINT_32 BEG, bool DBG>
00152 inline const T& KVector<T, BEG, DBG>::operator()(K_UINT_32 i) const {
00153 if (DBG) {
00154
00155
00156
00157
00158
00159
00160
00161 if (i >= n_ + BEG) {
00162 KOSTRINGSTREAM oss;
00163 oss << "Trying to access element " << i << " not included in ["
00164 << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
00165 throw OutOfBoundError(oss.str());
00166 }
00167 }
00168 return v_[i];
00169 }
00170
00173 template<typename T, K_UINT_32 BEG, bool DBG>
00174 inline K_UINT_32 KVector<T, BEG, DBG>::size() const {
00175 return n_;
00176 }
00177
00183 template<typename T, K_UINT_32 BEG, bool DBG>
00184 inline void KVector<T, BEG, DBG>::resize(K_UINT_32 n) {
00185
00186 if (n == n_) {
00187 return;
00188 }
00189
00190 vimpl_.resize(n);
00191 v_ = (n != 0) ? &vimpl_[0] - BEG : 0;
00192 n_ = n;
00193 }
00194
00197 template<typename T, K_UINT_32 BEG, bool DBG>
00198 inline KVector<T, BEG, DBG>& KVector<T, BEG, DBG>::operator=(const T& a) {
00199 if (n_ == 0) {
00200 return *this;
00201 }
00202
00203 T* ptr = &vimpl_[0];
00204 const T* end = ptr + n_;
00205
00206 while (ptr != end) {
00207 *ptr++ = a;
00208 }
00209 return *this;
00210 }
00211
00215 template<typename T, K_UINT_32 BEG, bool DBG>
00216 inline KVector<T, BEG, DBG>&
00217 KVector<T, BEG, DBG>::operator=(const KVector& v) {
00218 KVector temp(v);
00219 swap(temp);
00220 return *this;
00221 }
00222
00226 template<typename T, K_UINT_32 BEG, bool DBG>
00227 inline void KVector<T, BEG, DBG>::assign(K_UINT_32 n, const T* v) {
00228 KVector temp(n, v);
00229 swap(temp);
00230 }
00231
00237 template<typename T, K_UINT_32 BEG, bool DBG>
00238 inline void KVector<T, BEG, DBG>::swap(KVector& v) {
00239 vimpl_.swap(v.vimpl_);
00240 Util::swap(v_, v.v_);
00241 Util::swap(n_, v.n_);
00242 }
00243
00250 template<typename T, K_UINT_32 BEG, bool DBG>
00251 inline void KVector<T, BEG, DBG>::get(std::istream& is) {
00252 if (n_ == 0) {
00253 return;
00254 }
00255
00256 T* ptr = &vimpl_[0];
00257 std::string tmp;
00258 K_UINT_32 i;
00259
00260 if (currentVectorContext->skipStartDelim_) {
00261 is >> tmp;
00262 }
00263
00264 if (currentVectorContext->skipElemDelim_) {
00265 for (i = 0; i < n_-1; ++i) {
00266 is >> ptr[i] >> tmp;
00267 }
00268 is >> ptr[i];
00269 } else {
00270 for (i = 0; i < n_; ++i) {
00271 is >> ptr[i];
00272 }
00273 }
00274
00275 if (currentVectorContext->skipEndDelim_) {
00276 is >> tmp;
00277 }
00278 }
00279
00286 template<typename T, K_UINT_32 BEG, bool DBG>
00287 inline void KVector<T, BEG, DBG>::put(std::ostream& os) const {
00288 if (n_ == 0) {
00289 return;
00290 }
00291
00292 const T* ptr = &vimpl_[0];
00293 K_UINT_32 i;
00294
00295 std::ios::fmtflags f = os.setf(std::ios::scientific,
00296 std::ios::floatfield);
00297 os.setf(std::ios::showpoint);
00298 std::streamsize p = os.precision(currentVectorContext->precision_);
00299
00300 os << currentVectorContext->startDelim_;
00301 for (i = 0; i < n_ - 1; ++i) {
00302 os.width(currentVectorContext->width_);
00303 os << ptr[i] << currentVectorContext->elemDelim_;
00304 }
00305 os.width(currentVectorContext->width_);
00306 os << ptr[i] << currentVectorContext->endDelim_;
00307
00308 os.precision(p);
00309 os.flags(f);
00310 }
00311
00316 template<typename T, K_UINT_32 BEG, bool DBG>
00317 inline std::istream& operator>>(std::istream& is,
00318 KVector<T, BEG, DBG>& v) {
00319 v.get(is);
00320 return is;
00321 }
00322
00327 template<typename T, K_UINT_32 BEG, bool DBG>
00328 inline std::ostream& operator<<(std::ostream& os,
00329 const KVector<T, BEG, DBG>& v) {
00330 v.put(os);
00331 return os;
00332 }
00333
00334 }
00335
00336 #endif