Edinburgh Speech Tools  2.1-release
EST_TVector.h
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 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 /* Author : Paul Taylor */
34 /* Date : April 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* Vector class */
37 /* */
38 /*=======================================================================*/
39 
40 #ifndef __EST_TVector_H__
41 #define __EST_TVector_H__
42 
43 #include <iostream>
44 
45 #include "EST_bool.h"
46 #include "EST_rw_status.h"
47 
49 
50 template<class T> class EST_TMatrix;
51 template<class T> class EST_TList;
52 class EST_String;
53 
54 /* A constants to make it clearer what is going on when we pass `-1'
55  * meaning `current size' or `all the rest'
56  */
57 
58 extern const int EST_CURRENT;
59 extern const int EST_ALL;
60 
61 /* When set bounds checks (safe but slow) are done on vector access */
62 #ifndef TVECTOR_BOUNDS_CHECKING
63 # define TVECTOR_BOUNDS_CHECKING 0
64 #endif
65 
66 #if TVECTOR_BOUNDS_CHECKING
67 #define A_CHECK a_check
68 #else
69 #define A_CHECK a_no_check
70 #endif
71 
72 #define INLINE inline
73 
74 /* This doesn't work as I thought so I have disabled it for now.
75  */
76 
77 #if defined(__GNUC__) && 0
78 # define fast_a_v_gcc(C) \
79  ( *((T *)\
80  (((char (*) [sizeof(T)*p_column_step])p_memory) + (C))\
81  ))
82 # define fast_a_v_x(C) (fast_a_v_gcc(C))
83 #else
84 # define fast_a_v_x(C) (fast_a_v(C))
85 #endif
86 
87 
88 /** @class EST_TVector
89  * @brief Template vector
90  * @ingroup containerclasses
91  * @tparam T Type of vector elements
92 
93  This serves as a base class for a vector
94  of type `T`. This acts as a higher level
95  version of a normal C array as defined as `float *x`, etc.
96 
97  The vector can be resized after declaration, access can be
98  with or without bounds checking. Round brackets denote read-only
99  access (for consts) while square brackets are for read-write access.
100  In both cases references are returned.
101 
102  The standard operators () and [] should be thought of as
103  having no bounds checking, though they may do so optionally
104  as a compile time option. The methods EST_TVector::a_check and
105  EST_TVector::a_nocheck provide explicit boundary checking/nonchecking,
106  both const and non-const versions are provided.
107 
108  Access through () and [] are guaranteed to be as fast as standard
109  C arrays (assuming a reasonable optimizing compiler).
110 
111  @code{.cpp}
112  EST_FVector x(10);
113  int i;
114 
115  for (i=0; i < x.length(); ++i)
116  x[i] = sqrt((float)i);
117 
118  x.resize(20);
119 
120  for (i=10; i < x.length(); ++i)
121  x[i] = sqrt((float)i);
122 
123  @endcode
124 
125  To instantiate a template for a a vector of type `FooBar`
126 
127  @code{.cpp}
128  #include "../base_class/EST_TVector.cc"
129  // If you want List to vector conversion (and defined a TList)
130  #include "../base_class/EST_Tvectlist.cc"
131 
132  template class EST_TVector<FooBar>;
133  template std::ostream& operator <<
134  (std::ostream &st, const EST_TVector<FooBar> &v);
135  @endcode
136 
137  The EST library already has template vector instantiations for
138  `int`, `float`, `double` and EST_String. Also types are defined for them
139  in \ref EST_types.h as EST_IVector, EST_FVector,
140  EST_DVector and EST_StrVector for `int`s,
141  `float`s, `doubles`s and \ref EST_String respectively.
142 
143  * @see matrix_example */
144 template <class T>
146 {
147  // protected:
148 public:
149  /** Pointer to the start of the vector.
150  * The start of allocated memory is p_memory-p_offset.
151  */
152  T *p_memory;
153 
154  /// Visible shape
156 
157  /// How to access the memory
160 
162 
163 
164  /// The memory access rule, in one place for easy reference
166  {return cs==1?c:c*cs;}
167 
169  {
170  return vcell_pos(c,
171  p_column_step);
172  }
173 
175  {
176  return c;
177  }
178 
179  /// quick method for returning \(x[n]\)
180  INLINE const T &fast_a_v(int c) const { return p_memory[vcell_pos(c)]; }
181 
182  INLINE T &fast_a_v(int c) { return p_memory[vcell_pos(c)]; }
183 
184  INLINE const T &fast_a_1(int c) const { return p_memory[vcell_pos_1(c)]; }
185  INLINE T &fast_a_1(int c) { return p_memory[vcell_pos_1(c)]; }
186 
187  /// Get and set values from array
188  void set_values(const T *data, int step, int start_c, int num_c);
189  void get_values(T *data, int step, int start_c, int num_c) const;
190 
191  /// private copy function, called from all other copying functions.
192  void copy(const EST_TVector<T> &a);
193  /// just copy data, no resizing, no size check.
194  void copy_data(const EST_TVector<T> &a);
195 
196  /// resize the memory and reset the bounds, but don't set values.
197  void just_resize(int new_cols, T** old_vals);
198 
199  /// sets data and length to default values (0 in both cases).
200  void default_vals();
201 
202 public:
203  ///default constructor
204  EST_TVector();
205 
206  /// copy constructor
207  EST_TVector(const EST_TVector<T> &v);
208 
209  /// "size" constructor - make vector of size n.
211 
212  /// construct from memory supplied by caller
213  EST_TVector(int,
214  T *memory, int offset=0, int free_when_destroyed=0);
215 
216  /// destructor.
217  ~EST_TVector();
218 
219  /// default value, used for filling matrix after resizing
220  static const T *def_val;
221 
222  /** A reference to this variable is returned if you try and access
223  * beyond the bounds of the matrix. The value is undefined, but you
224  * can check for the reference you get having the same address as
225  * this variable to test for an error.
226  */
227  static T *error_return;
228 
229  /** resize vector. If `set=1`, then the current values in
230  the vector are preserved up to the new length `n`. If the
231  new length exceeds the old length, the rest of the vector is
232  filled with the `def_val`
233  */
234  void resize(ssize_t n, int set=1);
235 
236  /** For when you absolutely have to have access to the memory.
237  */
238  const T * memory() const { return p_memory; }
239  T * memory(){ return p_memory; }
240 
241  /**@name Access
242  * Basic access methods for vectors.
243  */
244  ///@{
245 
246  /// number of items in vector.
248  /// number of items in vector.
249  INLINE ssize_t length() const {return num_columns();}
250  /// number of items in vector.
251  INLINE ssize_t n() const {return num_columns();}
252 
253  /// read-only const access operator: without bounds checking
254  INLINE const T &a_no_check(ssize_t n) const { return fast_a_v_x(n); }
255  /// read/write non-const access operator: without bounds checking
256  INLINE T &a_no_check(ssize_t n) { return fast_a_v_x(n); }
257  /// read-only const access operator: without bounds checking
258  INLINE const T &a_no_check_1(ssize_t n) const { return fast_a_1(n); }
259  /// read/write non-const access operator: without bounds checking
260  INLINE T &a_no_check_1(ssize_t n) { return fast_a_1(n); }
261 
262  // #define pp_a_no_check(V,N) (pp_fast_a(V,N))
263 
264  /// read-only const access operator: with bounds checking
265  const T &a_check(ssize_t n) const;
266  /// read/write non-const access operator: with bounds checking
267  T &a_check(ssize_t n);
268 
269  const T &a(ssize_t n) const { return A_CHECK(n); }
270  T &a(ssize_t n) { return A_CHECK(n); }
271 
272  /// read-only const access operator: return reference to nth member
273  const T &operator () (ssize_t n) const {return A_CHECK(n);}
274 
275  // PT
276  // /// non const access operator: return reference to nth member
277  // T &operator () (int n) const {return a(n);}
278 
279  /// read/write non const access operator: return reference to nth member
280  T &operator [] (ssize_t n) { return A_CHECK(n); }
281 
282  ///@}
283 
284  void set_memory(T *buffer, int offset, ssize_t columns,
285  int free_when_destroyed=0);
286 
287  /// assignment operator
288  EST_TVector &operator=(const EST_TVector &s);
289 
290  /// Fill entire array will value `v`.
291  void fill(const T &v);
292 
293  /// Fill vector with default value
294  void empty() { fill(*def_val); }
295 
296  /// is true if vectors are equal size and all elements are equal.
297  int operator == (const EST_TVector &v) const;
298  /// is true if vectors are not equal size or a single elements isn't equal.
299  int operator != (const EST_TVector &v) const
300  { return ! ((*this) == v); }
301 
302  /// Copy data in and out. Subclassed by SimpleVector for speed.
303 
304  void copy_section(T* dest, int offset=0, int num=-1) const;
305  void set_section(const T* src, int offset=0, int num=-1);
306 
307  /// Create a sub vector.
308  void sub_vector(EST_TVector<T> &sv, int start_c=0, int len=-1);
309  /// print out vector.
310  friend std::ostream& operator << (std::ostream &st, const EST_TVector<T> &m)
311  {
312  int i;
313  for (i = 0; i < m.n(); ++i)
314  st << m(i) << " ";
315  st << std::endl;
316  return st;
317  }
318 
319  /// Matrix must be friend to set up subvectors
320  friend class EST_TMatrix<T>;
321 
322  void integrity() const;
323 
324 };
325 
326 /// assignment operator: fill track with values in list `s`.
327 template<class T>
328 extern EST_TVector<T> &set(EST_TVector<T> &v, const EST_TList<T> &s);
329 
330 #undef A_CHECK
331 #endif
INLINE ssize_t vcell_pos_1(ssize_t c) const
Definition: EST_TVector.h:174
ssize_t p_column_step
Definition: EST_TVector.h:159
INLINE const T & fast_a_v(int c) const
quick method for returning (x[n])
Definition: EST_TVector.h:180
ssize_t p_num_columns
Visible shape.
Definition: EST_TVector.h:155
INLINE T & fast_a_1(int c)
Definition: EST_TVector.h:185
Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
Definition: EST_TMatrix.h:89
void copy_data(const EST_TVector< T > &a)
just copy data, no resizing, no size check.
Definition: EST_TVector.cc:148
bool p_sub_matrix
Definition: EST_TVector.h:161
INLINE ssize_t num_columns() const
number of items in vector.
Definition: EST_TVector.h:247
#define INLINE
Definition: EST_TVector.h:72
void fill(const T &v)
Fill entire array will value v.
Definition: EST_TVector.cc:105
int operator!=(const EST_TVector &v) const
is true if vectors are not equal size or a single elements isn&#39;t equal.
Definition: EST_TVector.h:299
INLINE T & fast_a_v(int c)
Definition: EST_TVector.h:182
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:254
const T & a_check(ssize_t n) const
read-only const access operator: with bounds checking
Definition: EST_TVector.cc:249
int ssize_t
const int EST_ALL
const T & a(ssize_t n) const
Definition: EST_TVector.h:269
void default_vals()
sets data and length to default values (0 in both cases).
Definition: EST_TVector.cc:50
const T & operator()(ssize_t n) const
read-only const access operator: return reference to nth member
Definition: EST_TVector.h:273
INLINE T & a_no_check_1(ssize_t n)
read/write non-const access operator: without bounds checking
Definition: EST_TVector.h:260
static const T * def_val
default value, used for filling matrix after resizing
Definition: EST_TVector.h:220
INLINE T & a_no_check(ssize_t n)
read/write non-const access operator: without bounds checking
Definition: EST_TVector.h:256
INLINE ssize_t vcell_pos(ssize_t c) const
Definition: EST_TVector.h:168
void resize(ssize_t n, int set=1)
Definition: EST_TVector.cc:196
void copy(const EST_TVector< T > &a)
private copy function, called from all other copying functions.
Definition: EST_TVector.cc:154
INLINE ssize_t length() const
number of items in vector.
Definition: EST_TVector.h:249
void set_section(const T *src, int offset=0, int num=-1)
Definition: EST_TVector.cc:285
void get_values(T *data, int step, int start_c, int num_c) const
Definition: EST_TVector.cc:137
INLINE ssize_t vcell_pos(ssize_t c, ssize_t cs) const
The memory access rule, in one place for easy reference.
Definition: EST_TVector.h:165
~EST_TVector()
destructor.
Definition: EST_TVector.cc:90
const int EST_CURRENT
#define A_CHECK
Definition: EST_TVector.h:69
T & operator[](ssize_t n)
read/write non const access operator: return reference to nth member
Definition: EST_TVector.h:280
void sub_vector(EST_TVector< T > &sv, int start_c=0, int len=-1)
Create a sub vector.
Definition: EST_TVector.cc:298
static T * error_return
Definition: EST_TVector.h:227
void just_resize(int new_cols, T **old_vals)
resize the memory and reset the bounds, but don&#39;t set values.
Definition: EST_TVector.cc:161
const T * memory() const
Definition: EST_TVector.h:238
Template vector.
Definition: EST_TVector.h:145
EST_TVector & operator=(const EST_TVector &s)
assignment operator
Definition: EST_TVector.cc:233
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
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
INLINE const T & fast_a_1(int c) const
Definition: EST_TVector.h:184
#define fast_a_v_x(C)
Definition: EST_TVector.h:84
INLINE const T & a_no_check_1(ssize_t n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:258
EST_TVector()
default constructor
Definition: EST_TVector.cc:61
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
T & a(ssize_t n)
Definition: EST_TVector.h:270
int operator==(const EST_TVector &v) const
is true if vectors are equal size and all elements are equal.
Definition: EST_TVector.cc:255
void empty()
Fill vector with default value.
Definition: EST_TVector.h:294
void set_memory(T *buffer, int offset, ssize_t columns, int free_when_destroyed=0)
Definition: EST_TVector.cc:112
void integrity() const
Definition: EST_TVector.cc:315
ssize_t p_offset
How to access the memory.
Definition: EST_TVector.h:158
T * memory()
Definition: EST_TVector.h:239