Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_TSimpleVector.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: Richard Caley (rjc@cstr.ed.ac.uk) */
35  /* Date: Fri Oct 10 1997 */
36  /* -------------------------------------------------------------------- */
37  /* A subclass of TVector which copies using memcopy. This isn't */
38  /* suitable for matrices of class objects which have to be copied */
39  /* using a constructor or specialised assignment operator. */
40  /* */
41  /*************************************************************************/
42 
43 #include "EST_TSimpleVector.h"
44 #include "EST_matrix_support.h"
45 #include <fstream>
46 #include "EST_cutils.h"
47 #include <string.h>
48 
49 template<class T> void EST_TSimpleVector<T>::copy(const EST_TSimpleVector<T> &a)
50 {
51  if (this->p_column_step==1 && a.p_column_step==1)
52  {
53  resize(a.n(), FALSE);
54  memcpy((void *)(this->p_memory), (const void *)(a.p_memory), this->n() * sizeof(T));
55  }
56 else
57  ((EST_TVector<T> *)this)->copy(a);
58 }
59 
61 {
62  this->default_vals();
63  copy(in);
64 }
65 
66 // should copy from and delete old version first
67 template<class T> void EST_TSimpleVector<T>::resize(int newn, int set)
68 {
69  int oldn = this->n();
70  T *old_vals =NULL;
71  int old_offset = this->p_offset;
72  unsigned int q;
73 
74  this->just_resize(newn, &old_vals);
75 
76  if (set && old_vals)
77  {
78  int copy_c = 0;
79  if (this->p_memory != NULL)
80  {
81  copy_c = Lof(this->n(), oldn);
82  for (q=0; q<copy_c* sizeof(T); q++) /* for memcpy */
83  ((char *)this->p_memory)[q] = ((char *)old_vals)[q];
84  }
85 
86  for (int i=copy_c; i < this->n(); ++i)
87  this->p_memory[i] = *this->def_val;
88  }
89 
90  if (old_vals != NULL && old_vals != this->p_memory && !this->p_sub_matrix)
91  delete [] (old_vals - old_offset);
92 
93 }
94 
95 template<class T>
96 void EST_TSimpleVector<T>::copy_section(T* dest, int offset, int num) const
97 {
98  unsigned int q;
99  if (num<0)
100  num = this->num_columns()-offset;
101 
102  if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE))
103  return;
104 
105  if (!this->p_sub_matrix && this->p_column_step==1)
106  {
107  for (q=0; q<num* sizeof(T); q++) /* for memcpy */
108  ((char *)dest)[q] = ((char *)(this->p_memory+offset))[q];
109  }
110  else
111  for(int i=0; i<num; i++)
112  dest[i] = this->a_no_check(offset+i);
113 }
114 
115 template<class T>
116 void EST_TSimpleVector<T>::set_section(const T* src, int offset, int num)
117 {
118  unsigned int q;
119  if (num<0)
120  num = this->num_columns()-offset;
121 
122  if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE))
123  return;
124 
125  if (!this->p_sub_matrix && this->p_column_step==1)
126  {
127  for (q=0; q<num* sizeof(T); q++) /* for memcpy */
128  ((char *)(this->p_memory+offset))[q] = ((char *)(src))[q];
129  }
130  else
131  for(int i=0; i<num; i++)
132  this->a_no_check(offset+i) = src[i];
133 }
134 
136 {
137  copy(in);
138  return *this;
139 }
140 
141 template<class T> void EST_TSimpleVector<T>::zero()
142 {
143  if (this->p_column_step==1)
144  memset((void *)(this->p_memory), 0, this->n() * sizeof(T));
145  else
146  ((EST_TVector<T> *)this)->fill(*this->def_val);
147 }
148 
149