//
// Copyright (c) 2009 Froduald Kabanza and the Université de Sherbrooke.
// Use of this software is permitted for non-commercial research purposes, and
// it may be copied or applied only for that use. All copies must include this
// copyright message.
// 
// This is a research prototype and it has not gone through intensive tests and
// is delivered as is. It may still contain bugs. Froduald Kabanza and the
// Université de Sherbrooke disclaim any responsibility for damage that may be
// caused by using it.
//
// Implementation: Simon Chamberland
// Project Manager: Froduald Kabanza
//

#include "stdafx.h"
#include "TreeSetFactsContainer.h"
#include "../../../Utils/Utils.h"

using namespace planning;

TreeSetFactsContainer::TreeSetFactsContainer(IntegerInterval)
  : m_hashCode(0)
{
}

TreeSetFactsContainer::TreeSetFactsContainer(const TreeSetFactsContainer &other)
  : m_facts(other.m_facts), m_hashCode(0)
{
}

TreeSetFactsContainer* TreeSetFactsContainer::copy() const
{
  return new TreeSetFactsContainer(*this);
}

bool TreeSetFactsContainer::isSet(FormulaId id) const
{
  return m_facts.find(id) != m_facts.end();
}

void TreeSetFactsContainer::set(FormulaId id)
{
  std::pair<std::set<FormulaId>::iterator, bool> p = m_facts.insert(id);
  if (p.second)
  {
    m_hashCode += m_uintHasher(id);
  }
}

void TreeSetFactsContainer::unset(FormulaId id)
{
  if (m_facts.erase(id))
  {
    m_hashCode -= m_uintHasher(id);
  }
}

size_t TreeSetFactsContainer::getHashCode() const
{
  return m_hashCode;
}

int TreeSetFactsContainer::compareTo(const FactsContainer& other) const
{
  TreeSetFactsContainer& container = (TreeSetFactsContainer&) other;

  COMPARE(m_hashCode, container.m_hashCode);

  COMPARE(m_facts.size(), container.m_facts.size());
  
  for (std::set<FormulaId>::const_iterator it1 = m_facts.begin(), it2 = container.m_facts.begin();
       it1 != m_facts.end();
       ++it1, ++it2)
  {
    COMPARE(*it1, *it2);
  }
  return 0;
}