home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////
- // simpmat.mth: Class methods for simple dynamic matrices.
- // Copyright(c) 1993 Azarona Software. All rights reserved.
- /////////////////////////////////////////////////////////////////
-
- template<class TYPE>
- SimpleMatrix<TYPE>::SimpleMatrix(unsigned nr, unsigned nc)
- // General constructor
- {
- nrows = nr; ncols = nc;
- data = new TYPE[nrows*ncols];
- }
-
- template<class TYPE>
- SimpleMatrix<TYPE>::SimpleMatrix(const SimpleMatrix<TYPE> &m)
- // Copy constructor
- {
- nrows = m.nrows; ncols = m.ncols;
- data = new TYPE[nrows*ncols];
- Copy(m);
- }
-
- template<class TYPE>
- void SimpleMatrix<TYPE>::Copy(const SimpleMatrix<TYPE> &m)
- // Does a two-dimensional copy from m to this matrix,
- // truncating if necessary. The logical size of this
- // matrix may become smaller, but never larger.
- {
- // sc, tc = column vector pointers
- VecPtr<const TYPE> sc(m.data, m.ncols);
- VecPtr<TYPE> tc(data, ncols);
-
- // sr, tr = row vector pointers
- VecPtr<const TYPE> sr(m.data, 1);
- VecPtr<TYPE> tr(data, 1);
-
- unsigned nr = nrows;
- if (nr > m.nrows) nr = m.nrows;
- unsigned nc = ncols;
- if (nc > m.ncols) nc = m.ncols;
-
- for (unsigned i = 0; i<nr; i++) {
- // Note that only pointers are copied below, not the strides!
- tr = tc; // Point to current row of t
- sr = sc; // Point to current row of s
- for (unsigned j = 0; j<nc; j++) {
- *tr = *sr;
- tr++; // Next column
- sr++; // Ditto
- }
- tc++; // Next row
- sc++; // Ditto
- }
- nrows = nr; // New logical size of matrix, may be smaller
- ncols = nc;
- }
-