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 >
Wrap
C/C++ Source or Header
|
1994-01-10
|
3KB
|
98 lines
//Copyright (C) Man Machine Interfaces 1994. All rights reserved.
#include "stdafx.h"
#include "wmatrix.h"
static CWordArray Safety ; //returned on range errors
//construct a WORD matrix with numCols x numRows
//initialize all cells to init
CWordMatrix::CWordMatrix(int numRows, int numCols, WORD init)
{
m_Rows.SetSize(0,1) ;
m_NumRows = numRows ;
m_NumCols = numCols ;
for(int row=0;row<numRows;row++) {
m_Rows.SetAtGrow(row,new CWordArray()) ; //each row is represented by a word array
for (int col=0;col<numCols;col++)
m_Rows[row].SetAtGrow(col,init) ;
}
}
CWordMatrix::CWordMatrix(const CWordMatrix &source)
{
m_Rows.SetSize(0,1) ;
m_NumRows = source.m_NumRows ;
m_NumCols = source.m_NumCols ;
for(int row=0;row<m_NumRows;row++) {
m_Rows.SetAtGrow(row,new CWordArray()) ; //each row is represented by a word array
for (int col=0;col<m_NumCols;col++)
m_Rows[row].SetAtGrow(col,source[row][col]) ;
}
}
CWordMatrix::~CWordMatrix()
{
int size = m_Rows.GetSize() ;
for (int i=0;i<size;i++)
delete m_Rows.CObArray::GetAt(i) ;
}
CWordMatrix & CWordMatrix::operator =(const CWordMatrix &source)
{
if (this != &source) { //check for assignment to self
for(int row=0;row<source.m_NumRows;row++) {
if (row > m_NumRows)
m_Rows.SetAtGrow(row,new CWordArray()) ; //source is bigger so add a row
for (int col=0;col<source.m_NumCols;col++)
m_Rows[row].SetAtGrow(col,source[row][col]) ;
}
m_NumRows = source.m_NumRows ;
m_NumCols = source.m_NumCols ;
}
return *this ;
}
//query
WORD CWordMatrix::GetAt(int row, int col) const
{
if (row < m_NumRows && col <m_NumCols)
return m_Rows[row][col] ;
else
return 0; //error, you might want to raise an exception here!
}
const CWordArray & CWordMatrix::GetRow(int row) const
{
if (row < m_NumRows)
return m_Rows[row] ;
else
return Safety ; //error, you might want to raise an exception here!
}
//modify
CWordArray & CWordMatrix::operator [](int row)
{
if (row < m_NumRows)
return m_Rows[row] ;
else
return Safety ; //error, you might want to raise an exception or grow the matrix
}
void CWordMatrix::SetAt(int row, int col, WORD val)
{
m_Rows[row][col] = val ;
}
BOOL CWordMatrix::Find(WORD item, int &row, int &col)
{
for (row = 0; row < m_NumRows ; row++)
for (col = 0; col < m_NumCols ; col++)
if (m_Rows[row][col] == item)
return TRUE ;
return FALSE ;
}