/*******************************************************************************
* 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 "ValueSymbol.hh"
#include <limits.h>
#include <sstream>
#include "../domains/PredefinedDomain.hh"
#include <boost/functional/hash.hpp>

using namespace epoch::altarica;


ValueSymbol::ValueSymbol(const std::string s) {
    s_.assign(1,s);
    d_ = NULL;
}

ValueSymbol::ValueSymbol(const std::string s, const EnumDomain *d) {
    s_.assign(1,s);
    d_ = d;    
}

ValueSymbol::ValueSymbol(const EnumDomain *d) {
    d_ = d;
    s_.assign(d->getSymbols()->begin(),d->getSymbols()->end());
}

ValueSymbol::ValueSymbol(const std::vector<std::string> &vs, const EnumDomain *d)  {
    d_ = d;
    s_.assign(vs.begin(),vs.end());
}


ValueSymbol * ValueSymbol::clone() const {
    ValueSymbol * nvs = new ValueSymbol(s_, d_);
    nvs->setConstrained(constrained_);
    return nvs;
}

bool ValueSymbol::stringIn(const std::string s, const std::vector<std::string> &sv) const {    
    int test;
    std::vector<std::string>::const_iterator end = sv.end();
    for (std::vector<std::string>::const_iterator i = sv.begin(); i!=end; ++i) {
	test = s.compare(*i);
	if (test == 0)
	    return true;
	if (test <0)
	    return false;
    }
    return false;
}

void ValueSymbol::inverse() {
    std::vector<std::string> new_s;
    
    for (std::vector<std::string>::const_iterator i = d_->getSymbols()->begin(); i != d_->getSymbols()->end(); ++i) {
	if (!stringIn(*i, s_))
	    new_s.push_back(*i);	
    }
    s_.assign(new_s.begin(), new_s.end());
}



std::string ValueSymbol::toSymbol() const {
    if (s_.size() != 1)
	return NULL;
    return (s_[0]);
}

int ValueSymbol::toHash() const {
    int h = 0;    
    for (std::vector<std::string>::const_iterator i = s_.begin(); i!= s_.end(); ++i) 
	h+= 1<<d_->index(*i);
    return h;
}



bool ValueSymbol::equals(const ValueSymbol*v) const {
    if (s_.size() != v->toSymbols()->size())
      	return false;
    for (std::vector<std::string>::const_iterator i = s_.begin(); i!=s_.end();++i) {
     	bool found = false;
     	for (std::vector<std::string>::const_iterator j = v->toSymbols()->begin(); j!=v->toSymbols()->end()  && !found; ++j) {
     	    if ((*i).compare(*j) == 0)
    		found = true;
    	}
    	if (!found)
    	    return false;	
    }
    return true;
    //    return (includes(v) && v->includes(this));
}

void ValueSymbol::merge(const ValueSymbol*v) {
    if (d_ != NULL) 
	for (std::vector<std::string>::const_iterator vsi= v->toSymbols()->begin(); vsi!=v->toSymbols()->end(); ++vsi) {
	    if (d_->contains(*vsi))
		if (!includes(*vsi))
		    add(*vsi);
	}
    else 
	for (std::vector<std::string>::const_iterator vsi= v->toSymbols()->begin(); vsi!=v->toSymbols()->end(); ++vsi) 
	    if (!includes(*vsi))
		add(*vsi);	       
}

void ValueSymbol::add(std::string s) {
    std::vector<std::string>::iterator i = s_.begin();
    for (; i!= s_.end() && s.compare(*i) > 0 ; ++i);
    s_.insert(i, s);
	
}

bool ValueSymbol::includes(const ValueSymbol *s) const {
    // if (s_.size()<s->toSymbols()->size())
    // 	 return false;
    bool inc = true;
    for (std::vector<std::string>::const_iterator i = s->toSymbols()->begin();
	 i != s->toSymbols()->end() && inc; ++i) {
	if (!includes(*i))
	    inc = false;
    }
    return inc;
}

bool ValueSymbol::includes(const std::string s) const {
    return stringIn(s,s_);
}

bool ValueSymbol::contains(const ValueSymbol *fs) const {
    return includes(fs);
}

	    
// Value* ValueSymbol::intersectValue(Value* v) const {
//     v->intersectValue(this);
//     return v;
// }


void ValueSymbol::intersectValue(const ValueSymbol* v) {    
    if (s_.size() == 0) {
	return;
	// for (std::vector<std::string>::const_iterator i = v->toSymbols()->begin(); 
	//      i != v->toSymbols()->end();  ++i) 
	    //	    s_.push_back(*i);
	// s_.assign(v->toSymbols()->begin(),v->toSymbols()->end());
    } else {
	for (std::vector<std::string>::iterator i = s_.begin(); i!= s_.end(); )
	    if (v->includes(*i))
		++i;
	    else
		i = s_.erase(i);
    }
}


const EnumDomain * ValueSymbol::getDomain() const {
    return d_;
}


std::string ValueSymbol::toString() const {
  return toString(true);  
}



std::string ValueSymbol::toString(bool humanreadable) const {
    std::string s;
    if (humanreadable)
      s+= "{ ";
    for (std::vector<std::string>::const_iterator i = s_.begin(); i!= s_.end(); ++i) {      
      s+= *i;
      if (humanreadable) s+= "  ";
    }
    if (humanreadable) s+= " }";
    return s;
}

ValueSymbol::~ValueSymbol() {
    //    s_.clear();
}

void ValueSymbol::setDomain (const Domain *d) {
    d_ = (EnumDomain*) d;
}

std::vector<Value*> * ValueSymbol::getSingletons() const {
    std::vector<Value*> *ret = new std::vector<Value*>();
    for (std::vector<std::string>::const_iterator i = s_.begin(); i!= s_.end(); ++i) {
	Value *fv = new ValueSymbol(*i);
	fv->own();
	ret->push_back(fv);
    }
    return ret;
}

bool ValueSymbol::hasIntersection(const ValueSymbol*v) const {
    const std::vector<std::string>* os = v->toSymbols();
    for (std::vector<std::string>::const_iterator i = os->begin(); i!= os->end(); ++i)
	if (includes(*i))
	    return true;
    return false;
}

bool ValueSymbol::less(const ValueSymbol *v) const {
    return toString().compare(v->toString()) < 0;
}

void ValueSymbol::removeValue(const ValueSymbol *v) {
    const std::vector<std::string>* symb = v->toSymbols();
    for (std::vector<std::string>::const_iterator i = symb->begin(); i!=symb->end(); ++i) {
	removeSymbol(*i);
    }
}

void ValueSymbol::removeSymbol(std::string s) {
    std::vector<std::string>::iterator i;
    for (i = s_.begin(); i!= s_.end() && s.compare(*i) != 0; ++i);
    if (i!= s_.end())
	s_.erase(i);
}


bool ValueSymbol::pDifferent(const ValueSymbol *v) const {
    // if (this ==v)
    // 	return false;
    const std::vector<std::string>* os = v->toSymbols();
    unsigned int size = s_.size();
    if (size != os->size())
	return true;
    for (unsigned int i = 0; i<size; ++i)
	if (s_[i].compare((*os)[i]) != 0)
	    return true;
    return false;
    // int nFound = 0;
    // std::vector<std::string>::const_iterator startJ =  v->toSymbols()->begin();
    // std::vector<std::string>::const_iterator endJ =  v->toSymbols()->end();
    // std::vector<std::string>::const_iterator j;
    
    // for (std::vector<std::string>::const_iterator i = s_.begin(); i!=s_.end();++i) {
    // 	bool found = false;
    // 	for (j  = startJ; j!= endJ && !found;++j) {
    // 	    if (i->compare(*j) == 0) {
    // 		found = true;
    // 		nFound++;
    // 		startJ++; //symbols are alphabetically sorted
    // 	    }
    // 	}
    // 	if (!found)
    // 	    return true;    	
    // }
    // return (v->toSymbols()->size() != nFound);
    
    // for (std::vector<std::string>::const_iterator i = s_.begin(); i!= s_.end(); ++i)
    // 	if (!v->includes(*i))
    // 	    return true;
    // for (std::vector<std::string>::const_iterator i = v->toSymbols()->begin(); i!= v->toSymbols()->end(); ++i)
    // 	if (!includes(*i))
    // 	    return true;
    // return false;
}

ValueSymbol * ValueSymbol::getComplement() const {
    if (d_ == NULL)
	return NULL;
    ValueSymbol * nvs = new ValueSymbol(d_);
    nvs->removeValue(this);
    return nvs;
}


