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

Last change on this file since 4045 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
Line 
1// $Id: tvector.cc,v 1.29 2011-11-14 16:28:25 ansari Exp $
2// C.Magneville 04/99
3#include "sopnamsp.h"
4#include "machdefs.h"
5#include <stdlib.h>
6#include "pexceptions.h"
7
8#define TVECTOR_CC_BFILE // avoid extern template declarations
9#include "tvector.h"
10
11namespace SOPHYA {
12
13/*!
14 \class TVector
15 \ingroup TArray
16
17 The TVector class specializes the TMatrix class for representing
18 row or column vectors. TVector<T> objects can be initialised or converted
19 to std::vector<T>.
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
44////////////////////////////////////////////////////////////////
45//**** Createur, Destructeur
46
47//! Default constructor
48template <class T>
49TVector<T>::TVector()
50 : TMatrix<T>()
51{
52 arrtype_ = 2; // Type = Vector
53}
54
55//! construct a vector
56/*!
57 \param n : number of elements
58 \param lcv : line or column vector (BaseArray::AutoVectorType / SameVectorType / ColumnVector / RowVector)
59 \param mm : memory mapping type
60 \param fzero : if \b true , set vector elements to zero
61 \sa SelectVectorType
62 */
63template <class T>
64TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero)
65// Constructeur
66 : TMatrix<T>(1,1,mm,false)
67{
68 arrtype_ = 2; // Type = Vector
69 lcv = SelectVectorType(lcv);
70 ReSize(n,lcv,fzero);
71}
72
73//! Constructor by copy
74/*!
75 \warning datas are \b SHARED with \b a.
76 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
77*/
78template <class T>
79TVector<T>::TVector(const TVector<T>& a)
80// Constructeur par copie
81 : TMatrix<T>(a)
82{
83 arrtype_ = 2; // Type = Vector
84}
85
86//! Constructor by copy
87/*!
88 \param share : if true, share data. If false copy data
89 */
90template <class T>
91TVector<T>::TVector(const TVector<T>& a, bool share)
92// Constructeur par copie avec possibilite de forcer le partage ou non.
93: TMatrix<T>(a, share)
94{
95 arrtype_ = 2; // Type = Vector
96}
97
98
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 */
106template <class T>
107TVector<T>::TVector(const TArray<T>& a, bool share, short lcv)
108: TMatrix<T>(a, share)
109{
110 arrtype_ = 2; // Type = Vector
111 if (a.NbDimensions() == 0) return; // Reza-Nov 2011: we allow copy contrsuctor on non allocated arrays
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_;
118 }
119}
120
121//! Constructor of a vector from a TArray \b a , with a different data type
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*/
127template <class T>
128TVector<T>::TVector(const BaseArray& a, bool pack)
129 : TMatrix<T>(a,pack)
130{
131 arrtype_ = 2; // Type = Vector
132 if (a.NbDimensions() == 0) return; // Reza-Nov 2011: we allow copy contrsuctor on non allocated arrays
133 if ( (size_[0] != 1) && (size_[1] != 1) )
134 throw SzMismatchError("TVector<T>::TVector(const BaseArray& a,bool) NRows()!=1 && NCols()!=1 ");
135}
136
137//! Constructor from an STL vector
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
149//! Destructor
150template <class T>
151TVector<T>::~TVector()
152{
153}
154
155//! Resize the vector
156/*!
157 \param n : number of elements
158 \param lcv : line or column vector ?
159 \sa SelectVectorType
160 */
161template <class T>
162void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
163{
164 if( n == 0 )
165 throw(SzMismatchError("TVector::ReSize() n = 0 "));
166 sa_size_t r,c;
167 if (lcv == SameVectorType) lcv = GetVectorType();
168 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
169 if (lcv == ColumnVector) { r = n; c = 1; }
170 else { c = n; r = 1; }
171 TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
172 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
173}
174
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 */
183template <class T>
184void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
185{
186 if( n == 0 )
187 throw(SzMismatchError("TVector::Realloc() n = 0 "));
188 sa_size_t r,c;
189 if (lcv == SameVectorType) lcv = GetVectorType();
190 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
191 if (lcv == ColumnVector) { r = n; c = 1; }
192 else { c = n; r = 1; }
193 TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
194 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
195}
196
197// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
198//! Return a subvector define by \b Range \b relt
199template <class T>
200TVector<T> TVector<T>::SubVector(Range relt) const
201{
202 Range rr=Range::first();
203 Range cr=Range::first();
204 if (GetVectorType() == ColumnVector ) rr = relt;
205 else cr = relt;
206 TMatrix<T> const & mtx = (*this);
207 TVector sv( mtx(rr, cr) , true, GetVectorType() );
208 return(sv);
209}
210
211//! Return info on number of rows, column and type \b T
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);
221
222}
223
224//! Fill from an STL vector
225/*!
226 \param v : STL vector to copy
227 \param noresize : "true" means TVector keeps its size, if "false", ReSize(, ,false) is called
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
242//! Fill an STL vector
243/*!
244 \param v : STL vector to fill
245 \param addtoend : "true" means TVector elements will be appended at the end of "v"
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
257/*!
258 \brief to std::vector<T> conversion function
259
260 Return an std::vector<T> with the same size as the original vector and with elements std::vec[i]=(*this)(i)
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
271///////////////////////////////////////////////////////////////
272#ifdef __CXX_PRAGMA_TEMPLATES__
273#pragma define_template TVector<uint_1>
274#pragma define_template TVector<uint_2>
275#pragma define_template TVector<uint_4>
276#pragma define_template TVector<uint_8>
277#pragma define_template TVector<int_1>
278#pragma define_template TVector<int_2>
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> >
285#ifdef SO_LDBLE128
286#pragma define_template TVector<r_16>
287#pragma define_template TVector< complex<r_16> >
288#endif
289#endif
290
291#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
292template class TVector<uint_1>;
293template class TVector<uint_2>;
294template class TVector<uint_4>;
295template class TVector<uint_8>;
296template class TVector<int_1>;
297template class TVector<int_2>;
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> >;
304#ifdef SO_LDBLE128
305template class TVector<r_16>;
306template class TVector< complex<r_16> >;
307#endif
308#endif
309
310} // FIN namespace SOPHYA
311
Note: See TracBrowser for help on using the repository browser.