/*******************************************************************************
 * Copyright (c) 2015-2016 ONERA.
 * Ce logiciel est la propriété de l’ONERA (the French Aerospace Lab).
 * Tous droits réservés.
 *
 * Ce programme et les éléments qui l'accompagnent sont mis à disposition
 * aux conditions définies par le contrat de licence logiciel CeCILL-C soumise
 * au droit français et respectant les principes de diffusion des logiciels libres.
 * Vous pouvez utiliser, modifier et/ou redistribuer ce programme
 * sous les conditions de la licence CeCILL-C (http://www.cecill.info).

 * This software is property of ONERA (the French Aerospace Lab).
 * All rights reserved.
 *
 * This program and the accompanying materials are made available under
 * the terms of the CeCILL-C license under French law and
 * abiding by the rules of distribution of free software.
 * You can  use, modify and/ or redistribute the software under the terms of
 * the CeCILL-C license (http://www.cecill.info).
 *
 * Contributeurs/contributors:
 *     Guillaume Infantes (ONERA - Centre de Toulouse) - initial API and implementation
 *
 *******************************************************************************/

#include "EnumDomain.hh"
#include "Values.hh"
#include <stdlib.h>

namespace epoch {
    namespace altarica {

	EnumDomain::EnumDomain() {
	    indexes = NULL;
	}

	EnumDomain::EnumDomain(int nSymbols) {
	    symbols.reserve(nSymbols);
	    arity_ = nSymbols;
	}
	
	void EnumDomain::addSymbol(std::string s) {
	    //	    symbols.push_back(s);
	    std::vector<std::string>::iterator i = symbols.begin();
	    for (; i!= symbols.end() && s.compare(*i) > 0 ; ++i);
	    symbols.insert(i, s);
	    arity_ = symbols.size();	    
	}

	void EnumDomain::addSymbols(std::vector<std::string> *sv) {
	    for (std::vector<std::string>::iterator svit = sv->begin();
		 svit != sv->end(); ++svit)
		addSymbol(*svit);
	}
	
	unsigned int EnumDomain::index(std::string arg) const {
	    // if (indexes == NULL) {
	    // 	indexes = new std::map<std::string, int>();
	    // 	int j = 0;
	    // 	for (std::vector<std::string>::const_iterator i = symbols.begin();i!=symbols.end(); ++i)
	    // 	    (*indexes)[*i] = j++;
	    // }
	    // return (*indexes)[arg];
	    unsigned int i=0;
	    for (std::vector<std::string>::const_iterator sit = symbols.begin(); sit!=symbols.end(); ++sit) {
	    	if (sit->compare(arg) == 0)
	    	    return i;
	    	i++;
	    }
	    return i;
	}

	std::string  EnumDomain::text(unsigned int index) const {
	    std::string  s;
	    s+=symbols[index];
	    return s;
	}


	Value * EnumDomain::toValue() const {
	    return new ValueSymbol(this);
	}
	
	int EnumDomain::getMin() const {
	    return 0;
	}

	 int EnumDomain::getMax() const {
	     return arity_-1;
	}

	// Value * EnumDomain::getFixedValue(int i) {
	//     return new FixedInt(i);
	// }

	Value * EnumDomain::getValue(int i) {
	    return new ValueInt(i);
	}


	const std::vector<std::string>* EnumDomain::getSymbols() const {
	    return &symbols;
	}

	std::string EnumDomain::toString() const {
	    std::string s;
	    s+= "{ ";
	    for (std::vector<std::string>::const_iterator i = symbols.begin(); i!= symbols.end(); ++i) {
		s+=*i;
		s+=" ";
	    }
	    s+= "}";
	    return s;
	}
	


	bool EnumDomain::equals(const EnumDomain *d) const {
	    if (d == this)
		return true;
	    const std::vector<std::string> * os = d->getSymbols();
	    if (os->size() != symbols.size())
		return false;
	    for (std::vector<std::string>::const_iterator i = os->begin();
		 i!= os->end(); ++i) {
		bool isIn = false;
		for (std::vector<std::string>::const_iterator j = symbols.begin();
		     j!= symbols.end() && !isIn; ++j) {
		    if (j->compare(*i) == 0)
			isIn = true;
		}
		if (!isIn)
		    return false;
	    }
	    return true;
	}

	EnumDomain::~EnumDomain() {
	    symbols.clear();
	}

	bool EnumDomain::contains(const std::string s) const {
	    for (std::vector<std::string>::const_iterator i = symbols.begin(); i!= symbols.end(); ++i)
		if (s.compare(*i)==0)
		    return true;
	    return false;
	}


    }
}


