Edinburgh Speech Tools  2.1-release
EST_TSimpleMatrix.cc
Go to the documentation of this file.
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 template<class T>
52 {
53 
54  if (!a.p_sub_matrix && !this->p_sub_matrix)
55  std::memcpy((void *)&this->a_no_check(0,0),
56  (const void *)&a.a_no_check(0,0),
57  this->num_rows()*this->num_columns()*sizeof(T)
58  );
59  else
60  {
61  for (int i = 0; i < this->num_rows(); ++i)
62  for (int j = 0; j < this->num_columns(); ++j)
63  this->a_no_check(i,j) = a.a_no_check(i,j);
64  }
65 }
66 
67 template<class T>
69 {
70  if (this->num_rows() != a.num_rows() || this->num_columns() != a.num_columns())
71  resize(a.num_rows(), a.num_columns(), 0);
72 
73  copy_data(a);
74 }
75 
76 template<class T>
78 {
79  copy(in);
80 }
81 
82 template<class T>
83 void EST_TSimpleMatrix<T>::resize(int new_rows,
84  int new_cols,
85  int set)
86 {
87  T* old_vals=NULL;
88  int old_offset = this->p_offset;
89  unsigned int q;
90 
91  if (new_rows<0)
92  new_rows = this->num_rows();
93  if (new_cols<0)
94  new_cols = this->num_columns();
95 
96  if (set)
97  {
98  if (!this->p_sub_matrix && new_cols == this->num_columns() && new_rows != this->num_rows())
99  {
100  int copy_r = Lof(this->num_rows(), new_rows);
101 
102  this->just_resize(new_rows, new_cols, &old_vals);
103 
104  for (q=0; q<(copy_r*new_cols*sizeof(T)); q++) /* memcpy */
105  ((char *)this->p_memory)[q] = ((char *)old_vals)[q];
106 
107  int i,j;
108 
109  if (new_rows > copy_r)
110  {
111  if (*this->def_val == 0)
112  {
113  for (q=0; q<(new_rows-copy_r)*new_cols*sizeof(T); q++) /* memset */
114  ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0;
115  }
116  else
117  {
118  for(j=0; j<new_cols; j++)
119  for(i=copy_r; i<new_rows; i++)
120  this->a_no_check(i,j) = *this->def_val;
121  }
122  }
123  }
124  else if (!this->p_sub_matrix)
125  {
126  int old_row_step = this->p_row_step;
127  int old_column_step = this->p_column_step;
128  int copy_r = Lof(this->num_rows(), new_rows);
129  int copy_c = Lof(this->num_columns(), new_cols);
130 
131  this->just_resize(new_rows, new_cols, &old_vals);
132 
133  this->set_values(old_vals,
134  old_row_step, old_column_step,
135  0, copy_r,
136  0, copy_c);
137 
138  int i,j;
139 
140  for(i=0; i<copy_r; i++)
141  for(j=copy_c; j<new_cols; j++)
142  this->a_no_check(i,j) = *this->def_val;
143 
144  if (new_rows > copy_r)
145  {
146  if (*this->def_val == 0)
147  {
148  for (q=0; q<((new_rows-copy_r)*new_cols*sizeof(T)); q++) /* memset */
149  ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0;
150  }
151  else
152  {
153  for(j=0; j<new_cols; j++)
154  for(i=copy_r; i<new_rows; i++)
155  this->a_no_check(i,j) = *this->def_val;
156  }
157  }
158  }
159  else
160  EST_TMatrix<T>::resize(new_rows, new_cols, 1);
161  }
162  else
163  EST_TMatrix<T>::resize(new_rows, new_cols, 0);
164 
165  if (old_vals && old_vals != this->p_memory)
166  delete [] (old_vals-old_offset);
167 }
168 
170 {
171  copy(in);
172  return *this;
173 }
174 
ssize_t p_column_step
Definition: EST_TVector.h:159
Template Matrix class. This is an extension of the EST_TVector class to two dimensions.
Definition: EST_TMatrix.h:89
ssize_t num_columns() const
return number of columns
Definition: EST_TMatrix.h:179
bool p_sub_matrix
Definition: EST_TVector.h:161
ssize_t num_rows() const
return number of rows
Definition: EST_TMatrix.h:177
EST_TSimpleMatrix(void)
default constructor
#define Lof(a, b)
Definition: EST_cutils.h:97
const EST_DMatrix * def_val
void just_resize(ssize_t new_rows, ssize_t new_cols, T **old_vals)
resize the memory and reset the bounds, but don&#39;t set values.
Definition: EST_TMatrix.cc:209
void copy(const EST_TSimpleMatrix< T > &a)
copy one matrix into another
NULL
Definition: EST_WFST.cc:55
void copy_data(const EST_TSimpleMatrix< T > &a)
void resize(ssize_t rows, ssize_t cols, ssize_t set=1)
Definition: EST_TMatrix.cc:250
INLINE const T & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
Definition: EST_TMatrix.h:182
void set_values(const T *data, ssize_t r_step, ssize_t c_step, ssize_t start_r, ssize_t num_r, ssize_t start_c, ssize_t num_c)
Get and set values from array.
Definition: EST_TMatrix.cc:129
void resize(int rows, int cols, int set=1)
resize matrix
A subclass of EST_TMatrix which copies using memcopy.
ssize_t p_offset
How to access the memory.
Definition: EST_TVector.h:158
ssize_t p_row_step
How to access the memory.
Definition: EST_TMatrix.h:97
EST_TSimpleMatrix< T > & operator=(const EST_TSimpleMatrix< T > &s)
assignment operator