HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_RefMatrix.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  * This is a class template that implements a resizable matrix of
10  * arbitrary objects. The template parameter represents the type
11  * of object, and is called utRef. You can instantiate this class
12  * with any object or pointer, such as:
13  * UT_RefMatrix<int> iObj;
14  * UT_RefMatrix<GeoPrimMesh> mObj;
15  * UT_RefMatrix<GeoPoint*> pObj; ... etc
16  * The "Ref" in the class name stands for "passed by reference", which
17  * is the argument passing strategy used by this class. Use this class
18  * if you want to construct 2D arrays of arbitrary objects.
19  */
20 
21 #ifndef __UT_RefMatrix_h__
22 #define __UT_RefMatrix_h__
23 
24 #include "UT_Array.h"
25 
26 
27 template <typename T>
28 class UT_RefMatrix {
29 public:
30  typedef int (*Comparator)(const T *, const T *);
31 
32  // Trivial constructor and destructor:
33  explicit UT_RefMatrix(unsigned int mSz = 0, unsigned int nSz = 0);
34  ~UT_RefMatrix();
35 
36  int64 getMemoryUsage(bool inclusive) const
37  {
38  int64 mem = inclusive ? sizeof(*this) : 0;
39  mem += myArray.getMemoryUsage(false);
40  return mem;
41  }
42 
43  // Copy constructor that copies each T from the source matrix to
44  // this matrix using the '=' operator.
45  UT_RefMatrix(const UT_RefMatrix<T> &m);
46 
47  void swap(UT_RefMatrix<T> &other);
48 
49  // Append a row or insert it at a given index. The matrix is grown to
50  // accommodate it. Return the index of the row that was added.
51  unsigned int insertRow(unsigned int index);
52  unsigned int insertRow(const UT_Array<T> &r, unsigned int index);
53  unsigned int appendRow(const UT_Array<T> &r);
54 
55  // Append a column or insert it at a given index. The matrix is grown to
56  // accommodate it. Return the index of the column that was added.
57  unsigned int insertCol(unsigned int index);
58  unsigned int insertCol(const UT_Array<T> &c, unsigned int index);
59  unsigned int appendCol(const UT_Array<T> &c);
60 
61  // Remove a row or column from the matrix
62  int removeRow(unsigned int index);
63  int removeCol(unsigned int index);
64 
65  // Performs a cyclical shift on the rows and columns
66  void cycle(int rowshift, int colshift);
67 
68  // Compare the two matrices: to return 1, they should have the same
69  // number or entries, and their Things should all match when compared
70  // with '==' or compare() respectively.
71  // t1 and t2 must be pointers to T.
72  int operator==(const UT_RefMatrix<T> &m) const;
73 
74  // Assignment operator that copies the array piece by piece using the
75  // '=' operator defined in UT_Array. The row and column size of this
76  // matrix must be greater or equal to the "used" row and column count of
77  // matrix m.
79 
80  // Return an element of the matrix, WITHOUT any BOUND CHECKING. Use
81  // safeGet() if you don't know whether you are within range or not.
82  const T &operator()(unsigned i, unsigned j) const
83  {
84  return myArray(i*myCols+j);
85  }
86  T &operator()(unsigned i, unsigned j)
87  {
88  return myArray(i*myCols+j);
89  }
90 
91  // Return an element of the matrix, growing the matrix if necessary in
92  // the non-const case.
93  const T safeGet(unsigned i, unsigned j) const
94  {
95  return (i < rows() && j < cols()) ?
96  (*this)(i,j) : T();
97  }
98  T &safeGet(unsigned i, unsigned j)
99  {
100  if (i >= rows()) insertRow(i);
101  if (j >= cols()) insertCol(j);
102  return (*this)(i,j);
103  }
104 
105  // Linearly search for t and return index 0 if found, else -1.
106  // When found, the indices are returned in (m,n).
107  int find(const T &t, unsigned int *m, unsigned int *n) const;
108 
109  // Return the number of rows and column space has been allocated for, and
110  // the number of entries used in the rows and columns.
111  unsigned int rows() const { return myRows; }
112  unsigned int cols() const { return myCols; }
113 
114  // Resize the array. This function will change the number of rows or
115  // columns in the matrix.
116  void resize(unsigned int mSz, unsigned int nSz);
117 
118  // Apply a user-defined function to each element of the matrix
119  // as int as the function returns zero. If apply_func returns
120  // 1, apply() stops traversing the list and returns 1; otherwise, apply()
121  // returns 0;
122  int apply(int (*apply_func)(T &t, void *d), void *d);
123 
124 private:
125  // Array of rows, where each row is a reference array of type T:
126  UT_Array<T> myArray;
127  unsigned int myRows;
128  unsigned int myCols;
129 };
130 
131 #include "UT_RefMatrixImpl.h"
132 
133 #endif
int apply(int(*apply_func)(T &t, void *d), void *d)
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_RefMatrix< T > & operator=(const UT_RefMatrix< T > &m)
int removeRow(unsigned int index)
unsigned int insertRow(unsigned int index)
void swap(UT_RefMatrix< T > &other)
int64 getMemoryUsage(bool inclusive) const
Definition: UT_RefMatrix.h:36
UT_RefMatrix(unsigned int mSz=0, unsigned int nSz=0)
T & safeGet(unsigned i, unsigned j)
Definition: UT_RefMatrix.h:98
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
long long int64
Definition: SYS_Types.h:116
unsigned int insertCol(unsigned int index)
const T safeGet(unsigned i, unsigned j) const
Definition: UT_RefMatrix.h:93
const T & operator()(unsigned i, unsigned j) const
Definition: UT_RefMatrix.h:82
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(* Comparator)(const T *, const T *)
Definition: UT_RefMatrix.h:30
int operator==(const UT_RefMatrix< T > &m) const
GLboolean r
Definition: glcorearb.h:1222
unsigned int appendRow(const UT_Array< T > &r)
T & operator()(unsigned i, unsigned j)
Definition: UT_RefMatrix.h:86