00001
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef TBCI_BVECTOR_H
00014 #define TBCI_BVECTOR_H
00015
00016 #include "basics.h"
00017 #include "vector_sig.h"
00018
00019
00020 #if !defined(NO_GD) && !defined(AUTO_DECL)
00021 # include "bvector_gd.h"
00022 #endif
00023
00024 NAMESPACE_TBCI
00025
00027 #ifdef EXCEPT
00028
00029
00030 class VecErr : public NumErr
00031 {
00032 public:
00033 VecErr()
00034 : NumErr("Error in Vector library") {}
00035 VecErr(const char* t, const long i = 0)
00036 : NumErr(t, i) {}
00037 VecErr(const VecErr& ve)
00038 : NumErr(ve) {}
00039 virtual ~VecErr() throw() {}
00040 };
00041 #endif
00042
00043 #ifdef PRAGMA_I
00044 # pragma interface "bvector.h"
00045 #endif
00046
00047 template <typename T> class TMatrix;
00048 template <typename T> class Matrix;
00049 template <typename T> class F_TMatrix;
00050 template <typename T> class F_Matrix;
00051 template <typename T> class TVector;
00052 template <typename T> class TSVector;
00053 template <typename T> class Vector;
00054
00055
00056
00057
00058
00065 template <typename T>
00066 class BVector : public BVector_Sig<T>
00067 {
00068 protected:
00069 typedef T value_type;
00070 typedef T element_type;
00071 typedef T aligned_value_type TALIGN(MIN_ALIGN2);
00072 T* vec;
00073 unsigned long dim;
00074 bool keep;
00075
00076 public:
00077 friend class TMatrix<T>;
00078 friend class F_TMatrix<T>;
00079 friend class F_Matrix<T>;
00080 #ifdef HAVE_BCXX_INHER_BREAKS_FRIEND_BUG
00081 friend class TVector<T>;
00082 friend class Vector<T>;
00083
00084 #endif
00085
00086
00087 explicit BVector (const unsigned long = 0);
00088 BVector (const T&, const unsigned long);
00089 BVector (const BVector<T>&) HOT;
00090 #ifndef NO_POD
00091
00092 BVector(const vararg va, ...);
00093 #endif
00094 inline void destroy ();
00095 ~BVector ();
00096
00097
00098 BVector<T>& init_0 ()
00099 { if (UNLIKELY(dim)) TBCICLEAR (vec, T, dim); return *this; }
00100 BVector<T>& fill (const T&) HOT;
00101
00102
00103 friend NOINST BVector<T>& FRIEND_TBCI2__ bvfillm FGD (BVector<T>&, const Matrix<T>& m);
00104
00106 BVector<T>& resize (const BVector<T>&);
00107 BVector<T>& resize (const unsigned long);
00108 BVector<T>& resize (const T&, const unsigned long);
00109 BVector<T>& cheapdownsize (const unsigned long);
00110
00111 BVector<T>& clear () { return this->fill((T)0); };
00113 BVector<T>& append (const T&);
00115 BVector<T>& push_back (const T& value) { append(value); return *this; }
00116 BVector<T>& append (const BVector<T>&);
00117 BVector<T>& remove (const unsigned long);
00118 BVector<T>& revert ();
00119 BVector<T>& bubble_sort ();
00120
00121 BVector<T>& swap (BVector<T>& v);
00122
00123
00124 T& operator () (const unsigned long) HOT;
00125 typename tbci_traits<T>::const_refval_type
00126 operator () (const unsigned long) const HOT;
00127 const T& getcref (const unsigned long) const;
00128
00129
00130 T& operator [] (const unsigned long i) { return this->operator() (i); }
00131 typename tbci_traits<T>::const_refval_type
00132 operator [] (const unsigned long i) const { return this->operator() (i); }
00133
00134 inline unsigned long size () const HOT { return dim; }
00135
00136
00137 T* const & vecptr () const { return vec; }
00138
00143 typedef T* iterator;
00144 iterator begin () { return vec; }
00145 iterator end () { return vec+dim; }
00146 typedef const T* const_iterator;
00147 const_iterator begin () const { return vec; }
00148 const_iterator end () const { return vec+dim; }
00149
00150
00151 BVector<T>& setptr (T* pointer) { vec = pointer; return *this; }
00152 BVector<T>& setsize (const unsigned long size) { dim = size; return *this; }
00153
00154 T* const & get_fortran_vector () const { return vecptr (); }
00155 void set_fortran_vector (T* pointer) { setptr (pointer); }
00156
00157
00158 BVector<T>& operator = (const T& a) { return fill (a); }
00160 BVector<T>& operator = (const BVector<T>&) HOT;
00161 BVector<T>& alias (const BVector<T>& bv)
00162 { destroy (); vec = bv.vec; dim = bv.dim; keep = true; return *this; }
00164 BVector<T>& copy (const BVector<T>& bv);
00165
00166
00167 bool operator == (const BVector<T>&) const HOT;
00168 bool operator != (const BVector<T>& bv) const { return !(*this == bv); }
00169 bool operator <= (const BVector<T>& bv) const;
00170 bool operator >= (const BVector<T>& bv) const;
00171 bool operator < (const BVector<T>& bv) const { return !((*this) >= bv); }
00172 bool operator > (const BVector<T>& bv) const { return !((*this) <= bv); }
00173
00174
00175 bool contains (const T&, unsigned long * = 0) const;
00176
00177
00178 friend STD__ ostream& operator << FGD (STD__ ostream&, const BVector<T>&);
00179 friend STD__ istream& operator >> FGD (STD__ istream&, BVector<T>&);
00180
00181
00182
00183 BVector<T> concat (const BVector<T>&) const;
00184
00185 static const char* vec_info() { return "BVector"; }
00186
00187 #ifndef HAVE_PROMOTION_BUG
00188 # ifndef HAVE_GCC295_TMPLFRNDCLS_BUG
00189
00190 template <typename U> friend class BVector;
00191 template <typename U> explicit BVector (const BVector<U>& bv)
00192 : dim (bv.dim), keep (bv.keep)
00193 {
00194 if (LIKELY(dim)) vec = NEW (T, dim); else vec = 0;
00195 if (UNLIKELY(!vec)) dim = 0;
00196 for (unsigned long i = 0; i < dim; i++) vec[i] = bv.vec[i];
00197 }
00198 # else
00199 template <typename U> explicit BVector (const BVector<U>& bv)
00200 : keep (false)
00201 {
00202 dim = bv.size();
00203 if (LIKELY(dim)) vec = NEW (T, dim); else vec = 0;
00204 if (UNLIKELY(!vec)) dim = 0;
00205 for (unsigned long i = 0; i < dim; i++) vec[i] = bv(i);
00206 }
00207 # endif
00208 #endif
00209
00210 #ifdef HAVE_BCXX_INHER_BREAKS_FRIEND_BUG
00211
00212 friend NOINST void FRIEND_TBCI2__ do_mat_vec_mult FGD (const unsigned start, const unsigned end, \
00213 TVector<T> * res, const Matrix<T> * mat, const Vector<T> * vec);
00214 friend NOINST void FRIEND_TBCI2__ do_mat_vec_transmult FGD (const unsigned start, const unsigned end, \
00215 TVector<T> * res, const Matrix<T> * mat, const Vector<T> * vec);
00216 #endif
00217
00218 };
00219
00220
00221
00222
00223
00224 template <typename T>
00225 INLINE BVector<T>::BVector (const unsigned long c)
00226 : vec ((T*)0), dim (c), keep (false)
00227 {
00228 if (UNLIKELY(c)) {
00229 vec = NEW (T, c);
00230 if (UNLIKELY(!vec))
00231 dim = 0;
00232 }
00233 }
00234
00235 #ifndef NO_POD
00236 template <typename T>
00237 INLINE BVector<T>::BVector (const vararg va, ...)
00238 : vec ((T*)0), dim (va), keep (false)
00239 {
00240 if (LIKELY(dim))
00241 vec = NEW (T, dim);
00242 if (UNLIKELY(!vec))
00243 dim = 0;
00244 va_list vl;
00245 va_start (vl, va);
00246 for (unsigned long i=0; i < dim; ++i)
00247 vec[i] = va_arg (vl, T);
00248 va_end (vl);
00249 }
00250 #endif
00251
00252 template <typename T>
00253 inline void BVector<T>::destroy ()
00254 {
00255 if (LIKELY(dim))
00256 TBCIDELETE(T, vec, dim);
00257 }
00258
00259 template <typename T>
00260 INLINE BVector<T>::~BVector ()
00261 {
00262 if (UNLIKELY(!keep))
00263 destroy ();
00264 }
00265
00266 template <typename T>
00267 INLINE BVector<T>::BVector (const BVector<T>& v)
00268 : vec ((T*)0), dim(0), keep (false)
00269 {
00270 if (LIKELY(v.dim)) {
00271 vec = NEW (T, v.dim);
00272 if (LIKELY(vec)) {
00273 dim = v.dim;
00274 TBCICOPY(vec, v.vec, T, dim);
00275 }
00276 }
00277 }
00278
00279
00280 template <typename T>
00281 INLINE BVector<T>::BVector (const T& value, const unsigned long c)
00282 : vec ((T*)0), dim (c), keep (false)
00283 {
00284 if (LIKELY(dim))
00285 vec = NEW (T, dim);
00286 if (LIKELY(vec))
00287 TBCIFILL (vec, value, T, dim);
00288 else
00289 dim = 0;
00290 }
00291
00292 template <typename T>
00293 BVector<T>& BVector<T>::cheapdownsize (const unsigned long nd)
00294 {
00295 BCHK (nd > dim, VecErr, cheapdownsize does not upsize, nd, *this);
00296 if (UNLIKELY(!nd))
00297 return this->resize (nd);
00298 dim = nd;
00299 return *this;
00300 }
00301
00302 #ifndef LAPACK_INLINE
00303 # define LAPACK_INLINE
00304 #endif
00305 template <typename T>
00306 LAPACK_INLINE BVector<T>& BVector<T>::resize (const unsigned long c)
00307 {
00308 if (LIKELY(c == dim))
00309 return *this;
00310 if (UNLIKELY(c == 0)) {
00311 if (LIKELY(dim))
00312 TBCIDELETE(T, vec, dim);
00313 dim = c;
00314 return *this;
00315 }
00316 #if defined(BOUNDCHECK) && !defined(EXCEPT) && !defined(ABORT_ON_ERR)
00317 T* tmp = vec;
00318 #endif
00319 if (LIKELY(dim == 0))
00320 vec = NEW (T, c);
00321 else
00322 REALLOC(vec,dim,T,c);
00323 BCHK (!vec, VecErr, Realloc failed in resize, 0, (vec=tmp, *this));
00324 dim = c;
00325 return *this;
00326 }
00327
00328 template <typename T>
00329 BVector<T>& BVector<T>::resize (const T& value, const unsigned long c)
00330 {
00331 if (UNLIKELY(dim != c)) {
00332 if (LIKELY(dim))
00333 TBCIDELETE(T, vec, dim);
00334 dim = c;
00335 if (UNLIKELY(c))
00336 vec = NEW (T, c);
00337 else
00338 vec = (T*)0;
00339 }
00340 if (UNLIKELY(dim && vec))
00341 return this->fill (value);
00342 else
00343 dim = 0;
00344 return *this;
00345 }
00346
00347 template <typename T>
00348 BVector<T>& BVector<T>::resize (const BVector<T>& v)
00349 {
00350 if (UNLIKELY(dim))
00351 TBCIDELETE (T, vec, dim);
00352 dim = v.dim;
00353 if (UNLIKELY(v.dim)) {
00354 vec = NEW (T, v.dim);
00355 if (LIKELY(vec))
00356 TBCICOPY(vec, v.vec, T, dim);
00357 else
00358 dim = 0;
00359 } else
00360 vec = (T*)0;
00361 return *this;
00362 }
00363
00364 template <typename T>
00365 INLINE BVector<T>& BVector<T>::fill (const T& val)
00366 {
00367 TBCIFILL(vec, val, T, dim);
00368 return *this;
00369 }
00370
00371 template <typename T>
00372 INLINE BVector<T>& BVector<T>::revert ()
00373 {
00374 BVector<T> buf(dim);
00375 for (register unsigned long t = 0; t < dim; t++)
00376 buf[t] = vec[dim-t-1];
00377 return *this = buf;
00378 }
00379
00380 template <typename T>
00381 BVector<T>& BVector<T>::bubble_sort ()
00382 {
00383 T temp;
00384 if(dim > 1) {
00385 for (register unsigned long i = 0; i < dim-1; i++)
00386 for (register unsigned long j = dim-1; i < j; j--) {
00387 if ( vec[j] < vec[j-1] ) {
00388 temp = vec[j];
00389 vec[j] = vec[j-1];
00390 vec[j-1] = temp;
00391 }
00392 }
00393 }
00394 return *this;
00395 }
00396
00397
00398 template <typename T>
00399 INLINE BVector<T>& BVector<T>::append (const T& val)
00400 {
00401 #if defined(BOUNDCHECK) && !defined(EXCEPT) && !defined(ABORT_ON_ERR)
00402 T* tmp = vec;
00403 #endif
00404 REALLOC (vec,dim,T,dim+1);
00405 BCHK (!vec, VecErr, Realloc failed in append, 0, (vec=tmp, *this));
00406 vec[dim++] = val;
00407 return *this;
00408 }
00409
00410
00411 template <typename T>
00412 INLINE BVector<T>& BVector<T>::append (const BVector<T>& v)
00413 {
00414 #if defined(BOUNDCHECK) && !defined(EXCEPT) && !defined(ABORT_ON_ERR)
00415 T* tmp = vec;
00416 #endif
00417 REALLOC (vec, dim, T, dim+v.dim);
00418 BCHK (!vec, VecErr, Realloc failed in append, 0, (vec=tmp, *this));
00419 TBCICOPY(vec+dim, v.vec, T, v.dim);
00420 dim += v.dim;
00421 return *this;
00422 }
00423
00424
00425 template <typename T>
00426 BVector<T>& BVector<T>::remove (const unsigned long i)
00427 {
00428 BCHK (i>=dim, VecErr, Remove idx out of range, i, *this);
00429 T* tmp = NEW (T, dim-1);
00430 BCHK (!tmp, VecErr, Memory allocation failed in remove, (dim-1)*sizeof(T), *this);
00431 if (UNLIKELY(i))
00432 TBCICOPY (tmp, vec, T, i);
00433 dim -= 1;
00434 if (UNLIKELY(dim-i))
00435 TBCICOPY (&tmp[i], &vec[i+1], T, (dim-i));
00436 TBCIDELETE(T, vec, dim+1);
00437 vec = tmp;
00438 return *this;
00439 }
00440
00441 template <typename T>
00442 INLINE BVector<T>& BVector<T>::swap (BVector<T>& v)
00443 {
00444
00445 SWAP (v.dim, dim);
00446 SWAP (v.vec, vec);
00447 SWAP (v.keep, keep);
00448 return *this;
00449 }
00450
00451 template <typename T>
00452 inline typename tbci_traits<T>::const_refval_type
00453 BVector<T>::operator () (const unsigned long i) const
00454 {
00455 BCHK(i<0 || i>=dim, VecErr, Illegal index, i, vec[0]);
00456 return vec[i];
00457 }
00458
00459 template <typename T>
00460 inline T& BVector<T>::operator () (const unsigned long i)
00461 {
00462 BCHK(i<0 || i>=dim, VecErr, Illegal index, i, vec[0]);
00463 return vec[i];
00464 }
00465
00466 template <typename T>
00467 inline const T& BVector<T>::getcref (const unsigned long i) const
00468 {
00469 BCHK(i<0 || i>=dim, VecErr, Illegal index, i, vec[0]);
00470 return vec[i];
00471 }
00472
00473
00474 template <typename T>
00475 INLINE BVector<T>& BVector<T>::operator = (const BVector<T>& a)
00476 {
00477 BCHK(dim != a.dim, VecErr, Assignment from wrong dim vector, a.dim, *this);
00478 TBCICOPY (vec, a.vec, T, dim);
00479 return *this;
00480 }
00481
00482 template <typename T>
00483 inline BVector<T>& BVector<T>::copy (const BVector<T>& a)
00484 {
00485
00486 return this->resize (a);
00487
00488
00489 }
00490
00493 template <typename T>
00494 bool BVector<T>::operator == (const BVector<T>& bv) const
00495 {
00496 if (LIKELY(dim != bv.dim))
00497 return false;
00498 if (LIKELY(vec == bv.vec || dim == 0))
00499 return true;
00500 if (TBCICOMP (vec, bv.vec, T, dim))
00501 return false;
00502 else
00503 return true;
00504 }
00505
00506
00507 template<typename T>
00508 bool BVector<T>::operator <= (const BVector<T>& bv) const
00509 {
00510 BCHK(dim != bv.dim, VecErr, comparison between wrong dim vectors, bv.dim, false);
00511 for (unsigned long i=0; i<dim; i++)
00512 if (UNLIKELY(vec[i] > bv.vec[i]))
00513 return false;
00514 return true;
00515 }
00516
00517
00518 template<typename T>
00519 bool BVector<T>::operator >= (const BVector<T>& bv) const
00520 {
00521 BCHK(dim != bv.dim, VecErr, comparison between wrong dim vectors, bv.dim, false);
00522 for (unsigned long i=0; i<dim; i++)
00523 if( UNLIKELY(vec[i] < bv.vec[i]) )
00524 return false;
00525 return true;
00526 }
00527
00528
00529 template <typename T>
00530 inline STD__ ostream& operator << (STD__ ostream& os, const BVector<T>& v)
00531 {
00532 #ifdef VEC_COL
00533 for (unsigned long i = 0; i < v.dim; i++)
00534 os << v.vec[i] << "\n";
00535 #else
00536 for (unsigned long i = 0; i < v.dim; i++)
00537 os << v.vec[i] << " ";
00538 #endif
00539 return os.flush();
00540 }
00541
00542 #ifdef PTR
00543 template <typename T>
00544 STD__ istream& operator >> (STD__ istream& in, BVector<T>& v)
00545 {
00546 STD__ cerr << "BVector (" << __FILE__ << ":" << __LINE__
00547 <<") Input on pointers not possible!" << STD__ endl;
00548 abort();
00549 return in;
00550 }
00551 #else
00552 template <typename T>
00553 STD__ istream& operator >> (STD__ istream& in, BVector<T>& v)
00554 {
00555 T r ALIGN(MIN_ALIGN) = 0;
00556 char s = ',';
00557
00558
00559
00560
00561
00562
00563
00564 if( !( in >> r ) )
00565 return in;
00566 v.vec[0] = r;
00567 for (unsigned long i = 1; i < v.dim; i++) {
00568 in >> s;
00569 if (s != ',') in.putback(s);
00570 if( !( in >> r ) )
00571 return in;
00572 v.vec[i] = r;
00573 }
00574 return in;
00575 }
00576 #endif
00577
00578 template <typename T>
00579 inline bool BVector<T>::contains (const T& val, unsigned long *ind) const
00580 {
00581 for (register unsigned long i = 0; i < dim; ++i)
00582 if (UNLIKELY(val == vec[i])) {
00583 if (ind)
00584 *ind = i;
00585 return true;
00586 }
00587 return false;
00588 }
00589
00590
00591 template <typename T>
00592 BVector<T> BVector<T>::concat (const BVector<T>& b2) const
00593 {
00594 BVector<T> c (dim + b2.dim);
00595 TBCICOPY(c.vec, vec, T, dim);
00596 TBCICOPY(c.vec+dim, b2.vec, T, dim);
00597 return c;
00598 }
00599
00600
00601 INST(template <typename T> class BVector friend BVector<T> concat (const BVector<T>&, const BVector<T>&);)
00602 template <typename T>
00603 inline BVector<T> concat (const BVector<T>& b1, const BVector<T>& b2)
00604 { return (b1.concat (b2)); }
00605
00606
00607 NAMESPACE_END
00608
00609 #endif