Edinburgh Speech Tools  2.4-release
EST_TSimpleMatrix.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 TMatrix 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_TSimpleMatrix.h"
44 #include "EST_TVector.h"
45 #include <fstream>
46 #include <iostream>
47 #include <cstring>
48 #include "EST_cutils.h"
49 
50 using std::memcpy;
51 
52 
53 template<class T>
55 {
56 
57  if (!a.p_sub_matrix && !this->p_sub_matrix)
58  memcpy((void *)&this->a_no_check(0,0),
59  (const void *)&a.a_no_check(0,0),
60  this->num_rows()*this->num_columns()*sizeof(T)
61  );
62  else
63  {
64  for (int i = 0; i < this->num_rows(); ++i)
65  for (int j = 0; j < this->num_columns(); ++j)
66  this->a_no_check(i,j) = a.a_no_check(i,j);
67  }
68 }
69 
70 template<class T>
72 {
73  if (this->num_rows() != a.num_rows() || this->num_columns() != a.num_columns())
74  resize(a.num_rows(), a.num_columns(), 0);
75 
76  copy_data(a);
77 }
78 
79 template<class T>
81 {
82  copy(in);
83 }
84 
85 template<class T>
86 void EST_TSimpleMatrix<T>::resize(int new_rows,
87  int new_cols,
88  int set)
89 {
90  T* old_vals=NULL;
91  int old_offset = this->p_offset;
92  unsigned int q;
93 
94  if (new_rows<0)
95  new_rows = this->num_rows();
96  if (new_cols<0)
97  new_cols = this->num_columns();
98 
99  if (set)
100  {
101  if (!this->p_sub_matrix && new_cols == this->num_columns() && new_rows != this->num_rows())
102  {
103  int copy_r = Lof(this->num_rows(), new_rows);
104 
105  this->just_resize(new_rows, new_cols, &old_vals);
106 
107  for (q=0; q<(copy_r*new_cols*sizeof(T)); q++) /* memcpy */
108  ((char *)this->p_memory)[q] = ((char *)old_vals)[q];
109 
110  int i,j;
111 
112  if (new_rows > copy_r)
113  {
114  if (*this->def_val == 0)
115  {
116  for (q=0; q<(new_rows-copy_r)*new_cols*sizeof(T); q++) /* memset */
117  ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0;
118  }
119  else
120  {
121  for(j=0; j<new_cols; j++)
122  for(i=copy_r; i<new_rows; i++)
123  this->a_no_check(i,j) = *this->def_val;
124  }
125  }
126  }
127  else if (!this->p_sub_matrix)
128  {
129  int old_row_step = this->p_row_step;
130  int old_column_step = this->p_column_step;
131  int copy_r = Lof(this->num_rows(), new_rows);
132  int copy_c = Lof(this->num_columns(), new_cols);
133 
134  this->just_resize(new_rows, new_cols, &old_vals);
135 
136  this->set_values(old_vals,
137  old_row_step, old_column_step,
138  0, copy_r,
139  0, copy_c);
140 
141  int i,j;
142 
143  for(i=0; i<copy_r; i++)
144  for(j=copy_c; j<new_cols; j++)
145  this->a_no_check(i,j) = *this->def_val;
146 
147  if (new_rows > copy_r)
148  {
149  if (*this->def_val == 0)
150  {
151  for (q=0; q<((new_rows-copy_r)*new_cols*sizeof(T)); q++) /* memset */
152  ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0;
153  }
154  else
155  {
156  for(j=0; j<new_cols; j++)
157  for(i=copy_r; i<new_rows; i++)
158  this->a_no_check(i,j) = *this->def_val;
159  }
160  }
161  }
162  else
163  EST_TMatrix<T>::resize(new_rows, new_cols, 1);
164  }
165  else
166  EST_TMatrix<T>::resize(new_rows, new_cols, 0);
167 
168  if (old_vals && old_vals != this->p_memory)
169  delete [] (old_vals-old_offset);
170 }
171 
173 {
174  copy(in);
175  return *this;
176 }
177 
EST_TMatrix::num_columns
int num_columns() const
return number of columns
Definition: EST_TMatrix.h:181
EST_TSimpleMatrix
Definition: EST_TSimpleMatrix.h:50
EST_TSimpleMatrix::copy
void copy(const EST_TSimpleMatrix< T > &a)
copy one matrix into another
Definition: EST_TSimpleMatrix.cc:71
EST_TMatrix::num_rows
int num_rows() const
return number of rows
Definition: EST_TMatrix.h:179
EST_TMatrix::resize
void resize(int rows, int cols, int set=1)
Definition: EST_TMatrix.cc:250
EST_TMatrix::a_no_check
INLINE const T & a_no_check(int row, int col) const
const access with no bounds check, care recommend
Definition: EST_TMatrix.h:184
EST_TSimpleMatrix::resize
void resize(int rows, int cols, int set=1)
resize matrix
Definition: EST_TSimpleMatrix.cc:86
EST_TSimpleMatrix::operator=
EST_TSimpleMatrix< T > & operator=(const EST_TSimpleMatrix< T > &s)
assignment operator
Definition: EST_TSimpleMatrix.cc:172
EST_TSimpleMatrix::EST_TSimpleMatrix
EST_TSimpleMatrix(void)
default constructor
Definition: EST_TSimpleMatrix.h:58