#include "variable.hpp"
#include <iostream>

std::map<std::string, Variable*> Variable::var_map;
unsigned long Variable::counter = 1;

Variable::Variable(const std::string name)
    : var_name(name), 
      preference(true), not_preference(false),
      true_cost(0), false_cost(0),
      true_literal(NULL), false_literal(NULL)
{
    counter++;
}

Variable::~Variable(void)
{
}

void Variable::assign_prefered_value()
{
    if(true_literal)
	true_literal->value = preference;
    if(false_literal)
	false_literal->value = !preference;
}

void Variable::assign_not_prefered_value()
{
    if(true_literal)
	true_literal->value = !preference;
    if(false_literal)
	false_literal->value = preference;
}

void Variable::propagate(unsigned long assigner_id, bool b)
{
    if(true_literal)
	true_literal->propagate(assigner_id, b);
    
    if(false_literal)
	false_literal->propagate(assigner_id, !b);
}

Variable* Variable::get_variable(const std::string name)
{
    // return Variable* for the given name.
    // This is for returning the same Variable* for the same given name.
    // 
    // Search the name in the var_map.
    // If it's already there, return it.
    //
    // If it's not in the var_map.
    // Make it, add it.
    std::map<std::string, Variable*>::iterator iter = var_map.find(name);
    if(iter != var_map.end()) {
	return iter->second;
    }
    else {
	Variable *new_one = new Variable(name);
	var_map.insert(std::map<std::string, Variable*>::value_type(name, new_one));
	return new_one;
    }
}
