/*
 * Copyright (C) 2008-2012 Delft University of Technology - All rights reserved.
 *
 * Author: Reijer Idema
 */

/*
 * EquationMap class source.
 */
#include "EquationMap.h"


POWERFLOWER_NAMESPACE_BEGIN


/////////////////////////////// PUBLIC ///////////////////////////////////////


//============================= LIFECYCLE ====================================


EquationMap::EquationMap(void) : mIsFinalized(false), mBusCount(0), mpMap(0)
{ } // EquationMap::EquationMap


EquationMap::~EquationMap(void)
{
	Clear();
} // EquationMap::~EquationMap


//============================= OPERATIONS ===================================


void EquationMap::Clear(void)
{
	mIsFinalized = false;
	mBusCount = 0;
	delete[] mpMap;
	mpMap = 0;
} // EquationMap::Clear


bool EquationMap::Initialize(IndexType busCount)
{
	if (mIsFinalized) return false;		// No changing of a finished map!
	if (busCount <= 0) return false;
	mBusCount = busCount;
	delete mpMap;
	mpMap = new IndexType[mBusCount+1];
	for (IndexType i=0; i<=mBusCount; ++i) mpMap[i] = -1;
	return true;
} // EquationMap::Initialize


bool EquationMap::SetSlackBus(IndexType busIndex)
{
	if (mIsFinalized) return false;		// No changing of a finished map!
	if (busIndex < 0 || busIndex >= mBusCount) return false;
	mpMap[busIndex+1] = 0;
	return true;
} // EquationMap::SetSlackBus


bool EquationMap::SetGeneratorBus(IndexType busIndex)
{
	if (mIsFinalized) return false;		// No changing of a finished map!
	if (busIndex < 0 || busIndex >= mBusCount) return false;
	mpMap[busIndex+1] = 1;
	return true;
} // EquationMap::SetGeneratorBus


bool EquationMap::SetLoadBus(IndexType busIndex)
{
	if (mIsFinalized) return false;		// No changing of a finished map!
	if (busIndex < 0 || busIndex >= mBusCount) return false;
	mpMap[busIndex+1] = 2;
	return true;
} // EquationMap::SetLoadBus


bool EquationMap::Finalize(void)
{
	if (mIsFinalized) return false;		// No changing of a finished map!
	if (mBusCount == 0) return false;
	mpMap[0] = 0;
	for (IndexType i=1; i<=mBusCount; ++i)
	{
		if (mpMap[i] < 0 || mpMap[i] > 2) return false;
		mpMap[i] += mpMap[i-1];
	}
	mIsFinalized = true;
	return true;
} // EquationMap::Finalize


//============================= ACCESS     ===================================


bool EquationMap::IsValid(void) const
{
	return mIsFinalized;
} // EquationMap::IsValid


EquationMap::IndexType EquationMap::GetBusCount(void) const
{
	return mBusCount;
} // EquationMap::GetBusCount


EquationMap::IndexType EquationMap::GetEquationCount(void) const
{
	return mpMap[mBusCount];
} // EquationMap::GetEquationCount


EquationMap::IndexType EquationMap::GetEquationCount(IndexType busIndex) const
{
	return mpMap[busIndex+1] - mpMap[busIndex];
} // EquationMap::GetEquationCount


EquationMap::IndexType EquationMap::GetEquationIndex(IndexType busIndex) const
{
	return mpMap[busIndex];
} // EquationMap::GetEquationIndex


const EquationMap::IndexType* EquationMap::GetArray(void) const
{
	return mpMap;
} // EquationMap::GetArray


/////////////////////////////// PRIVATE    ///////////////////////////////////


POWERFLOWER_NAMESPACE_END
