Edinburgh Speech Tools  2.4-release
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 <cstring>
47 #include "EST_cutils.h"
48 
49 using std::memset;
50 using std::memcpy;
51 
52 template<class T> void EST_TSimpleVector<T>::copy(const EST_TSimpleVector<T> &a)
53 {
54  if (this->p_column_step==1 && a.p_column_step==1)
55  {
56  resize(a.n(), FALSE);
57  memcpy((void *)(this->p_memory), (const void *)(a.p_memory), this->n() * sizeof(T));
58  }
59 else
60  ((EST_TVector<T> *)this)->copy(a);
61 }
62 
64 {
65  this->default_vals();
66  copy(in);
67 }
68 
69 // should copy from and delete old version first
70 template<class T> void EST_TSimpleVector<T>::resize(int newn, int set)
71 {
72  int oldn = this->n();
73  T *old_vals =NULL;
74  int old_offset = this->p_offset;
75  unsigned int q;
76 
77  this->just_resize(newn, &old_vals);
78 
79  if (set && old_vals)
80  {
81  int copy_c = 0;
82  if (this->p_memory != NULL)
83  {
84  copy_c = Lof(this->n(), oldn);
85  for (q=0; q<copy_c* sizeof(T); q++) /* for memcpy */
86  ((char *)this->p_memory)[q] = ((char *)old_vals)[q];
87  }
88 
89  for (int i=copy_c; i < this->n(); ++i)
90  this->p_memory[i] = *this->def_val;
91  }
92 
93  if (old_vals != NULL && old_vals != this->p_memory && !this->p_sub_matrix)
94  delete [] (old_vals - old_offset);
95 
96 }
97 
98 template<class T>
99 void EST_TSimpleVector<T>::copy_section(T* dest, int offset, int num) const
100 {
101  unsigned int q;
102  if (num<0)
103  num = this->num_columns()-offset;
104 
105  if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE))
106  return;
107 
108  if (!this->p_sub_matrix && this->p_column_step==1)
109  {
110  for (q=0; q<num* sizeof(T); q++) /* for memcpy */
111  ((char *)dest)[q] = ((char *)(this->p_memory+offset))[q];
112  }
113  else
114  for(int i=0; i<num; i++)
115  dest[i] = this->a_no_check(offset+i);
116 }
117 
118 template<class T>
119 void EST_TSimpleVector<T>::set_section(const T* src, int offset, int num)
120 {
121  unsigned int q;
122  if (num<0)
123  num = this->num_columns()-offset;
124 
125  if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE))
126  return;
127 
128  if (!this->p_sub_matrix && this->p_column_step==1)
129  {
130  for (q=0; q<num* sizeof(T); q++) /* for memcpy */
131  ((char *)(this->p_memory+offset))[q] = ((char *)(src))[q];
132  }
133  else
134  for(int i=0; i<num; i++)
135  this->a_no_check(offset+i) = src[i];
136 }
137 
139 {
140  copy(in);
141  return *this;
142 }
143 
144 template<class T> void EST_TSimpleVector<T>::zero()
145 {
146  if (this->p_column_step==1)
147  memset((void *)(this->p_memory), 0, this->n() * sizeof(T));
148  else
149  ((EST_TVector<T> *)this)->fill(*this->def_val);
150 }
151 
152 
EST_TSimpleVector::resize
void resize(int n, int set=1)
resize vector
Definition: EST_TSimpleVector.cc:70
EST_TVector::p_memory
T * p_memory
Definition: EST_TVector.h:154
EST_TSimpleVector::EST_TSimpleVector
EST_TSimpleVector()
default constructor
Definition: EST_TSimpleVector.h:58
EST_TSimpleVector::zero
void zero(void)
Fill entire array with 0 bits.
Definition: EST_TSimpleVector.cc:144
EST_TSimpleVector::operator=
EST_TSimpleVector & operator=(const EST_TSimpleVector< T > &s)
assignment operator
Definition: EST_TSimpleVector.cc:138
EST_TSimpleVector
Definition: EST_TSimpleVector.h:52
EST_TVector::n
INLINE int n() const
number of items in vector.
Definition: EST_TVector.h:254
EST_TVector
Definition: EST_TVector.h:147