home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / simpmat.mth < prev    next >
Encoding:
Text File  |  1995-05-19  |  1.6 KB  |  57 lines

  1. /////////////////////////////////////////////////////////////////
  2. // simpmat.mth: Class methods for simple dynamic matrices.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. /////////////////////////////////////////////////////////////////
  5.  
  6. template<class TYPE>
  7. SimpleMatrix<TYPE>::SimpleMatrix(unsigned nr, unsigned nc)
  8. // General constructor
  9. {
  10.   nrows = nr; ncols = nc;
  11.   data = new TYPE[nrows*ncols];
  12. }
  13.  
  14. template<class TYPE>
  15. SimpleMatrix<TYPE>::SimpleMatrix(const SimpleMatrix<TYPE> &m)
  16. // Copy constructor
  17. {
  18.   nrows = m.nrows; ncols = m.ncols;
  19.   data = new TYPE[nrows*ncols];
  20.   Copy(m);
  21. }
  22.  
  23. template<class TYPE>
  24. void SimpleMatrix<TYPE>::Copy(const SimpleMatrix<TYPE> &m)
  25. // Does a two-dimensional copy from m to this matrix,
  26. // truncating if necessary. The logical size of this
  27. // matrix may become smaller, but never larger.
  28. {
  29.   // sc, tc = column vector pointers
  30.   VecPtr<const TYPE> sc(m.data, m.ncols);
  31.   VecPtr<TYPE> tc(data, ncols);
  32.  
  33.   // sr, tr = row vector pointers
  34.   VecPtr<const TYPE> sr(m.data, 1);
  35.   VecPtr<TYPE> tr(data, 1);
  36.  
  37.   unsigned nr = nrows;
  38.   if (nr > m.nrows) nr = m.nrows;
  39.   unsigned nc = ncols;
  40.   if (nc > m.ncols) nc = m.ncols;
  41.  
  42.   for (unsigned i = 0; i<nr; i++) {
  43.       // Note that only pointers are copied below, not the strides!
  44.       tr = tc; // Point to current row of t
  45.       sr = sc; // Point to current row of s
  46.       for (unsigned j = 0; j<nc; j++) {
  47.           *tr = *sr;
  48.           tr++; // Next column
  49.           sr++; // Ditto
  50.       }
  51.       tc++; // Next row
  52.       sc++; // Ditto
  53.   }
  54.   nrows = nr; // New logical size of matrix, may be smaller
  55.   ncols = nc;
  56. }
  57.