#include "Matrix.h"

using namespace MATRIX;

MatrixManager::iterator MatrixManager::begin(void)
{
	return this->matrices.begin();
}

MatrixManager::const_iterator MatrixManager::begin(void) const
{
	return this->matrices.begin();
}

MatrixManager::iterator MatrixManager::end(void)
{
	return this->matrices.end();
}

MatrixManager::const_iterator MatrixManager::end(void) const
{
	return this->matrices.end();
}

MatrixManager::MatrixManager(void)
{
}

MatrixManager::~MatrixManager(void)
{
	for ( iterator it= this->matrices.begin(); it!= this->matrices.end(); ++it)
	{
		delete (it->second);
	}
}

Matrix4X4<float>* MatrixManager::getMatrix( const string &matrixName)
{
	iterator it =this->matrices.find( matrixName);

	if ( it != this->matrices.end() )
	{
		return it->second;
	}
	
	return NULL;
}

Matrix4X4<float>* MatrixManager::insertMatrix( const string& matrixName, Matrix4X4<float>* matrixPtr)
{
	iterator it =this->matrices.find( matrixName);

	if ( it != this->matrices.end() )
	{
		return NULL;	//matrix with given name already exists in database
	}

	this->matrices.insert( make_pair( matrixName, matrixPtr) );
	return matrixPtr;
}

MatrixManager& MatrixManager::operator=( const MatrixManager &matrixManager )
{
	for ( iterator it= this->matrices.begin(); it!= this->matrices.end(); ++it)
	{
		delete (it->second);
	}
	this->matrices.clear();

	for ( MatrixManager::const_iterator it=matrixManager.begin(); it!= matrixManager.end(); ++it)
	{
		Matrix4X4<float> *newMatrix = new Matrix4X4<float>( *it->second );
		this->matrices.insert( make_pair( it->first, newMatrix) );
	}

	return *this;
}

MatrixManager::MatrixManager( const MatrixManager &matrixManager )
{
	*this = matrixManager;
}
