// $Id: BiVector.cc,v 1.6 2002/10/22 16:20:54 mf Exp $ // David Sachs - Fermilab - April 2002 // // 02-10-22 MF Repaired V,size() (meant to be V.size) in 2 places // 02-10-22 MF Added a moot return*this after exit() in op=() to make code legal // 02-10-22 MF Corrected resize method, which due to a statement n == nn had been a NOP // #include "CovMatrixInternal.h" // Bivector routines namespace CovMatrices { BiVector::BiVector(const std::vector& va, const std::vector& vb) : n(va.size()), v0(va), v1(vb) { if ( n>0 && n == vb.size() ) return; std::ostringstream msg; msg << " **** Bivector( const std::vector&, const std::vector& ) ****\n"; msg << "A BiVector must be constructed from equal sized nonempty vectors\n\n"; msg << "Sizes of supplied vectors were " << va.size(); msg << " and " << vb.size() << std::endl; CovMatrixThrow(msg.str()); exit(2); } BiVector::BiVector( const std::vector& V ) : n(V.size()/2), v0(n), v1(n) { if (V.size() == n+n && n > 0) { memcpy(&v0[0], &V[0], n*sizeof(double)); memcpy(&v1[0], &V[n], n*sizeof(double)); return; } std::ostringstream msg; msg << " **** Bivector( const vector& ) ****\n"; msg << "A BiVector must be constructed from a nonempty vectors\n"; msg << "containing an even number of elenments.\n"; msg << "Size of supplied vector was " << V.size() << std::endl; CovMatrixThrow(msg.str()); exit(2); } BiVector& BiVector::operator=( const std::vector& V ) { int m = V.size()/2; if (V.size() == m+m && m > 0) { v0.resize(m); v1.resize(m); n = m; memcpy(&v0[0], &V[0], n*sizeof(double)); memcpy(&v1[0], &V[n], n*sizeof(double)); return *this; } std::ostringstream msg; msg << " **** Bivector = const vector ****\n"; msg << "A BiVector can only be assigned from a nonempty vectors\n"; msg << "containing an even number of elenments.\n"; msg << "Size of supplied vector was " << V.size() << std::endl; CovMatrixThrow(msg.str()); exit(2); return *this; } void BiVector::dump( std::vector & V) const { V.resize(n+n); memcpy(&V[0],&v0[0],n*sizeof(double)); memcpy(&V[n],&v1[0],n*sizeof(double)); } void BiVector::put( std::ostream& o) const { o << "{ "; CovMatrixPutVector( o, v0 ); o << ' '; CovMatrixPutVector( o, v1 ); o << " }" ; } void BiVector::printv( std::ostream& o, const std::vector& v) { CovMatrixPutVector( o, v ); o << std::endl; } // Resize a BiVector without guaranteeing element contents void BiVector::resize(int nn) { if (n == nn) return; // No action needed if ( nn >= 0) { n = nn; v0.resize(n); v1.resize(n); } else { std::ostringstream msg; msg << "**** BiVector.resize( int ) ****"; msg << "Illegal requested size: " << nn << std::endl; CovMatrixThrow(msg.str()); exit(2); } } } // namespace CovMatrices