//
// 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 that 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.
//
// Please note that some files were inspired in part by the PDDL4J library:
// http://www.math-info.univ-paris5.fr/~pellier/software/software.php 
//
// Implementation: Simon Chamberland
// Project Manager: Froduald Kabanza
//

#include "stdafx.h"
#include "Type.h"
#include "../Object.h"

using namespace planning;

Type::Type()
  : m_isDomainCached(false)
{
}

Type::~Type()
{
}

//const std::vector<const Object*>& Type::getDomain() const
//{
//  assert(m_isDomainCached);
//
//  return m_cachedDomain;
//}

size_t Type::getDomainCardinality() const
{
  assert(m_isDomainCached);

  return m_cachedDomain.size();
}

void Type::cacheDomain()
{
  if (!m_isDomainCached)
  {
    // Retrieve the set of all objects
    stdext::hash_set<const Object*> allObjects;
    retrieveDomain(allObjects);

    // Fill the cached domain vector
    m_cachedDomain.assign(allObjects.begin(), allObjects.end());

    m_isDomainCached = true;
  }
}

void Type::clearObjects()
{
  m_cachedDomain.clear();
  m_isDomainCached = false;
}

void Type::preprocess()
{
  cacheDomain();

  // Assign IDs to all objects
  for (size_t i = 0; i < m_cachedDomain.size(); ++i)
  {
    m_cachedDomain[i]->setObjectId(this, i);
  }
}