//
// C++ Interface: ChainGenerator
//
// Description:
//
// Author: Alessandro Pinto <apinto@eecs.berkeley.edu>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <iostream>
#include <iterator>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include "float.h"
#include "networks/wiredbas/WiredBasSpecificaion.h"
#include "networks/wiredbas/WiredBasPlatformInstance.h"
#include "networks/wiredbas/WiredBasImplementation.h"
#include "networks/graph/RealWeightedGraph.h"
#include "platforms/wiredbas/BusPlatform.h"
#include "algorithms/graph/Tsp.h"

#ifndef _CHAINGENERATOR_H_
#define _CHAINGENERATOR_H_

namespace cosi {

namespace algorithms {

namespace wiredbas {

class ChainGenerator {
public:

	ChainGenerator();

	void Initialize(cosi::commstruct::wiredbas::Specification* pSpec,
			cosi::platforms::wiredbas::BusPlatform* pPlt);

	void Run();

	std::list< cosi::commstruct::wiredbas::Implementation* > GetChains();
	std::set<int> GetRouters() ;

private:

	cosi::commstruct::wiredbas::Implementation* GenerateCompleteWiring();

	cosi::commstruct::graph::U_RealWeightedGraph* GenerateCompleteDistance(
			cosi::commstruct::wiredbas::Implementation* pW);

	void AddRouter(int Id , Position pP, cosi::commstruct::wiredbas::Implementation* pW,
			cosi::commstruct::graph::U_RealWeightedGraph* pD);

	std::list<int> FindTour(cosi::commstruct::graph::U_RealWeightedGraph* pD);

	void GenerateChains(std::list<int> pTour, int pRouterId,Position P,
			cosi::commstruct::wiredbas::Implementation* pW);
	void RemoveRouter(int pRouterId,
			cosi::commstruct::wiredbas::Implementation* pW);
	void RemoveRouter(int pRouterId,
			cosi::commstruct::graph::U_RealWeightedGraph* pD);

	cosi::commstruct::wiredbas::Implementation* GenerateBusInstance(std::list<int> pSubTour, int RouterId, Position P , cosi::commstruct::wiredbas::Implementation* pW);

	bool CheckLatency(cosi::commstruct::wiredbas::Implementation* pBus );

	cosi::commstruct::wiredbas::Specification* mSpecification;

	cosi::platforms::wiredbas::BusPlatform* mPlt;

	cosi::commstruct::wiredbas::Implementation* mTour;

	std::list< cosi::commstruct::wiredbas::Implementation* > mChains;

	std::set<int> mRouters ;

};

}
}
}
#endif
