Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EST_TVector.h
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 using namespace std;
45 #include "EST_bool.h"
46 #include "EST_rw_status.h"
47 
48 #include "instantiate/EST_TVectorI.h"
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 ostream& operator <<
134  (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
155  unsigned int p_num_columns;
156 
157  /// How to access the memory
158  unsigned int p_offset;
159  unsigned int p_column_step;
160 
161  bool p_sub_matrix;
162 
163 
164  /// The memory access rule, in one place for easy reference
165  INLINE unsigned int vcell_pos(unsigned int c,
166  unsigned int cs) const
167  {return cs==1?c:c*cs;}
168 
169  INLINE unsigned int vcell_pos(unsigned int c) const
170  {
171  return vcell_pos(c,
172  p_column_step);
173  }
174 
175  INLINE unsigned int vcell_pos_1(unsigned int c) const
176  {
177  return c;
178  }
179 
180  /// quick method for returning \(x[n]\)
181  INLINE const T &fast_a_v(int c) const { return p_memory[vcell_pos(c)]; }
182 
183  INLINE T &fast_a_v(int c) { return p_memory[vcell_pos(c)]; }
184 
185  INLINE const T &fast_a_1(int c) const { return p_memory[vcell_pos_1(c)]; }
186  INLINE T &fast_a_1(int c) { return p_memory[vcell_pos_1(c)]; }
187 
188  /// Get and set values from array
189  void set_values(const T *data, int step, int start_c, int num_c);
190  void get_values(T *data, int step, int start_c, int num_c) const;
191 
192  /// private copy function, called from all other copying functions.
193  void copy(const EST_TVector<T> &a);
194  /// just copy data, no resizing, no size check.
195  void copy_data(const EST_TVector<T> &a);
196 
197  /// resize the memory and reset the bounds, but don't set values.
198  void just_resize(int new_cols, T** old_vals);
199 
200  /// sets data and length to default values (0 in both cases).
201  void default_vals();
202 
203 public:
204  ///default constructor
205  EST_TVector();
206 
207  /// copy constructor
208  EST_TVector(const EST_TVector<T> &v);
209 
210  /// "size" constructor - make vector of size n.
211  EST_TVector(int n);
212 
213  /// construct from memory supplied by caller
214  EST_TVector(int,
215  T *memory, int offset=0, int free_when_destroyed=0);
216 
217  /// destructor.
218  ~EST_TVector();
219 
220  /// default value, used for filling matrix after resizing
221  static const T *def_val;
222 
223  /** A reference to this variable is returned if you try and access
224  * beyond the bounds of the matrix. The value is undefined, but you
225  * can check for the reference you get having the same address as
226  * this variable to test for an error.
227  */
228  static T *error_return;
229 
230  /** resize vector. If `set=1`, then the current values in
231  the vector are preserved up to the new length `n`. If the
232  new length exceeds the old length, the rest of the vector is
233  filled with the `def_val`
234  */
235  void resize(int n, int set=1);
236 
237  /** For when you absolutely have to have access to the memory.
238  */
239  const T * memory() const { return p_memory; }
240  T * memory(){ return p_memory; }
241 
242  /**@name Access
243  * Basic access methods for vectors.
244  */
245  ///@{
246 
247  /// number of items in vector.
248  INLINE int num_columns() const {return p_num_columns;}
249  /// number of items in vector.
250  INLINE int length() const {return num_columns();}
251  /// number of items in vector.
252  INLINE int n() const {return num_columns();}
253 
254  /// read-only const access operator: without bounds checking
255  INLINE const T &a_no_check(int n) const { return fast_a_v_x(n); }
256  /// read/write non-const access operator: without bounds checking
257  INLINE T &a_no_check(int n) { return fast_a_v_x(n); }
258  /// read-only const access operator: without bounds checking
259  INLINE const T &a_no_check_1(int n) const { return fast_a_1(n); }
260  /// read/write non-const access operator: without bounds checking
261  INLINE T &a_no_check_1(int n) { return fast_a_1(n); }
262 
263  // #define pp_a_no_check(V,N) (pp_fast_a(V,N))
264 
265  /// read-only const access operator: with bounds checking
266  const T &a_check(int n) const;
267  /// read/write non-const access operator: with bounds checking
268  T &a_check(int n);
269 
270  const T &a(int n) const { return A_CHECK(n); }
271  T &a(int n) { return A_CHECK(n); }
272 
273  /// read-only const access operator: return reference to nth member
274  const T &operator () (int n) const {return A_CHECK(n);}
275 
276  // PT
277  // /// non const access operator: return reference to nth member
278  // T &operator () (int n) const {return a(n);}
279 
280  /// read/write non const access operator: return reference to nth member
281  T &operator [] (int n) { return A_CHECK(n); }
282 
283  ///@}
284 
285  void set_memory(T *buffer, int offset, int columns,
286  int free_when_destroyed=0);
287 
288  /// assignment operator
289  EST_TVector &operator=(const EST_TVector &s);
290 
291  /// Fill entire array will value `v`.
292  void fill(const T &v);
293 
294  /// Fill vector with default value
295  void empty() { fill(*def_val); }
296 
297  /// is true if vectors are equal size and all elements are equal.
298  int operator == (const EST_TVector &v) const;
299  /// is true if vectors are not equal size or a single elements isn't equal.
300  int operator != (const EST_TVector &v) const
301  { return ! ((*this) == v); }
302 
303  /// Copy data in and out. Subclassed by SimpleVector for speed.
304 
305  void copy_section(T* dest, int offset=0, int num=-1) const;
306  void set_section(const T* src, int offset=0, int num=-1);
307 
308  /// Create a sub vector.
309  void sub_vector(EST_TVector<T> &sv, int start_c=0, int len=-1);
310  /// print out vector.
311  friend ostream& operator << (ostream &st, const EST_TVector<T> &m)
312  {
313  int i;
314  for (i = 0; i < m.n(); ++i)
315  st << m(i) << " "; st << 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 const T & fast_a_v(int c) const
quick method for returning (x[n])
Definition: EST_TVector.h:181
INLINE T & a_no_check_1(int n)
read/write non-const access operator: without bounds checking
Definition: EST_TVector.h:261
Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
Definition: EST_TMatrix.h:90
unsigned int p_num_columns
Visible shape.
Definition: EST_TVector.h:155
static const T * def_val
default value, used for filling matrix after resizing
Definition: EST_TVector.h:221
INLINE int length() const
number of items in vector.
Definition: EST_TVector.h:250
INLINE int num_columns() const
number of items in vector.
Definition: EST_TVector.h:248
INLINE unsigned int vcell_pos(unsigned int c, unsigned int cs) const
The memory access rule, in one place for easy reference.
Definition: EST_TVector.h:165
unsigned int p_offset
How to access the memory.
Definition: EST_TVector.h:158
INLINE const T & a_no_check_1(int n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:259
INLINE int n() const
number of items in vector.
Definition: EST_TVector.h:252
INLINE const T & a_no_check(int n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:255
INLINE T & a_no_check(int n)
read/write non-const access operator: without bounds checking
Definition: EST_TVector.h:257
static T * error_return
Definition: EST_TVector.h:228
const T * memory() const
Definition: EST_TVector.h:239
Template vector.
Definition: EST_TVector.h:145
void empty()
Fill vector with default value.
Definition: EST_TVector.h:295