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

/**
 * @file EquationMap.h
 * EquationMap class header.
 */
#ifndef PowerFlower_EquationMap_h
#define PowerFlower_EquationMap_h

#include "PowerFlower.def"

POWERFLOWER_NAMESPACE_BEGIN

/**
 * Equation map class.
 * Implements a mapping from power system bus index to power flow problem equation index.
 */
class EquationMap
{

public:
	// TYPES
	typedef unsigned int IndexType;		///< Type of bus and equation indices.

	// LIFECYCLE
	EquationMap(void);
	~EquationMap(void);

	// OPERATIONS

	/**
	 * Clears the equation map object.
	 * Clears the map data and resets the finalization status to \c false.
	 */
	void Clear(void);

	/**
	 * Initialized the equation map to a given number of busses.
	 * @param busCount number of busses.
	 * @return \c true on success, \c false otherwise.
	 */
	bool Initialize(IndexType busCount);

	/**
	 * Sets a given bus to be a slack bus.
	 * @param busIndex index of the slack bus.
	 * @return \c true on success, \c false otherwise.
	 */
	bool SetSlackBus(IndexType busIndex);

	/**
	 * Sets a given bus to be a generator bus.
	 * @param busIndex index of the generator bus.
	 * @return \c true on success, \c false otherwise.
	 */
	bool SetGeneratorBus(IndexType busIndex);

	/**
	 * Sets a given bus to be a load bus.
	 * @param busIndex index of the load bus.
	 * @return \c true on success, \c false otherwise.
	 */
	bool SetLoadBus(IndexType busIndex);

	/**
	 * Finalizes the equation map.
	 * Checks whether all busses have been properly set to be either a slack, generator, or load bus,
	 * and transforms the equation map data to the finalized format.
	 * @note No changes can be made after finalization. Instead, Clear() can be called to reset the data,
	 * after which all the busses have to be set to their proper types again.
	 * @return \c true on success, \c false otherwise.
	 */
	bool Finalize(void);

	// ACCESS

	/**
	 * Checks if the equation map is a valid finalized map.
	 * @return \c true if the map is valid, \c false otherwise.
	 */
	bool IsValid(void) const;

	/**
	 * Gets the number of busses in the equation map.
	 * @return number of busses.
	 */
	IndexType GetBusCount(void) const;

	/**
	 * Gets the total number of equations the busses map to.
	 * @note Should only be called on a valid finalized map.
	 * @return total number of equations.
	 */
	IndexType GetEquationCount(void) const;

	/**
	 * Gets the number of equations a given bus maps to.
	 * @note Should only be called on a valid finalized map.
	 * @param busIndex index of the bus to get the number of equations for.
	 * @return number of equations.
	 */
	IndexType GetEquationCount(IndexType busIndex) const;

	/**
	 * Gets the equation index of the first equation a given bus maps to.
	 * @note Should only be called on a valid finalized map.
	 * @param busIndex index of the bus to get the equation index for.
	 * @return equation index.
	 */
	IndexType GetEquationIndex(IndexType busIndex) const;

	/**
	 * Gets a pointer to the equation map array.
	 * @note Should only be called on a valid finalized map.
	 * @return pointer to the equation map array.
	 */
	const IndexType* GetArray(void) const;

private:
	bool mIsFinalized;		///< \c true if the equation map is finalized, \c false otherwise.
	IndexType mBusCount;	///< Number of busses in de power system.
	IndexType* mpMap;		///< Mapping array of size mBusCount+1.
};

POWERFLOWER_NAMESPACE_END

#endif // PowerFlower_EquationMap_h
