/* Grid.cpp Written by Matthew Fisher */ #pragma once #include "Grid.h" template void Grid::Allocate(unsigned int Rows, unsigned int Cols) { _Rows = Rows; _Cols = Cols; if(_Data) { delete[] _Data; } _Data = new type[Rows * Cols]; } template void Grid::Clear(const type &T) { const unsigned int TotalEntries = _Rows * _Cols; for(unsigned int Index = 0; Index < TotalEntries; Index++) { _Data[Index] = T; } } template Vec2i Grid::MaxIndex() const { Vec2i maxIndex(0, 0); const type *maxValue = &_Data[0]; for(unsigned int rowIndex = 0; rowIndex < _Rows; rowIndex++) { for(unsigned int colIndex = 0; colIndex < _Cols; colIndex++) { const type *curValue = &_Data[rowIndex * _Cols + colIndex]; if(*curValue > *maxValue) { maxIndex = Vec2i(rowIndex, colIndex); maxValue = curValue; } } } return maxIndex; } template const type& Grid::MaxValue() const { Vec2i index = MaxIndex(); return _Data[index.x * _Cols + index.y]; } template Grid::Grid() { _Rows = 0; _Cols = 0; _Data = NULL; } template Grid::Grid(unsigned int Rows, unsigned int Cols) { _Rows = Rows; _Cols = Cols; _Data = new type[Rows * Cols]; } template Grid::Grid(const Grid &G) { _Rows = G._Rows; _Cols = G._Cols; const unsigned int TotalEntries = _Rows * _Cols; _Data = new type[TotalEntries]; for(unsigned int Index = 0; Index < TotalEntries; Index++) { _Data[Index] = G._Data[Index]; } } template Grid::~Grid() { FreeMemory(); } template void Grid::FreeMemory() { _Rows = 0; _Cols = 0; if(_Data != NULL) { delete[] _Data; _Data = NULL; } } template Grid& Grid::operator = (const Grid &G) { if(_Data) { delete[] _Data; } _Rows = G._Rows; _Cols = G._Cols; const unsigned int TotalEntries = _Rows * _Cols; _Data = new type[TotalEntries]; for(unsigned int Index = 0; Index < TotalEntries; Index++) { _Data[Index] = G._Data[Index]; } return *this; }