inline void
CertificateCacheTtl::remove(const Name& certificateName)
{
  Name name = certificateName.getPrefix(-1);
  Cache::iterator it = m_cache.find(name);
  if (it != m_cache.end())
    m_cache.erase(it);
}
inline shared_ptr<const IdentityCertificate>
CertificateCacheTtl::getCertificate(const Name& certificateName)
{
  Cache::iterator it = m_cache.find(certificateName);
  if (it != m_cache.end())
    return it->second.first;
  else
    return shared_ptr<IdentityCertificate>();
}
inline void
CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
{
  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
                               certificate->getFreshnessPeriod() : m_defaultTtl);

  Name index = certificate->getName().getPrefix(-1);

  Cache::iterator it = m_cache.find(index);
  if (it != m_cache.end())
    m_scheduler.cancelEvent(it->second.second);

  EventId eventId = m_scheduler.scheduleEvent(expire,
                                              bind(&CertificateCacheTtl::remove,
                                                   this, certificate->getName()));

  m_cache[index] = std::make_pair(certificate, eventId);
}
Example #4
0
double player_move(const Grid& grid, Cache& cache,int depth)
{
	if (depth == 0) // End of branch
	{		
		return has_move(grid) ? evaluate_heuristic(grid) : 0; // has_move(grid) Penalizes dead end
	}

	double best_score = 0;

	for(int direction = 0; direction < 4; direction++){
		Grid computer_grid = grid;
		move(computer_grid, direction);

		if (computer_grid == grid)  // No change due to movement
		{
			continue; // Skip to next direction
		}
		
		double computer_score = 0;

		// Pruning
		Cache::const_iterator iter = cache.find(computer_grid);
		if (iter != cache.end())
		{
			computer_score = iter->second;
		}
		else
		{
			computer_score = computer_move(computer_grid, depth - 1);
			cache[computer_grid] = computer_score;
		}

		if (computer_score > best_score){
			best_score = computer_score;
		}
	}
		
	return best_score;
}