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

  1. /////////////////////////////////////////////////////////////////
  2. // simpmat.h: Class definition for simple dynamic matrices.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. /////////////////////////////////////////////////////////////////
  5. #ifndef H_SIMPLEMAT
  6. #define H_SIMPLEMAT
  7. #include "vecptr.h"
  8.  
  9. #define INLINE
  10.  
  11. // Note: You can define the macro constant VECTORPTRRETURNS
  12. // to change the class to return vector pointers for rows
  13. // instead of ordinary pointers. By switching back and forth
  14. // you can see for yourself what kind of overhead vector
  15. // pointers do or don't have over ordinary pointers.
  16.  
  17. template<class TYPE>
  18. class SimpleMatrix {
  19. private:
  20.   TYPE *data;
  21.   unsigned nrows, ncols;
  22. public:
  23.   SimpleMatrix(unsigned nr, unsigned nc);
  24.   SimpleMatrix(const SimpleMatrix<TYPE> &m);
  25.   ~SimpleMatrix();
  26.   void Copy(const SimpleMatrix<TYPE> &m);
  27.   SimpleMatrix<TYPE> &operator=(const SimpleMatrix<TYPE> &m);
  28. #ifndef VECTORPTRRETURNS
  29.   TYPE *operator[](unsigned r);
  30. #else
  31.   VecPtr<TYPE> operator[](unsigned r);
  32. #endif
  33.   VecPtr<TYPE> RowPtr(unsigned r=0);
  34.   VecPtr<TYPE> ColPtr(unsigned c=0);
  35.   VecPtr<TYPE> DiagPtr();
  36.   VecPtr<TYPE> PtrToAll();
  37. #ifndef VECTORPTRRETURNS
  38.   const TYPE *operator[](unsigned r) const;
  39. #else
  40.   VecPtr<const TYPE> operator[](unsigned r) const;
  41. #endif
  42.   VecPtr<const TYPE> RowPtr(unsigned r=0) const;
  43.   VecPtr<const TYPE> ColPtr(unsigned c=0) const;
  44.   VecPtr<const TYPE> DiagPtr() const;
  45.   VecPtr<const TYPE> PtrToAll() const;
  46.   unsigned NRows() const;
  47.   unsigned NCols() const;
  48.   unsigned NElems() const;
  49. };
  50.  
  51. template<class TYPE>
  52. INLINE SimpleMatrix<TYPE>::~SimpleMatrix()
  53. {
  54.   delete[] data;
  55. }
  56.  
  57. template<class TYPE>
  58. INLINE SimpleMatrix<TYPE> &
  59. SimpleMatrix<TYPE>::operator=(const SimpleMatrix<TYPE> &m)
  60. {
  61.   if (this != &m) Copy(m); // Trap assignment to self
  62.   return *this;
  63. }
  64.  
  65. // Selectors for non-const matrices
  66.  
  67. #ifndef VECTORPTRRETURNS
  68. template<class TYPE>
  69. INLINE TYPE *SimpleMatrix<TYPE>::operator[](unsigned r)
  70. // Returns an ordinary pointer (stride 1) to row r
  71. {
  72.   return data + r*ncols;
  73. }
  74. #else
  75. template<class TYPE>
  76. INLINE VecPtr<TYPE> SimpleMatrix<TYPE>::operator[](unsigned r)
  77. // Returns an vector pointer (stride 1) to row r
  78. {
  79.   return VecPtr<TYPE>(data + r*ncols);
  80. }
  81. #endif
  82.  
  83. template<class TYPE>
  84. INLINE VecPtr<TYPE> SimpleMatrix<TYPE>::RowPtr(unsigned r)
  85. // Return a row vector pointer
  86. {
  87.   return VecPtr<TYPE>(data + r*ncols, 1);
  88. }
  89.  
  90. template<class TYPE>
  91. INLINE VecPtr<TYPE> SimpleMatrix<TYPE>::ColPtr(unsigned c)
  92. // Return a column vector pointer
  93. {
  94.   return VecPtr<TYPE>(data+c, nrows);
  95. }
  96.  
  97. template<class TYPE>
  98. INLINE VecPtr<TYPE> SimpleMatrix<TYPE>::DiagPtr()
  99. // Return a diagonal vector pointer. In case matrix
  100. // isn't square, the smallest dimension is used to
  101. // determine the diagonal length.
  102. {
  103.   unsigned dstride = ((nrows > ncols) ? ncols : nrows) + 1;
  104.   return VecPtr<TYPE>(data, dstride);
  105. }
  106.  
  107.  
  108. template<class TYPE>
  109. INLINE VecPtr<TYPE> SimpleMatrix<TYPE>::PtrToAll()
  110. // Return pointer to all elements of the matrix
  111. {
  112.   return VecPtr<TYPE>(data, 1);
  113. }
  114.  
  115.  
  116. // Selectors for const matrices
  117.  
  118. #ifndef VECTORPTRRETURNS
  119. template<class TYPE>
  120. INLINE const TYPE *SimpleMatrix<TYPE>::operator[](unsigned r) const
  121. // Returns an ordinary pointer (stride 1) to row r
  122. {
  123.   return data + r*ncols;
  124. }
  125. #else
  126. template<class TYPE>
  127. INLINE VecPtr<const TYPE> SimpleMatrix<TYPE>::operator[](unsigned r) const
  128. // Returns an vector pointer (stride 1) to row r
  129. {
  130.   return VecPtr<const TYPE>(data + r*ncols);
  131. }
  132. #endif
  133.  
  134. template<class TYPE>
  135. INLINE VecPtr<const TYPE> SimpleMatrix<TYPE>::RowPtr(unsigned r) const
  136. // Return a row vector pointer
  137. {
  138.   return VecPtr<const TYPE>(data + r*ncols, 1);
  139. }
  140.  
  141. template<class TYPE>
  142. INLINE VecPtr<const TYPE> SimpleMatrix<TYPE>::ColPtr(unsigned c) const
  143. // Return a column vector pointer
  144. {
  145.   return VecPtr<const TYPE>(data+c, nrows);
  146. }
  147.  
  148. template<class TYPE>
  149. INLINE VecPtr<const TYPE> SimpleMatrix<TYPE>::DiagPtr() const
  150. // Return a diagonal vector pointer. In case matrix
  151. // isn't square, the smallest dimension is used to
  152. // determine the diagonal length.
  153. {
  154.   unsigned dstride = ((nrows > ncols) ? ncols : nrows) + 1;
  155.   return VecPtr<const TYPE>(data, dstride);
  156. }
  157.  
  158.  
  159. template<class TYPE>
  160. INLINE VecPtr<const TYPE> SimpleMatrix<TYPE>::PtrToAll() const
  161. // Return pointer to all elements of the matrix
  162. {
  163.   return VecPtr<const TYPE>(data, 1);
  164. }
  165.  
  166. template<class TYPE>
  167. INLINE unsigned SimpleMatrix<TYPE>::NRows() const
  168. {
  169.   return nrows;
  170. }
  171.  
  172. template<class TYPE>
  173. INLINE unsigned SimpleMatrix<TYPE>::NCols() const
  174. {
  175.   return ncols;
  176. }
  177.  
  178. template<class TYPE>
  179. INLINE unsigned SimpleMatrix<TYPE>::NElems() const
  180. {
  181.   return nrows * ncols;
  182. }
  183.  
  184. #undef INLINE
  185.  
  186. // Whether or not we should include the non-line methods for
  187. // our class templates here is implementation dependent.
  188.  
  189. #include "simpmat.mth"
  190.  
  191. #endif
  192.