home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / matrix.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  6.5 KB  |  230 lines

  1. ////////////////////////////////////////////////////////////
  2. // matrix.h: Matrix class template.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. ////////////////////////////////////////////////////////////
  5. #ifndef H_MATRIX
  6. #define H_MATRIX
  7. #include "vector.h"
  8.  
  9. #define INLINE
  10.  
  11. template<class TYPE>
  12. class Matrix {
  13. protected:
  14.   Vector<TYPE> data;
  15.   unsigned nrows, ncols, rowstride, colstride;
  16. public:
  17.   Matrix(unsigned nr=0, unsigned nc=0, const TYPE *s = 0);
  18.   Matrix(const Matrix<TYPE> &m);
  19.   Matrix(const Matrix<TYPE> &m, SliceType styp,
  20.          unsigned sr=0, unsigned sc=0, 
  21.          unsigned nr=0, unsigned nc=0);
  22.   void Copy(const Matrix<TYPE> &m);
  23.   void Share(const Matrix<TYPE> &m);
  24.   Matrix<TYPE> Transpose();
  25.   Matrix<TYPE> &operator=(const Matrix<TYPE> &m);
  26.   Matrix<TYPE> &operator=(const TYPE &x);
  27. #ifndef NO_RANGE_CHECK
  28.   unsigned CheckRow(unsigned i) const;
  29.   unsigned CheckCol(unsigned i) const;
  30. #endif
  31.   Vector<TYPE> operator[](unsigned r);
  32.   const Vector<TYPE> operator[](unsigned r) const;
  33.   TYPE &operator()(unsigned r, unsigned c);
  34.   const TYPE &operator()(unsigned r, unsigned c) const;
  35.   Vector<TYPE> Row(unsigned r, SliceType styp=SHARED);
  36.   Vector<TYPE> Col(unsigned c, SliceType styp=SHARED);
  37.   Vector<TYPE> Diag(SliceType styp=SHARED);
  38.   Vector<TYPE> All(SliceType styp=SHARED);
  39.   const Vector<TYPE> Row(unsigned r, SliceType styp=SHARED) const;
  40.   const Vector<TYPE> Col(unsigned c, SliceType styp=SHARED) const;
  41.   const Vector<TYPE> Diag(SliceType styp=SHARED) const;
  42.   const Vector<TYPE> All(SliceType styp=SHARED) const;
  43.   // Mid-level hooks. Use these at your own risk:
  44.   VecPtr<TYPE> RowPtr(unsigned r=0);
  45.   VecPtr<TYPE> ColPtr(unsigned c=0);
  46.   VecPtr<TYPE> DiagPtr();
  47.   VecPtr<TYPE> PtrToAll();
  48.   VecPtr<const TYPE> RowPtr(unsigned r=0) const;
  49.   VecPtr<const TYPE> ColPtr(unsigned c=0) const;
  50.   VecPtr<const TYPE> DiagPtr() const;
  51.   VecPtr<const TYPE> PtrToAll() const;
  52.   int IsNull() const;
  53.   int IsUnique() const;
  54.   Matrix<TYPE> Clone() const;
  55.   int EnsureUnique();
  56.   unsigned NCols() const;
  57.   unsigned NRows() const;
  58.   unsigned RowStride() const;
  59.   unsigned ColStride() const;
  60.   int IsRowMajor() const;
  61.   int IsColMajor() const;
  62.   int IsSquare() const;
  63. };
  64.  
  65. template<class TYPE>
  66. INLINE Matrix<TYPE> &Matrix<TYPE>::operator=(const Matrix<TYPE> &m)
  67. // Share semantics used for assignment.
  68. {
  69.   if (this != &m) Share(m); // Note trap for assignment to self.
  70.   return *this;
  71. }
  72.  
  73. template<class TYPE>
  74. INLINE Vector<TYPE> Matrix<TYPE>::operator[](unsigned r)
  75. // Row subscripting operator for non-const matrices. 
  76. // This routine does the same thing as Row(r,SHARED).
  77. {
  78.   return Vector<TYPE>(data, SHARED,
  79.                       ncols, rowstride, CHECKROW(r)*colstride);
  80. }
  81.  
  82. template<class TYPE>
  83. INLINE const Vector<TYPE> Matrix<TYPE>::operator[](unsigned r) const
  84. // Row subscripting operator for const matrices. 
  85. // This routine does the same thing as Row(r,SHARED).
  86. {
  87.   return Vector<TYPE>(data, SHARED,
  88.                       ncols, rowstride, CHECKROW(r)*colstride);
  89. }
  90.  
  91. template<class TYPE>
  92. INLINE VecPtr<TYPE> Matrix<TYPE>::RowPtr(unsigned r)
  93. // Returns vector pointer to row r. No reference counting
  94. // done. Use at your own risk.
  95. {
  96.   return VecPtr<TYPE>(data.start+CHECKROW(r)*colstride, rowstride);
  97. }
  98.  
  99. template<class TYPE>
  100. INLINE VecPtr<const TYPE> Matrix<TYPE>::RowPtr(unsigned r) const
  101. // Returns vector pointer to row r. No reference counting
  102. // done. Use at your own risk.
  103. {
  104.   return VecPtr<const TYPE>(data.start+CHECKROW(r)*colstride, rowstride);
  105. }
  106.  
  107.  
  108. template<class TYPE>
  109. INLINE VecPtr<TYPE> Matrix<TYPE>::ColPtr(unsigned c)
  110. // Returns vector pointer to column c. No reference counting
  111. // done. Use at your own risk.
  112. {
  113.   return VecPtr<TYPE>(data.start+CHECKCOL(c)*rowstride, colstride);
  114. }
  115.  
  116. template<class TYPE>
  117. INLINE VecPtr<const TYPE> Matrix<TYPE>::ColPtr(unsigned c) const
  118. // Returns vector pointer to column c. No reference counting
  119. // done. Use at your own risk.
  120. {
  121.   return VecPtr<const TYPE>(data.start+CHECKCOL(c)*rowstride, colstride);
  122. }
  123.  
  124. template<class TYPE>
  125. INLINE VecPtr<TYPE> Matrix<TYPE>::DiagPtr()
  126. // Returns vector pointer to right/downward diagonal.
  127. {
  128.   unsigned diastride = (rowstride == 1) ? colstride : rowstride;
  129.   diastride++;
  130.   return VecPtr<TYPE>(data.start, diastride);
  131. }
  132.  
  133. template<class TYPE>
  134. INLINE VecPtr<const TYPE> Matrix<TYPE>::DiagPtr() const
  135. // Returns vector pointer to right/downward diagonal.
  136. {
  137.   unsigned diastride = (rowstride == 1) ? colstride : rowstride;
  138.   diastride++;
  139.   return VecPtr<const TYPE>(data.start, diastride);
  140. }
  141.  
  142. template<class TYPE>
  143. INLINE VecPtr<TYPE> Matrix<TYPE>::PtrToAll()
  144. // Returns vector pointer to underlying 1D vector.
  145. // No reference counting done, so use at your own risk.
  146. {
  147.   return VecPtr<TYPE>(data.start, 1);
  148. }
  149.  
  150. template<class TYPE>
  151. INLINE VecPtr<const TYPE> Matrix<TYPE>::PtrToAll() const
  152. // Returns vector pointer to underlying 1D vector.
  153. // No reference counting done, so use at your own risk.
  154. {
  155.   return VecPtr<const TYPE>(data.start, 1);
  156. }
  157.  
  158. template<class TYPE>
  159. INLINE int Matrix<TYPE>::IsNull() const
  160. // Returns true if the matrix references null_rep.
  161. {
  162.   return data.IsNull();
  163. }
  164.  
  165. template<class TYPE>
  166. INLINE int Matrix<TYPE>::IsUnique() const
  167. // Returns true of matrix has only reference to shared data.
  168. {
  169.   return data.IsUnique();
  170. }
  171.  
  172. template<class TYPE>
  173. INLINE unsigned Matrix<TYPE>::NCols() const
  174. {
  175.   return ncols;
  176. }
  177.  
  178. template<class TYPE>
  179. INLINE unsigned Matrix<TYPE>::NRows() const
  180. {
  181.   return nrows;
  182. }
  183.  
  184. template<class TYPE>
  185. INLINE unsigned Matrix<TYPE>::RowStride() const
  186. // Returns the stride that a row vector would have
  187. // in the matrix. For normal row-major matrices,
  188. // the row stride will be 1. For shared submatrices, 
  189. // the rowstride is that of the parent matrix.
  190. {
  191.   return rowstride;
  192. }
  193.  
  194. template<class TYPE>
  195. INLINE unsigned Matrix<TYPE>::ColStride() const
  196. // Returns the stride that a column vector would have
  197. // in the matrix. For a column-major matrix, the
  198. // column stride will be 1. For shared submatrices.
  199. // the colstride is that of the parent matrix.
  200. {
  201.   return colstride;
  202. }
  203.  
  204. template<class TYPE>
  205. INLINE int Matrix<TYPE>::IsRowMajor() const
  206. {
  207.   return rowstride == 1;
  208. }
  209.  
  210. template<class TYPE>
  211. INLINE int Matrix<TYPE>::IsColMajor() const
  212. {
  213.   return colstride == 1;
  214. }
  215.  
  216. template<class TYPE>
  217. INLINE int Matrix<TYPE>::IsSquare() const
  218. {
  219.   return nrows == ncols;
  220. }
  221.  
  222. #undef INLINE
  223.  
  224. // Whether or not we should include the non-line methods for
  225. // our class templates here is implementation dependent.
  226.  
  227. #include "matrix.mth"
  228.  
  229. #endif
  230.