2006-05-27 16:12:16 +00:00
|
|
|
/* $Id$ */
|
|
|
|
#ifndef YAPF_COSTCACHE_HPP
|
|
|
|
#define YAPF_COSTCACHE_HPP
|
|
|
|
|
|
|
|
|
|
|
|
/** CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements
|
2006-09-04 20:40:33 +00:00
|
|
|
* PfNodeCacheFetch() and PfNodeCacheFlush() callbacks. Used when nodes don't have CachedData
|
|
|
|
* defined (they don't count with any segment cost caching).
|
|
|
|
*/
|
2006-05-27 16:12:16 +00:00
|
|
|
template <class Types>
|
|
|
|
class CYapfSegmentCostCacheNoneT
|
|
|
|
{
|
|
|
|
public:
|
2006-05-29 18:39:42 +00:00
|
|
|
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
|
2006-05-27 16:12:16 +00:00
|
|
|
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to attach cached or local segment cost data to the given node.
|
2006-09-04 20:40:33 +00:00
|
|
|
* @return true if globally cached data were used or false if local data was used */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE bool PfNodeCacheFetch(Node& n)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to flush the cached segment cost data back into cache storage.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Current cache implementation doesn't use that. */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE void PfNodeCacheFlush(Node& n)
|
|
|
|
{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment
|
2006-09-04 20:40:33 +00:00
|
|
|
* cost caching functionality for yapf. Used when node needs caching, but you don't want to
|
|
|
|
* cache the segment costs.
|
|
|
|
*/
|
2006-05-27 16:12:16 +00:00
|
|
|
template <class Types>
|
|
|
|
class CYapfSegmentCostCacheLocalT
|
|
|
|
{
|
|
|
|
public:
|
2006-05-29 18:39:42 +00:00
|
|
|
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
|
2006-05-27 16:12:16 +00:00
|
|
|
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
|
2006-05-29 18:39:42 +00:00
|
|
|
typedef typename Node::Key Key; ///< key to hash tables
|
2006-05-27 16:12:16 +00:00
|
|
|
typedef typename Node::CachedData CachedData;
|
|
|
|
typedef typename CachedData::Key CacheKey;
|
|
|
|
typedef CArrayT<CachedData> LocalCache;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
LocalCache m_local_cache;
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/// to access inherited path finder
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
|
|
|
|
|
|
|
|
public:
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to attach cached or local segment cost data to the given node.
|
2006-09-04 20:40:33 +00:00
|
|
|
* @return true if globally cached data were used or false if local data was used */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE bool PfNodeCacheFetch(Node& n)
|
|
|
|
{
|
|
|
|
CacheKey key(n.GetKey());
|
|
|
|
Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to flush the cached segment cost data back into cache storage.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Current cache implementation doesn't use that. */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE void PfNodeCacheFlush(Node& n)
|
|
|
|
{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Base class for segment cost cache providers. Contains global counter
|
2006-09-04 20:40:33 +00:00
|
|
|
* of track layout changes and static notification function called whenever
|
|
|
|
* the track layout changes. It is implemented as base class because it needs
|
|
|
|
* to be shared between all rail YAPF types (one shared counter, one notification
|
|
|
|
* function. */
|
2006-05-27 16:12:16 +00:00
|
|
|
struct CSegmentCostCacheBase
|
|
|
|
{
|
|
|
|
static int s_rail_change_counter;
|
|
|
|
|
|
|
|
static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** CSegmentCostCacheT - template class providing hash-map and storage (heap)
|
2006-09-04 20:40:33 +00:00
|
|
|
* of Tsegment structures. Each rail node contains pointer to the segment
|
|
|
|
* that contains cached (or non-cached) segment cost information. Nodes can
|
|
|
|
* differ by key type, but they use the same segment type. Segment key should
|
|
|
|
* be always the same (TileIndex + DiagDirection) that represent the beginning
|
|
|
|
* of the segment (origin tile and exit-dir from this tile).
|
|
|
|
* Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
|
|
|
|
* Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
|
2006-05-27 16:12:16 +00:00
|
|
|
template <class Tsegment>
|
|
|
|
struct CSegmentCostCacheT
|
|
|
|
: public CSegmentCostCacheBase
|
|
|
|
{
|
|
|
|
enum {c_hash_bits = 14};
|
|
|
|
|
|
|
|
typedef CHashTableT<Tsegment, c_hash_bits> HashTable;
|
|
|
|
typedef CArrayT<Tsegment> Heap;
|
|
|
|
typedef typename Tsegment::Key Key; ///< key to hash table
|
|
|
|
|
|
|
|
HashTable m_map;
|
|
|
|
Heap m_heap;
|
|
|
|
|
|
|
|
FORCEINLINE CSegmentCostCacheT() {}
|
|
|
|
|
|
|
|
FORCEINLINE Tsegment& Get(Key& key, bool *found)
|
|
|
|
{
|
2006-11-14 13:42:50 +00:00
|
|
|
Tsegment* item = m_map.Find(key);
|
2006-05-27 16:12:16 +00:00
|
|
|
if (item == NULL) {
|
|
|
|
*found = false;
|
|
|
|
item = new (&m_heap.AddNC()) Tsegment(key);
|
|
|
|
m_map.Push(*item);
|
|
|
|
} else {
|
|
|
|
*found = true;
|
|
|
|
}
|
|
|
|
return *item;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost
|
2006-09-04 20:40:33 +00:00
|
|
|
* caching functionality to yapf. Using this class as base of your will provide the global
|
|
|
|
* segment cost caching services for your Nodes.
|
2006-05-27 16:12:16 +00:00
|
|
|
*/
|
|
|
|
template <class Types>
|
|
|
|
class CYapfSegmentCostCacheGlobalT
|
|
|
|
: public CYapfSegmentCostCacheLocalT<Types>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
|
2006-05-29 18:39:42 +00:00
|
|
|
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
|
2006-05-27 16:12:16 +00:00
|
|
|
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
|
|
|
|
typedef typename Node::Key Key; ///< key to hash tables
|
|
|
|
typedef typename Node::CachedData CachedData;
|
|
|
|
typedef typename CachedData::Key CacheKey;
|
|
|
|
typedef CSegmentCostCacheT<CachedData> Cache;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Cache& m_global_cache;
|
|
|
|
|
|
|
|
FORCEINLINE CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/// to access inherited path finder
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
|
|
|
|
|
|
|
|
FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
|
|
|
|
|
|
|
|
FORCEINLINE static Cache& stGetGlobalCache()
|
|
|
|
{
|
|
|
|
static int last_rail_change_counter = 0;
|
2006-08-17 20:22:35 +00:00
|
|
|
static Date last_date = 0;
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
// some statistics
|
2006-08-17 20:22:35 +00:00
|
|
|
if (last_date != _date) {
|
|
|
|
last_date = _date;
|
2006-11-03 01:18:40 +00:00
|
|
|
DEBUG(yapf, 1) ("pf time today:%5d ms", _total_pf_time_us / 1000);
|
2006-05-27 16:12:16 +00:00
|
|
|
_total_pf_time_us = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache*& pC = stGlobalCachePtr();
|
|
|
|
|
|
|
|
// delete the cache sometimes...
|
|
|
|
if (pC != NULL && last_rail_change_counter != Cache::s_rail_change_counter) {
|
|
|
|
last_rail_change_counter = Cache::s_rail_change_counter;
|
|
|
|
delete pC;
|
|
|
|
pC = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pC == NULL)
|
|
|
|
pC = new Cache();
|
|
|
|
return *pC;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to attach cached or local segment cost data to the given node.
|
2006-09-04 20:40:33 +00:00
|
|
|
* @return true if globally cached data were used or false if local data was used */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE bool PfNodeCacheFetch(Node& n)
|
|
|
|
{
|
|
|
|
if (!Yapf().CanUseGlobalCache(n)) {
|
|
|
|
return Tlocal::PfNodeCacheFetch(n);
|
|
|
|
}
|
|
|
|
CacheKey key(n.GetKey());
|
|
|
|
bool found;
|
|
|
|
CachedData& item = m_global_cache.Get(key, &found);
|
|
|
|
Yapf().ConnectNodeToCachedData(n, item);
|
|
|
|
return found;
|
|
|
|
};
|
|
|
|
|
2006-05-29 18:39:42 +00:00
|
|
|
/** Called by YAPF to flush the cached segment cost data back into cache storage.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Current cache implementation doesn't use that. */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE void PfNodeCacheFlush(Node& n)
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* YAPF_COSTCACHE_HPP */
|