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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef BZ_VECTOR_H
00038 #define BZ_VECTOR_H
00039
00040 #include <blitz/blitz.h>
00041 #include <blitz/memblock.h>
00042 #include <blitz/range.h>
00043 #include <blitz/listinit.h>
00044
00045 BZ_NAMESPACE(blitz)
00046
00047
00048 template<typename P_numtype> class VectorIter;
00049 template<typename P_numtype> class VectorIterConst;
00050 template<typename P_expr> class _bz_VecExpr;
00051 template<typename P_numtype> class VectorPick;
00052 template<typename P_numtype> class Random;
00053
00054
00055
00056 template<typename P_numtype>
00057 class Vector : protected MemoryBlockReference<P_numtype> {
00058
00059 private:
00060 typedef MemoryBlockReference<P_numtype> T_base;
00061 using T_base::data_;
00062
00063 public:
00065
00067
00068 typedef P_numtype T_numtype;
00069 typedef Vector<T_numtype> T_vector;
00070 typedef VectorIter<T_numtype> T_iterator;
00071 typedef VectorIterConst<T_numtype> T_constIterator;
00072 typedef VectorPick<T_numtype> T_pick;
00073 typedef Vector<int> T_indexVector;
00074
00076
00078
00079
00080
00081
00082 Vector()
00083 {
00084 length_ = 0;
00085 stride_ = 0;
00086 }
00087
00088
00089
00090 Vector(const Vector<T_numtype>& vec)
00091 : MemoryBlockReference<T_numtype>(const_cast<Vector<T_numtype>&>(vec))
00092 {
00093 length_ = vec.length_;
00094 stride_ = vec.stride_;
00095 }
00096
00097 explicit Vector(int length)
00098 : MemoryBlockReference<T_numtype>(length)
00099 {
00100 length_ = length;
00101 stride_ = 1;
00102 }
00103
00104 Vector(const Vector<T_numtype>& vec, Range r)
00105 : MemoryBlockReference<T_numtype>(const_cast<Vector<T_numtype>&>(vec),
00106 r.first() * vec.stride())
00107 {
00108 BZPRECONDITION((r.first() >= 0) && (r.first() < vec.length()));
00109 BZPRECONDITION((r.last(vec.length()-1) >= 0)
00110 && (r.last(vec.length()-1) < vec.length()));
00111 length_ = (r.last(vec.length()-1) - r.first()) / r.stride() + 1;
00112 stride_ = vec.stride() * r.stride();
00113 }
00114
00115 Vector(int length, T_numtype initValue)
00116 : MemoryBlockReference<T_numtype>(length)
00117 {
00118 length_ = length;
00119 stride_ = 1;
00120 (*this) = initValue;
00121 }
00122
00123 Vector(int length, T_numtype firstValue, T_numtype delta)
00124 : MemoryBlockReference<T_numtype>(length)
00125 {
00126 length_ = length;
00127 stride_ = 1;
00128 for (int i=0; i < length; ++i)
00129 data_[i] = firstValue + i * delta;
00130 }
00131
00132 template<typename P_distribution>
00133 Vector(int length, Random<P_distribution>& random)
00134 : MemoryBlockReference<T_numtype>(length)
00135 {
00136 length_ = length;
00137 stride_ = 1;
00138 (*this) = random;
00139 }
00140
00141 template<typename P_expr>
00142 Vector(_bz_VecExpr<P_expr> expr)
00143 : MemoryBlockReference<T_numtype>(expr._bz_suggestLength())
00144 {
00145 length_ = expr._bz_suggestLength();
00146 stride_ = 1;
00147 (*this) = expr;
00148 }
00149
00150
00151
00152
00153 Vector(int length, T_numtype* restrict data, int stride = 1)
00154 : MemoryBlockReference<T_numtype>(length, data, neverDeleteData)
00155 {
00156 length_ = length;
00157 stride_ = stride;
00158 }
00159
00160
00161 Vector(Range r)
00162 : MemoryBlockReference<T_numtype>(r._bz_suggestLength())
00163 {
00164 length_ = r._bz_suggestLength();
00165 stride_ = 1;
00166 (*this) = _bz_VecExpr<Range>(r);
00167 }
00168
00170
00172
00173
00174
00175
00176
00177
00178
00179
00180 void assertUnitStride()
00181 {
00182 BZPRECONDITION(stride_ == 1);
00183 stride_ = 1;
00184 }
00185
00186 T_iterator beginFast() { return T_iterator(*this); }
00187 T_constIterator beginFast() const { return T_constIterator(*this); }
00188
00189 T_vector copy() const;
00190
00191
00192
00193
00194 T_numtype * restrict data()
00195 { return data_; }
00196
00197 const T_numtype * restrict data() const
00198 { return data_; }
00199
00200 bool isUnitStride() const
00201 { return stride_ == 1; }
00202
00203 int length() const
00204 { return length_; }
00205
00206 void makeUnique();
00207
00208
00209
00210
00211
00212 void reference(T_vector&);
00213
00214 void resize(int length);
00215
00216 void resizeAndPreserve(int newLength);
00217
00218
00219
00220 T_vector reverse()
00221 { return T_vector(*this,Range(length()-1,0,-1)); }
00222
00223 int stride() const
00224 { return stride_; }
00225
00226 operator _bz_VecExpr<VectorIterConst<T_numtype> > () const
00227 { return _bz_VecExpr<VectorIterConst<T_numtype> >(beginFast()); }
00228
00230
00231
00232
00234
00235 int _bz_suggestLength() const
00236 { return length_; }
00237
00238 bool _bz_hasFastAccess() const
00239 { return stride_ == 1; }
00240
00241 T_numtype& _bz_fastAccess(int i)
00242 { return data_[i]; }
00243
00244 T_numtype _bz_fastAccess(int i) const
00245 { return data_[i]; }
00246
00247 template<typename P_expr, typename P_updater>
00248 void _bz_assign(P_expr, P_updater);
00249
00250 _bz_VecExpr<T_constIterator> _bz_asVecExpr() const
00251 { return _bz_VecExpr<T_constIterator>(beginFast()); }
00252
00254
00256
00257
00258
00259 T_numtype operator()(int i) const
00260 {
00261 BZPRECONDITION(i < length_);
00262 BZPRECONDITION(stride_ == 1);
00263 return data_[i];
00264 }
00265
00266
00267
00268 T_numtype& restrict operator()(int i)
00269 {
00270 BZPRECONDITION(i < length_);
00271 BZPRECONDITION(stride_ == 1);
00272 return data_[i];
00273 }
00274
00275 T_numtype operator[](int i) const
00276 {
00277 BZPRECONDITION(i < length_);
00278 return data_[i * stride_];
00279 }
00280
00281 T_numtype& restrict operator[](int i)
00282 {
00283 BZPRECONDITION(i < length_);
00284 return data_[i * stride_];
00285 }
00286
00287 T_vector operator()(Range r)
00288 {
00289 return T_vector(*this, r);
00290 }
00291
00292 T_vector operator[](Range r)
00293 {
00294 return T_vector(*this, r);
00295 }
00296
00297 T_pick operator()(T_indexVector i)
00298 {
00299 return T_pick(*this, i);
00300 }
00301
00302 T_pick operator[](T_indexVector i)
00303 {
00304 return T_pick(*this, i);
00305 }
00306
00307
00308
00310
00312
00313
00314 ListInitializationSwitch<T_vector,T_iterator> operator=(T_numtype x)
00315 {
00316 return ListInitializationSwitch<T_vector,T_iterator>(*this, x);
00317 }
00318
00319 T_iterator getInitializationIterator()
00320 { return beginFast(); }
00321
00322 T_vector& initialize(T_numtype);
00323 T_vector& operator+=(T_numtype);
00324 T_vector& operator-=(T_numtype);
00325 T_vector& operator*=(T_numtype);
00326 T_vector& operator/=(T_numtype);
00327 T_vector& operator%=(T_numtype);
00328 T_vector& operator^=(T_numtype);
00329 T_vector& operator&=(T_numtype);
00330 T_vector& operator|=(T_numtype);
00331 T_vector& operator>>=(int);
00332 T_vector& operator<<=(int);
00333
00334
00335
00336 template<typename P_numtype2> T_vector& operator=(const Vector<P_numtype2> &);
00337
00338
00339
00340
00341
00342
00343 template<typename P_numtype2> T_vector& operator+=(const Vector<P_numtype2> &);
00344 template<typename P_numtype2> T_vector& operator-=(const Vector<P_numtype2> &);
00345 template<typename P_numtype2> T_vector& operator*=(const Vector<P_numtype2> &);
00346 template<typename P_numtype2> T_vector& operator/=(const Vector<P_numtype2> &);
00347 template<typename P_numtype2> T_vector& operator%=(const Vector<P_numtype2> &);
00348 template<typename P_numtype2> T_vector& operator^=(const Vector<P_numtype2> &);
00349 template<typename P_numtype2> T_vector& operator&=(const Vector<P_numtype2> &);
00350 template<typename P_numtype2> T_vector& operator|=(const Vector<P_numtype2> &);
00351 template<typename P_numtype2> T_vector& operator>>=(const Vector<P_numtype2> &);
00352 template<typename P_numtype2> T_vector& operator<<=(const Vector<P_numtype2> &);
00353
00354
00355 template<typename P_expr> T_vector& operator=(_bz_VecExpr<P_expr>);
00356 template<typename P_expr> T_vector& operator+=(_bz_VecExpr<P_expr>);
00357 template<typename P_expr> T_vector& operator-=(_bz_VecExpr<P_expr>);
00358 template<typename P_expr> T_vector& operator*=(_bz_VecExpr<P_expr>);
00359 template<typename P_expr> T_vector& operator/=(_bz_VecExpr<P_expr>);
00360 template<typename P_expr> T_vector& operator%=(_bz_VecExpr<P_expr>);
00361 template<typename P_expr> T_vector& operator^=(_bz_VecExpr<P_expr>);
00362 template<typename P_expr> T_vector& operator&=(_bz_VecExpr<P_expr>);
00363 template<typename P_expr> T_vector& operator|=(_bz_VecExpr<P_expr>);
00364 template<typename P_expr> T_vector& operator>>=(_bz_VecExpr<P_expr>);
00365 template<typename P_expr> T_vector& operator<<=(_bz_VecExpr<P_expr>);
00366
00367
00368 template<typename P_numtype2>
00369 T_vector& operator=(const VectorPick<P_numtype2> &);
00370 template<typename P_numtype2>
00371 T_vector& operator+=(const VectorPick<P_numtype2> &);
00372 template<typename P_numtype2>
00373 T_vector& operator-=(const VectorPick<P_numtype2> &);
00374 template<typename P_numtype2>
00375 T_vector& operator*=(const VectorPick<P_numtype2> &);
00376 template<typename P_numtype2>
00377 T_vector& operator/=(const VectorPick<P_numtype2> &);
00378 template<typename P_numtype2>
00379 T_vector& operator%=(const VectorPick<P_numtype2> &);
00380 template<typename P_numtype2>
00381 T_vector& operator^=(const VectorPick<P_numtype2> &);
00382 template<typename P_numtype2>
00383 T_vector& operator&=(const VectorPick<P_numtype2> &);
00384 template<typename P_numtype2>
00385 T_vector& operator|=(const VectorPick<P_numtype2> &);
00386 template<typename P_numtype2>
00387 T_vector& operator>>=(const VectorPick<P_numtype2> &);
00388 template<typename P_numtype2>
00389 T_vector& operator<<=(const VectorPick<P_numtype2> &);
00390
00391
00392 T_vector& operator=(Range);
00393 T_vector& operator+=(Range);
00394 T_vector& operator-=(Range);
00395 T_vector& operator*=(Range);
00396 T_vector& operator/=(Range);
00397 T_vector& operator%=(Range);
00398 T_vector& operator^=(Range);
00399 T_vector& operator&=(Range);
00400 T_vector& operator|=(Range);
00401 T_vector& operator>>=(Range);
00402 T_vector& operator<<=(Range);
00403
00404
00405 template<typename P_distribution>
00406 T_vector& operator=(Random<P_distribution>& random);
00407 template<typename P_distribution>
00408 T_vector& operator+=(Random<P_distribution>& random);
00409 template<typename P_distribution>
00410 T_vector& operator-=(Random<P_distribution>& random);
00411 template<typename P_distribution>
00412 T_vector& operator*=(Random<P_distribution>& random);
00413 template<typename P_distribution>
00414 T_vector& operator/=(Random<P_distribution>& random);
00415 template<typename P_distribution>
00416 T_vector& operator%=(Random<P_distribution>& random);
00417 template<typename P_distribution>
00418 T_vector& operator^=(Random<P_distribution>& random);
00419 template<typename P_distribution>
00420 T_vector& operator&=(Random<P_distribution>& random);
00421 template<typename P_distribution>
00422 T_vector& operator|=(Random<P_distribution>& random);
00423
00425
00427
00428
00429
00430
00431
00432
00433 private:
00434 int length_;
00435 int stride_;
00436 };
00437
00438
00439
00440 template<typename P_numtype>
00441 ostream& operator<<(ostream& os, const Vector<P_numtype>& x);
00442
00443 template<typename P_expr>
00444 ostream& operator<<(ostream& os, _bz_VecExpr<P_expr> expr);
00445
00446 BZ_NAMESPACE_END
00447
00448 #include <blitz/veciter.h>
00449 #include <blitz/vecpick.h>
00450 #include <blitz/vecexpr.h>
00451 #include <blitz/vecglobs.h>
00452 #include <blitz/vector.cc>
00453 #include <blitz/vecio.cc>
00454
00455 #endif // BZ_VECTOR_H