home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / APR94_1.ZIP / GA.ZIP / SOURCE.ZIP / WMATRIX.CPP < prev    next >
C/C++ Source or Header  |  1994-01-10  |  3KB  |  98 lines

  1. //Copyright (C) Man Machine Interfaces 1994. All rights reserved.
  2.  
  3. #include "stdafx.h"
  4. #include "wmatrix.h"
  5.  
  6. static CWordArray Safety ;    //returned on range errors
  7.  
  8. //construct a WORD matrix with numCols x numRows
  9. //initialize all cells to init
  10. CWordMatrix::CWordMatrix(int numRows, int numCols, WORD init) 
  11. {                      
  12.     m_Rows.SetSize(0,1) ;
  13.     m_NumRows = numRows ;
  14.     m_NumCols = numCols ;                    
  15.     for(int row=0;row<numRows;row++) {
  16.         m_Rows.SetAtGrow(row,new CWordArray()) ;     //each row is represented by a word array
  17.         for (int col=0;col<numCols;col++) 
  18.             m_Rows[row].SetAtGrow(col,init) ;
  19.         }
  20. }
  21.  
  22. CWordMatrix::CWordMatrix(const CWordMatrix &source) 
  23. {
  24.     m_Rows.SetSize(0,1) ;
  25.     m_NumRows = source.m_NumRows ;
  26.     m_NumCols = source.m_NumCols ;
  27.     for(int row=0;row<m_NumRows;row++) {
  28.         m_Rows.SetAtGrow(row,new CWordArray()) ;     //each row is represented by a word array
  29.         for (int col=0;col<m_NumCols;col++) 
  30.             m_Rows[row].SetAtGrow(col,source[row][col]) ;
  31.         }
  32.     
  33. }
  34.  
  35. CWordMatrix::~CWordMatrix() 
  36. {
  37.     int size = m_Rows.GetSize() ;
  38.     for (int i=0;i<size;i++)
  39.         delete m_Rows.CObArray::GetAt(i) ;
  40. }
  41.  
  42.  
  43. CWordMatrix &  CWordMatrix::operator =(const CWordMatrix &source) 
  44. {
  45.     if (this != &source) { //check for assignment to self
  46.         for(int row=0;row<source.m_NumRows;row++) {
  47.             if (row >  m_NumRows)
  48.                 m_Rows.SetAtGrow(row,new CWordArray()) ;     //source is bigger so add a row
  49.             for (int col=0;col<source.m_NumCols;col++) 
  50.                 m_Rows[row].SetAtGrow(col,source[row][col]) ;
  51.             }
  52.         m_NumRows = source.m_NumRows ;
  53.         m_NumCols = source.m_NumCols ;
  54.         }             
  55.     return *this ;        
  56. }
  57.  
  58. //query
  59.  
  60. WORD CWordMatrix::GetAt(int row, int col) const 
  61. {  
  62.     if (row < m_NumRows && col <m_NumCols)
  63.         return m_Rows[row][col] ;
  64.     else
  65.         return 0; //error, you might want to raise an exception here!
  66. }
  67.  
  68. const CWordArray & CWordMatrix::GetRow(int row) const
  69.     if (row < m_NumRows)
  70.         return m_Rows[row] ;
  71.     else
  72.         return Safety ;    //error, you might want to raise an exception here!
  73. }
  74.  
  75. //modify
  76. CWordArray & CWordMatrix::operator [](int row) 
  77. {
  78.     if (row < m_NumRows)
  79.         return m_Rows[row] ;
  80.     else
  81.         return Safety ;    //error, you might want to raise an exception or grow the matrix
  82. }
  83.                                               
  84. void CWordMatrix::SetAt(int row, int col, WORD val) 
  85. {
  86.     m_Rows[row][col]  = val ;                                             
  87. }
  88.  
  89. BOOL CWordMatrix::Find(WORD item, int &row, int &col) 
  90. {
  91.     for (row = 0; row < m_NumRows ; row++)
  92.         for (col = 0; col < m_NumCols ; col++)
  93.             if (m_Rows[row][col] == item)
  94.                 return TRUE ;
  95.     return FALSE ;    
  96. }
  97.