source: Sophya/trunk/SophyaLib/TArray/tvector.cc@ 4035

Last change on this file since 4035 was 4035, checked in by ansari, 14 years ago

1/ modif mineure ds TArray<T>::::ReadASCII() au print level global de BaseArray
2/ Correction bug gestion memoire au niveau des constructeurs de copie TArray/TMatrix/TVector
avec un BaseArray en argument. Ajout argument optionnel bool pack a ces constructeurs
3/ On autorise desormais la creation des objets TArray/TMatrix/TVector par constructeur de
copie sur des objets non alloues

Reza, 14/11/2011

File size: 9.0 KB
RevLine 
[4035]1// $Id: tvector.cc,v 1.29 2011-11-14 16:28:25 ansari Exp $
[762]2// C.Magneville 04/99
[2615]3#include "sopnamsp.h"
[762]4#include "machdefs.h"
5#include <stdlib.h>
6#include "pexceptions.h"
[3831]7
8#define TVECTOR_CC_BFILE // avoid extern template declarations
[762]9#include "tvector.h"
10
[3751]11namespace SOPHYA {
12
[926]13/*!
[3751]14 \class TVector
[926]15 \ingroup TArray
16
[2267]17 The TVector class specializes the TMatrix class for representing
[3857]18 row or column vectors. TVector<T> objects can be initialised or converted
19 to std::vector<T>.
[2267]20
21 \b Vector is a typedef for double precision floating point vectors ( TVector<r_8> ).
22
23 \sa SOPHYA::TArray SOPHYA::TMatrix
24 \sa SOPHYA::Range SOPHYA::Sequence
25 \sa SOPHYA::MathArray
26
27 The following sample code illustrates sub-vector manipulation:
28 \code
29 #include "array.h"
30 // ...
31 // Input Vector containing a noisy periodic signal
32 Vector in(1024), out(1024);
33 in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
34 for(int kk=0; kk<in.Size(); kk++)
35 in(kk) += 2*sin(kk*0.05);
36 // Compute the output vector by a simple low pass filter
37 Vector out(1024);
38 int w = 2;
39 for(int k=w; k<in.Size()-w; k++)
40 out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
41 \endcode
42*/
43
[804]44////////////////////////////////////////////////////////////////
45//**** Createur, Destructeur
46
[894]47//! Default constructor
[762]48template <class T>
[804]49TVector<T>::TVector()
50 : TMatrix<T>()
[762]51{
[1560]52 arrtype_ = 2; // Type = Vector
[762]53}
54
[894]55//! construct a vector
56/*!
57 \param n : number of elements
[4035]58 \param lcv : line or column vector (BaseArray::AutoVectorType / SameVectorType / ColumnVector / RowVector)
[894]59 \param mm : memory mapping type
[2575]60 \param fzero : if \b true , set vector elements to zero
[894]61 \sa SelectVectorType
62 */
[762]63template <class T>
[2575]64TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero)
[804]65// Constructeur
[2575]66 : TMatrix<T>(1,1,mm,false)
[762]67{
[1099]68 arrtype_ = 2; // Type = Vector
[813]69 lcv = SelectVectorType(lcv);
[2575]70 ReSize(n,lcv,fzero);
[762]71}
72
[967]73//! Constructor by copy
[976]74/*!
75 \warning datas are \b SHARED with \b a.
76 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
77*/
[762]78template <class T>
[804]79TVector<T>::TVector(const TVector<T>& a)
[967]80// Constructeur par copie
[804]81 : TMatrix<T>(a)
[762]82{
[1099]83 arrtype_ = 2; // Type = Vector
[762]84}
85
[894]86//! Constructor by copy
87/*!
88 \param share : if true, share data. If false copy data
89 */
[762]90template <class T>
[804]91TVector<T>::TVector(const TVector<T>& a, bool share)
[762]92// Constructeur par copie avec possibilite de forcer le partage ou non.
[804]93: TMatrix<T>(a, share)
[762]94{
[1099]95 arrtype_ = 2; // Type = Vector
[762]96}
97
98
[894]99//! Constructor of a vector from a TArray \b a
100/*!
101 \param a : TArray to be copied or shared
102 \param share : if true, share data. If false copy data
103 \param lcv : line or column vector ?
104 \sa SelectVectorType
105 */
[762]106template <class T>
[2752]107TVector<T>::TVector(const TArray<T>& a, bool share, short lcv)
108: TMatrix<T>(a, share)
[762]109{
[4035]110 arrtype_ = 2; // Type = Vector
111 if (a.NbDimensions() == 0) return; // Reza-Nov 2011: we allow copy contrsuctor on non allocated arrays
[813]112 if ( (size_[0] != 1) && (size_[1] != 1) )
113 throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
114 if ( (size_[0] == 1) && (size_[1] == 1) ) {
115 if (lcv == SameVectorType) lcv = a.GetVectorType();
116 if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
117 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
[804]118 }
[762]119}
120
[1099]121//! Constructor of a vector from a TArray \b a , with a different data type
[4035]122/*!
123 vector size and memory layout are copied from the array \b a, or a packed vector is created if \b pack==true.
124 \param a : original array, to copy sizes and data from
125 \param pack : if \b true , create a packed vector, else same memory layout as \b a.
126*/
[1099]127template <class T>
[4035]128TVector<T>::TVector(const BaseArray& a, bool pack)
129 : TMatrix<T>(a,pack)
[1099]130{
[4035]131 arrtype_ = 2; // Type = Vector
132 if (a.NbDimensions() == 0) return; // Reza-Nov 2011: we allow copy contrsuctor on non allocated arrays
[1103]133 if ( (size_[0] != 1) && (size_[1] != 1) )
[4035]134 throw SzMismatchError("TVector<T>::TVector(const BaseArray& a,bool) NRows()!=1 && NCols()!=1 ");
[1099]135}
136
[3853]137//! Constructor from an STL vector
[2719]138template <class T>
139TVector<T>::TVector(const vector<T>& v, short lcv)
140{
141 sa_size_t n = v.size();
142 if(n==0)
143 throw SzMismatchError("TVector<T>::TVector(const vector<T>& v) size()==0 ");
144 arrtype_ = 2; // Type = Vector
145 ReSize(n,lcv,false);
146 for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
147}
148
[894]149//! Destructor
[762]150template <class T>
[804]151TVector<T>::~TVector()
[762]152{
153}
154
[894]155//! Resize the vector
156/*!
157 \param n : number of elements
158 \param lcv : line or column vector ?
159 \sa SelectVectorType
160 */
[762]161template <class T>
[2575]162void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
[762]163{
[804]164 if( n == 0 )
165 throw(SzMismatchError("TVector::ReSize() n = 0 "));
[1156]166 sa_size_t r,c;
[813]167 if (lcv == SameVectorType) lcv = GetVectorType();
168 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
[804]169 if (lcv == ColumnVector) { r = n; c = 1; }
170 else { c = n; r = 1; }
[2575]171 TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
[813]172 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
[762]173}
174
[894]175//! Re-allocate space for the vector
176/*!
177 \param n : number of elements
178 \param lcv : line or column vector ?
179 \param force : if true re-allocation is forced, if not it occurs
180 only if the required space is greater than the old one.
181 \sa ReSize SelectVectorType
182 */
[762]183template <class T>
[1156]184void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
[762]185{
[804]186 if( n == 0 )
187 throw(SzMismatchError("TVector::Realloc() n = 0 "));
[1156]188 sa_size_t r,c;
[813]189 if (lcv == SameVectorType) lcv = GetVectorType();
190 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
[804]191 if (lcv == ColumnVector) { r = n; c = 1; }
192 else { c = n; r = 1; }
193 TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
[813]194 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
[762]195}
196
[804]197// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
[894]198//! Return a subvector define by \b Range \b relt
[762]199template <class T>
[813]200TVector<T> TVector<T>::SubVector(Range relt) const
[762]201{
[2915]202 Range rr=Range::first();
203 Range cr=Range::first();
[804]204 if (GetVectorType() == ColumnVector ) rr = relt;
205 else cr = relt;
206 TMatrix<T> const & mtx = (*this);
[2752]207 TVector sv( mtx(rr, cr) , true, GetVectorType() );
[804]208 return(sv);
[762]209}
210
[894]211//! Return info on number of rows, column and type \b T
[813]212template <class T>
213string TVector<T>::InfoString() const
214{
215 string rs = "TVector<";
216 rs += typeid(T).name();
217 char buff[64];
218 sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
219 rs += buff;
220 return(rs);
[804]221
[813]222}
223
[3853]224//! Fill from an STL vector
[2719]225/*!
226 \param v : STL vector to copy
[3853]227 \param noresize : "true" means TVector keeps its size, if "false", ReSize(, ,false) is called
[2719]228 \warning Filling is always done starting at TVector(0)
229 */
230template <class T>
231sa_size_t TVector<T>::FillFr(const vector<T>& v,bool noresize)
232{
233 sa_size_t n = v.size();
234 if(n==0) return 0; //STL vector de taille nulle!
235 if(!noresize) ReSize(n,SameVectorType,false);
236 if(Size()<n) n = Size();
237 if(n==0) return 0; //TVector de taille nulle sans resize!
238 for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
239 return n;
240}
241
[3853]242//! Fill an STL vector
[2719]243/*!
244 \param v : STL vector to fill
[3853]245 \param addtoend : "true" means TVector elements will be appended at the end of "v"
[2719]246 */
247template <class T>
248sa_size_t TVector<T>::FillTo(vector<T>& v,bool addtoend)
249{
250 sa_size_t n = Size();
251 if(n==0) return 0; //TVector de taille nulle!
252 if(!addtoend) v.resize(0);
253 for(sa_size_t i=0;i<n;i++) v.push_back((*this)(i));
254 return n;
255}
256
[3869]257/*!
[3857]258 \brief to std::vector<T> conversion function
259
[3869]260 Return an std::vector<T> with the same size as the original vector and with elements std::vec[i]=(*this)(i)
[3857]261*/
262template <class T>
263vector<T> TVector<T>::ConvertTostdvec()
264{
265 sa_size_t n = Size();
266 std::vector<T> rv(n);
267 for(sa_size_t i=0;i<n;i++) rv[i]=(*this)(i);
268 return rv;
269}
270
[762]271///////////////////////////////////////////////////////////////
272#ifdef __CXX_PRAGMA_TEMPLATES__
[3661]273#pragma define_template TVector<uint_1>
[762]274#pragma define_template TVector<uint_2>
[2927]275#pragma define_template TVector<uint_4>
[1543]276#pragma define_template TVector<uint_8>
[3661]277#pragma define_template TVector<int_1>
[2927]278#pragma define_template TVector<int_2>
[762]279#pragma define_template TVector<int_4>
280#pragma define_template TVector<int_8>
281#pragma define_template TVector<r_4>
282#pragma define_template TVector<r_8>
283#pragma define_template TVector< complex<r_4> >
284#pragma define_template TVector< complex<r_8> >
[3751]285#ifdef SO_LDBLE128
286#pragma define_template TVector<r_16>
287#pragma define_template TVector< complex<r_16> >
[762]288#endif
[3751]289#endif
[762]290
291#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
[3661]292template class TVector<uint_1>;
[762]293template class TVector<uint_2>;
[2927]294template class TVector<uint_4>;
[1543]295template class TVector<uint_8>;
[3661]296template class TVector<int_1>;
[2927]297template class TVector<int_2>;
[762]298template class TVector<int_4>;
299template class TVector<int_8>;
300template class TVector<r_4>;
301template class TVector<r_8>;
302template class TVector< complex<r_4> >;
303template class TVector< complex<r_8> >;
[3751]304#ifdef SO_LDBLE128
305template class TVector<r_16>;
306template class TVector< complex<r_16> >;
[762]307#endif
[3751]308#endif
[804]309
[3751]310} // FIN namespace SOPHYA
311
Note: See TracBrowser for help on using the repository browser.