HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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(void);
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  // Search for t in each row in one of two ways: linearly using the '=='
106  // operator, or binary using the function specified in the parameter list
107  // respectively. find() returns 0 and the indices of the matching element,
108  // or -1 if not found. t1 and t2 must be pointers to T.
109  int find(const T &t, unsigned int *m, unsigned int *n) const;
110  int find(const T &t, unsigned int *m, unsigned int *n,
111  Comparator compare) const;
112 
113  // Return the number of rows and column space has been allocated for, and
114  // the number of entries used in the rows and columns.
115  unsigned int rows(void) const { return myRows; }
116  unsigned int cols(void) const { return myCols; }
117 
118  // Resize the array. This function will change the number of rows or
119  // columns in the matrix.
120  void resize(unsigned int mSz, unsigned int nSz);
121 
122  // Apply a user-defined function to each element of the matrix
123  // as int as the function returns zero. If applyFct returns
124  // 1, apply() stops traversing the list and returns 1; otherwise, apply()
125  // returns 0;
126  int apply(int (*applyFct)(T &t, void *d), void *d);
127 
128 private:
129  // Array of rows, where each row is a reference array of type T:
130  UT_Array<T> myArray;
131  unsigned int myRows;
132  unsigned int myCols;
133 };
134 
135 #if defined(WIN32) || defined(LINUX) || defined(MBSD) || defined(GAMEOS)
136  #include "UT_RefMatrixImpl.h"
137 #endif
138 
139 
140 #endif
UT_RefMatrix< T > & operator=(const UT_RefMatrix< T > &m)
int removeRow(unsigned int index)
unsigned int insertRow(unsigned int index)
unsigned int cols(void) const
Definition: UT_RefMatrix.h:116
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)
png_uint_32 i
Definition: png.h:2877
T & safeGet(unsigned i, unsigned j)
Definition: UT_RefMatrix.h:98
void resize(unsigned int mSz, unsigned int nSz)
long long int64
Definition: SYS_Types.h:100
GLdouble n
Definition: glcorearb.h:2007
int apply(int(*applyFct)(T &t, void *d), void *d)
int find(const T &t, unsigned int *m, unsigned int *n) const
unsigned int rows(void) const
Definition: UT_RefMatrix.h:115
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
int removeCol(unsigned int index)
typedef int
Definition: png.h:1175
void cycle(int rowshift, int colshift)
unsigned int appendCol(const UT_Array< T > &c)
GLuint index
Definition: glcorearb.h:785
int(* Comparator)(const T *, const T *)
Definition: UT_RefMatrix.h:30
int operator==(const UT_RefMatrix< T > &m) const
GLboolean r
Definition: glcorearb.h:1221
unsigned int appendRow(const UT_Array< T > &r)
T & operator()(unsigned i, unsigned j)
Definition: UT_RefMatrix.h:86