HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_RefMatrixImpl.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Utility Library (C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_RefMatrixImpl__
12 #define __UT_RefMatrixImpl__
13 
14 #ifndef __UT_RefMatrix_h__
15 #error Do not include this file directly. Include UT_RefMatrix.h instead.
16 #endif
17 
18 #include "UT_Assert.h"
19 #include "UT_Swap.h"
20 
21 template <class T>
22 UT_RefMatrix<T>::UT_RefMatrix(unsigned int m, unsigned int n)
23  : myRows(0)
24  , myCols(0)
25 {
26  resize(m, n);
27 }
28 
29 template <class T>
31  : myArray(m.myArray)
32  , myRows(m.myRows)
33  , myCols(m.myCols)
34 {
35 }
36 
37 template <class T>
39 {
40 }
41 
42 template <class T>
43 void
45 {
46  myArray.swap(other.myArray);
47  UTswap(myRows, other.myRows);
48  UTswap(myCols, other.myCols);
49 }
50 
51 template <class T>
52 unsigned int
54 {
55  if (index > rows())
56  myRows = index+1;
57  else
58  myRows++;
59  myArray.entries(myRows*myCols);
60  for (int i = rows(); i-- > index+1; )
61  for (int j = 0; j < cols(); j++)
62  (*this)(i, j) = (*this)(i-1, j);
63  return index;
64 }
65 
66 template <class T>
67 unsigned int
69 {
70  return insertRow(r, rows());
71 }
72 
73 template <class T>
74 unsigned int
76  unsigned int index)
77 {
78  insertRow(index);
79  for (int j = 0; j < cols(); j++)
80  (*this)(index, j) = r(j);
81  return index;
82 }
83 
84 template <class T>
85 unsigned int
87 {
88  int pcols = myCols;
89  if (index > cols())
90  myCols = index+1;
91  else
92  myCols++;
93 
94  myArray.entries(myRows*myCols);
95  for (int i = rows(); i-- > 0; )
96  {
97  for (int j = cols(); j-- > index+1; )
98  myArray(i*myCols+j) = myArray(i*pcols+j-1);
99  for (int j = index; j-- > 0; )
100  myArray(i*myCols+j) = myArray(i*pcols+j);
101  }
102  return index;
103 }
104 
105 template <class T>
106 unsigned int
108 {
109  return insertCol(r, cols());
110 }
111 
112 template <class T>
113 unsigned int
115 {
116  insertCol(index);
117  for (int i = 0; i < rows(); i++)
118  (*this)(i, index) = r(i);
119  return index;
120 }
121 
122 template <class T>
123 int
125 {
126  for (int i = index; i < rows()-1; i++)
127  {
128  for (int j = 0; j < cols(); j++)
129  (*this)(i, j) = (*this)(i+1, j);
130  }
131  myRows--;
132  myArray.entries(myRows*myCols);
133  return index;
134 }
135 
136 template <class T>
137 int
139 {
140  int ncols = myCols-1;
141  for (int i = 0; i < rows(); i++)
142  {
143  for (int j = 0; j < index; j++)
144  myArray(i*ncols+j) = myArray(i*myCols+j);
145  for (int j = index; j < cols()-1; j++)
146  myArray(i*ncols+j) = myArray(i*myCols+j+1);
147  }
148  myCols = ncols;
149  myArray.entries(myRows*myCols);
150  return index;
151 }
152 
153 template <class T>
154 void
155 UT_RefMatrix<T>::cycle(int rowshift, int colshift)
156 {
157  if (rowshift || colshift)
158  {
159  UT_RefMatrix<T> tmp(*this);
160  int ic, jc;
161 
162  ic = rowshift % rows();
163  ic = ic < 0 ? ic+rows() : ic;
164  for (int i = 0; i < rows(); i++)
165  {
166  jc = colshift % cols();
167  jc = jc < 0 ? jc+cols() : jc;
168  for (int j = 0; j < cols(); j++)
169  {
170  (*this)(ic, jc) = tmp(i, j);
171  jc++; if (jc >= cols()) jc -= cols();
172  }
173  ic++; if (ic >= rows()) ic -= rows();
174  }
175  }
176 }
177 
178 template <class T>
179 int
180 UT_RefMatrix<T>::find(const T &t, unsigned int *m,
181  unsigned int *n) const
182 {
183  for (*m = 0; *m < rows(); (*m)++)
184  for (*n = 0; *n < cols(); (*n)++)
185  if ((*this)(*m,*n) == t) return 0;
186  return -1;
187 }
188 
189 template <class T>
190 void
191 UT_RefMatrix<T>::resize(unsigned int mSz, unsigned int nSz)
192 {
193  if (mSz == rows() && nSz == cols())
194  return;
195 
196  UT_RefMatrix<T> tmp;
197 
198  // Avoid a copy
199  tmp.swap(*this);
200 
201  myRows = mSz;
202  myCols = nSz;
203  myArray.entries(myRows*myCols);
204 
205  for (int i = 0; i < SYSmin(rows(), tmp.rows()); i++)
206  for (int j = 0; j < SYSmin(cols(), tmp.cols()); j++)
207  (*this)(i, j) = tmp(i, j);
208 }
209 
210 template <class T>
213 {
214  if (this == &m) return *this;
215  myArray = m.myArray;
216  myRows = m.myRows;
217  myCols = m.myCols;
218  return *this;
219 }
220 
221 template <class T>
222 int
224 {
225  if (this == &m) return 1;
226  if (!(rows() == m.rows() && cols() == m.cols()))
227  return 0;
228  return myArray == m.myArray;
229 }
230 
231 template <class T>
232 int
233 UT_RefMatrix<T>::apply(int (*apply_func)(T &t, void *d), void *d)
234 {
235  if (myArray.apply(apply_func,d) != myArray.entries())
236  return 1;
237  return 0;
238 }
239 
240 #endif // __UT_RefMatrixImpl__
int apply(int(*apply_func)(T &t, void *d), void *d)
UT_RefMatrix< T > & operator=(const UT_RefMatrix< T > &m)
int removeRow(unsigned int index)
unsigned int insertRow(unsigned int index)
void UTswap(T &a, T &b)
Definition: UT_Swap.h:35
void swap(UT_RefMatrix< T > &other)
UT_RefMatrix(unsigned int mSz=0, unsigned int nSz=0)
void resize(unsigned int mSz, unsigned int nSz)
unsigned int rows() const
Definition: UT_RefMatrix.h:111
GLdouble n
Definition: glcorearb.h:2008
int find(const T &t, unsigned int *m, unsigned int *n) const
unsigned int insertCol(unsigned int index)
GLdouble t
Definition: glad.h:2397
int removeCol(unsigned int index)
GLint j
Definition: glad.h:2733
void cycle(int rowshift, int colshift)
unsigned int appendCol(const UT_Array< T > &c)
unsigned int cols() const
Definition: UT_RefMatrix.h:112
GLuint index
Definition: glcorearb.h:786
int operator==(const UT_RefMatrix< T > &m) const
GLboolean r
Definition: glcorearb.h:1222
#define SYSmin(a, b)
Definition: SYS_Math.h:1539
unsigned int appendRow(const UT_Array< T > &r)