#include "InstantiationId.h"

namespace cosi {

namespace rules {

namespace onchipcommunication {

InstantiationId::InstantiationId(int Nip,
		std::vector< InstallationSite > InstallationSites,
		std::vector<Name> IdType) {
	mInstallationSites = InstallationSites ;
	mNip = Nip ;
	mIdType = IdType ;
	//build the inverse map
	for (unsigned int i = 0; i< mInstallationSites.size() ; i++) {
		mId[mInstallationSites[i].GetPosition()] = i ;
	}
	for( unsigned int i = 0  ; i < IdType.size() ; i++ ){
		mTypeId[IdType[i]] = i ;
	}

}

InstantiationId::~InstantiationId() {
}

int InstantiationId::Id(Name pName, Position pP) {
	//get the index of the component
	if (mTypeId.find(pName) == mTypeId.end() ) {
		CosiLog << " Component "<< pName << " not present in component table "
				<< std::endl ;
		return -1;
	}
	int C = mTypeId[pName];
	if (mId.find(pP) == mId.end() ) {
		CosiLog << " Position "<< pP << " is not an installation site, return -1"
				<< std::endl ;
		return -1 ;
	} else {
		int P = mId[pP];
		return ( C*mId.size() + P ) ;
	}
}

Name InstantiationId::GetName(int V){
	int C = V/mId.size() ;
	return mIdType[C] ;
}

Position InstantiationId::GetPosition(int V){
	if ( V == -1 ) {
		return Position() ;
	}
	int P = V%mId.size() ;
	return mInstallationSites[P].GetPosition() ;
}

}

}

}
