Edinburgh Speech Tools 2.4-release
 
Loading...
Searching...
No Matches
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
50using std::memcpy;
51
52
53template<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 }
69
70template<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
79template<class T>
84
85template<class T>
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
int num_columns() const
return number of columns
INLINE const T & a_no_check(int row, int col) const
const access with no bounds check, care recommend
int num_rows() const
return number of rows
void resize(int rows, int cols, int set=1)
void resize(int rows, int cols, int set=1)
resize matrix
EST_TSimpleMatrix(void)
default constructor
void copy(const EST_TSimpleMatrix< T > &a)
copy one matrix into another
EST_TSimpleMatrix< T > & operator=(const EST_TSimpleMatrix< T > &s)
assignment operator