#include "transcache.h"

transcache::transcache()
{
  getsuccess = gettry = 0;
}

transcache::~transcache()
{
}

int transcache::add( transformation &a )
{
  tctransform tc;
  tc.x = a.transx;
  tc.y = a.transy;
  data[tc] = a.score;
  return 0;
}

int transcache::get( transformation &a )
{
  std::map< tctransform, double, tctlt >::iterator i;
  tctransform tc;
  tc.x = a.transx;
  tc.y = a.transy;
  i = data.find( tc );
  gettry++;
  if( i == data.end() )
    return -1;
  else
    a.score = data[tc];
  getsuccess++;
  return 0;
}

double transcache::hitfrequency()
{
  if( gettry == 0 )
    return 0;
  return ( (double)getsuccess )/( (double)gettry );
}

int transcache::resetfreq()
{
  getsuccess = gettry = 0;
  return 0;
}

int transcache::getkeys( std::set< std::pair< double, double > > &s )
{
  std::map< tctransform, double, tctlt >::iterator i;
  std::pair< double, double > j;
  for( i = data.begin(); i != data.end(); i++ )
    {
      j.first = i->first.x;
      j.second = i->first.y;
      s.insert( j );
    }
  return 0;
}
