Edinburgh Speech Tools 2.4-release
EST_TVector.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* */
34 /* Author : Paul Taylor */
35 /* Date : April 1995 */
36 /* --------------------------------------------------------------------- */
37 /* Template Vector Class */
38 /* */
39 /*************************************************************************/
40
41
42#include <iostream>
43#include <fstream>
44#include "EST_TVector.h"
45#include "EST_matrix_support.h"
46#include "EST_cutils.h"
47#include "EST_error.h"
48
49template<class T>
51{
52 p_num_columns = 0;
53 p_offset=0;
54 p_column_step=0;
55
56 p_memory = NULL;
57 p_sub_matrix=FALSE;
58}
59
60template<class T>
62{
63 default_vals();
64}
65
66template<class T>
68{
69 default_vals();
70 resize(n);
71}
72
73template<class T>
75{
76 default_vals();
77 copy(in);
78}
79
80template<class T>
82 T *memory, int offset, int free_when_destroyed)
83{
84 default_vals();
85
86 set_memory(memory, offset, n, free_when_destroyed);
87}
88
89template<class T>
91{
92 p_num_columns = 0;
93 p_offset=0;
94 p_column_step=0;
95
96 if (p_memory != NULL && !p_sub_matrix)
97 {
98 delete [] (p_memory-p_offset);
99 p_memory = NULL;
100 }
101}
102
103
104template<class T>
105void EST_TVector<T>::fill(const T &v)
106{
107 for (int i = 0; i < num_columns(); ++i)
108 fast_a_v(i) = v;
109}
110
111template<class T>
112void EST_TVector<T>::set_memory(T *buffer, int offset, int columns,
113 int free_when_destroyed)
114{
115 if (p_memory != NULL && !p_sub_matrix)
116 delete [] (p_memory-p_offset);
117
118 p_memory = buffer-offset;
119 p_offset=offset;
120 p_num_columns = columns;
121 p_column_step=1;
122 p_sub_matrix = !free_when_destroyed;
123}
124
125template<class T>
126void EST_TVector<T>::set_values(const T *data,
127 int step,
128 int start_c,
129 int num_c)
130{
131 for(int i=0, c=start_c, p=0; i<num_c; i++, c++, p+=step)
132 a_no_check(c) = data[p];
133}
134
135
136template<class T>
137void EST_TVector<T>::get_values(T *data,
138 int step,
139 int start_c,
140 int num_c) const
141{
142 for(int i=0, c=start_c, p=0; i<num_c; i++, c++, p+=step)
143 data[p] = a_no_check(c);
144}
145
146
147template<class T>
149{
150 set_values(a.p_memory, a.p_column_step, 0, num_columns());
151}
152
153template<class T>
155{
156 resize(a.n(), FALSE);
157 copy_data(a);
158}
159
160template<class T>
161void EST_TVector<T>::just_resize(int new_cols, T** old_vals)
162{
163 T *new_m;
164
165 if (num_columns() != new_cols || p_memory == NULL )
166 {
167 if (p_sub_matrix)
168 EST_error("Attempt to resize Sub-Vector");
169
170 if (new_cols < 0)
171 EST_error("Attempt to resize vector to negative size: %d",
172 new_cols);
173
174 new_m = new T[new_cols];
175
176 if (p_memory != NULL)
177 {
178 if (old_vals != NULL)
179 *old_vals = p_memory;
180 else if (!p_sub_matrix)
181 delete [] (p_memory-p_offset);
182 }
183
184 p_memory = new_m;
185 //cout << "vr: mem: " << p_memory << " (" << (int)p_memory << ")\n";
186 p_offset=0;
187 p_num_columns = new_cols;
188 p_column_step=1;
189 }
190 else
191 *old_vals = p_memory;
192}
193
194
195template<class T>
196void EST_TVector<T>::resize(int new_cols, int set)
198 int i;
199 T * old_vals = p_memory;
200 int old_cols = num_columns();
201 int old_offset = p_offset;
202 int old_column_step = p_column_step;
204 just_resize(new_cols, &old_vals);
205
206 if (set)
208 int copy_c = 0;
209
210 if (!old_vals)
211 copy_c=0;
212 else if (old_vals != p_memory)
214 copy_c = Lof(num_columns(), old_cols);
215
216 for(i=0; i<copy_c; i++)
217 a_no_check(i)
218 = old_vals[vcell_pos(i,
219 old_column_step)];
221 else
222 copy_c = old_cols;
223
224 for(i=copy_c; i<new_cols; i++)
225 a_no_check(i) = *def_val;
226 }
227
228 if (old_vals && old_vals != p_memory && !p_sub_matrix)
229 delete [] (old_vals-old_offset);
230}
231
232template<class T>
234{
235 copy(in);
236 return *this;
238
239template<class T>
241{
242 if (!EST_vector_bounds_check(n, num_columns(), FALSE))
243 return *error_return;
244
245 return fast_a_v(n);
246}
247
248template<class T>
249const T &EST_TVector<T>::a_check(int n) const
250{
251 return ((EST_TVector<T> *)this)->a(n);
252}
253
254template<class T>
256{
257 if (num_columns() != v.num_columns())
258 return 0;
259
260 for(int i=0; i<num_columns() ; i++)
261 {
262 if (fast_a_v(i) == v.fast_a_v(i))
263 continue;
264 else
265 return 0;
266 }
267 return 1;
269
270template<class T>
271void EST_TVector<T>::copy_section(T* dest, int offset, int num) const
272{
273 if (num<0)
274 num = num_columns()-offset;
275
276 if (!EST_vector_bounds_check(num+offset-1, num_columns(), FALSE))
277 return;
278
279
280 for(int i=0; i<num; i++)
281 dest[i] = a_no_check(offset+i);
282}
283
284template<class T>
285void EST_TVector<T>::set_section(const T* src, int offset, int num)
286{
287 if (num<0)
288 num = num_columns()-offset;
289
290 if (!EST_vector_bounds_check(num+offset-1, num_columns(), FALSE))
291 return;
292
293 for(int i=0; i<num; i++)
294 a_no_check(offset+i) = src[i];
295}
296
297template<class T>
299 int start_c, int len)
301 if (len < 0)
302 len = num_columns()-start_c;
303
304 if (sv.p_memory != NULL && ! sv.p_sub_matrix)
305 delete [] (sv.p_memory - sv.p_offset);
306
307 sv.p_sub_matrix = TRUE;
308 sv.p_offset = p_offset + start_c*p_column_step;
309 sv.p_memory = p_memory - p_offset + sv.p_offset;
310 sv.p_column_step=p_column_step;
312}
313
314template<class T>
315void EST_TVector<T>::integrity() const
316{
317 cout << "integrity: p_memory=" << p_memory << endl;
318 if(p_memory == (T *)0x00080102)
319 {
320 cout << "fatal value!!!\n";
321 }
322}
323
324
unsigned int p_offset
How to access the memory.
Definition: EST_TVector.h:160
INLINE const T & fast_a_v(int c) const
quick method for returning (x[n])
Definition: EST_TVector.h:183
const T & a_check(int n) const
read-only const access operator: with bounds checking
Definition: EST_TVector.cc:249
void just_resize(int new_cols, T **old_vals)
resize the memory and reset the bounds, but don't set values.
Definition: EST_TVector.cc:161
void resize(int n, int set=1)
Definition: EST_TVector.cc:196
void default_vals()
sets data and length to default values (0 in both cases).
Definition: EST_TVector.cc:50
EST_TVector & operator=(const EST_TVector &s)
assignment operator
Definition: EST_TVector.cc:233
void copy_data(const EST_TVector< T > &a)
just copy data, no resizing, no size check.
Definition: EST_TVector.cc:148
INLINE int num_columns() const
number of items in vector.
Definition: EST_TVector.h:250
void copy_section(T *dest, int offset=0, int num=-1) const
Copy data in and out. Subclassed by SimpleVector for speed.
Definition: EST_TVector.cc:271
void copy(const EST_TVector< T > &a)
private copy function, called from all other copying functions.
Definition: EST_TVector.cc:154
unsigned int p_num_columns
Visible shape.
Definition: EST_TVector.h:157
INLINE int n() const
number of items in vector.
Definition: EST_TVector.h:254
int operator==(const EST_TVector &v) const
is true if vectors are equal size and all elements are equal.
Definition: EST_TVector.cc:255
EST_TVector()
default constructor
Definition: EST_TVector.cc:61
void sub_vector(EST_TVector< T > &sv, int start_c=0, int len=-1)
Create a sub vector.
Definition: EST_TVector.cc:298
void fill(const T &v)
Fill entire array will value <parameter>v</parameter>.
Definition: EST_TVector.cc:105
~EST_TVector()
destructor.
Definition: EST_TVector.cc:90
void set_values(const T *data, int step, int start_c, int num_c)
Get and set values from array.
Definition: EST_TVector.cc:126