yapf/yapf_costcache.hpp
changeset 3914 6bdd22b93698
parent 3900 2c84ed52709d
child 4297 d0311b62255c
equal deleted inserted replaced
3913:1a104ca49e17 3914:6bdd22b93698
     9 */
     9 */
    10 template <class Types>
    10 template <class Types>
    11 class CYapfSegmentCostCacheNoneT
    11 class CYapfSegmentCostCacheNoneT
    12 {
    12 {
    13 public:
    13 public:
    14 	typedef typename Types::Tpf Tpf;
    14 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
    15 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
    15 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
    16 
    16 
       
    17 	/** Called by YAPF to attach cached or local segment cost data to the given node.
       
    18 	*   @return true if globally cached data were used or false if local data was used */
    17 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    19 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    18 	{
    20 	{
    19 		return false;
    21 		return false;
    20 	};
    22 	};
    21 
    23 
       
    24 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
       
    25 	*   Current cache implementation doesn't use that. */
    22 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    26 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    23 	{
    27 	{
    24 	};
    28 	};
    25 };
    29 };
    26 
    30 
    31 */
    35 */
    32 template <class Types>
    36 template <class Types>
    33 class CYapfSegmentCostCacheLocalT
    37 class CYapfSegmentCostCacheLocalT
    34 {
    38 {
    35 public:
    39 public:
    36 	typedef typename Types::Tpf Tpf;
    40 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
    37 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
    41 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
    38 	typedef typename Node::Key Key;    ///< key to hash tables
    42 	typedef typename Node::Key Key;               ///< key to hash tables
    39 	typedef typename Node::CachedData CachedData;
    43 	typedef typename Node::CachedData CachedData;
    40 	typedef typename CachedData::Key CacheKey;
    44 	typedef typename CachedData::Key CacheKey;
    41 	typedef CArrayT<CachedData> LocalCache;
    45 	typedef CArrayT<CachedData> LocalCache;
    42 
    46 
    43 protected:
    47 protected:
    44 	LocalCache      m_local_cache;
    48 	LocalCache      m_local_cache;
    45 
    49 
       
    50 	/// to access inherited path finder
    46 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
    51 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
    47 
    52 
    48 public:
    53 public:
       
    54 	/** Called by YAPF to attach cached or local segment cost data to the given node.
       
    55 	*   @return true if globally cached data were used or false if local data was used */
    49 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    56 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    50 	{
    57 	{
    51 		CacheKey key(n.GetKey());
    58 		CacheKey key(n.GetKey());
    52 		Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
    59 		Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
    53 		return false;
    60 		return false;
    54 	};
    61 	};
    55 
    62 
       
    63 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
       
    64 	*   Current cache implementation doesn't use that. */
    56 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    65 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    57 	{
    66 	{
    58 	};
    67 	};
    59 };
    68 };
    60 
    69 
    61 
    70 
    62 
    71 /** Base class for segment cost cache providers. Contains global counter
       
    72 *   of track layout changes and static notification function called whenever
       
    73 *   the track layout changes. It is implemented as base class because it needs
       
    74 *   to be shared between all rail YAPF types (one shared counter, one notification
       
    75 *   function. */
    63 struct CSegmentCostCacheBase
    76 struct CSegmentCostCacheBase
    64 {
    77 {
    65 	static int   s_rail_change_counter;
    78 	static int   s_rail_change_counter;
    66 
    79 
    67 	static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
    80 	static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
    74     differ by key type, but they use the same segment type. Segment key should
    87     differ by key type, but they use the same segment type. Segment key should
    75     be always the same (TileIndex + DiagDirection) that represent the beginning
    88     be always the same (TileIndex + DiagDirection) that represent the beginning
    76     of the segment (origin tile and exit-dir from this tile).
    89     of the segment (origin tile and exit-dir from this tile).
    77     Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
    90     Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
    78     Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
    91     Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
    79 
       
    80 template <class Tsegment>
    92 template <class Tsegment>
    81 struct CSegmentCostCacheT
    93 struct CSegmentCostCacheT
    82 	: public CSegmentCostCacheBase
    94 	: public CSegmentCostCacheBase
    83 {
    95 {
    84 	enum {c_hash_bits = 14};
    96 	enum {c_hash_bits = 14};
   114 class CYapfSegmentCostCacheGlobalT
   126 class CYapfSegmentCostCacheGlobalT
   115 	: public CYapfSegmentCostCacheLocalT<Types>
   127 	: public CYapfSegmentCostCacheLocalT<Types>
   116 {
   128 {
   117 public:
   129 public:
   118 	typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
   130 	typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
   119 	typedef typename Types::Tpf Tpf;
   131 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
   120 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
   132 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
   121 	typedef typename Node::Key Key;    ///< key to hash tables
   133 	typedef typename Node::Key Key;    ///< key to hash tables
   122 	typedef typename Node::CachedData CachedData;
   134 	typedef typename Node::CachedData CachedData;
   123 	typedef typename CachedData::Key CacheKey;
   135 	typedef typename CachedData::Key CacheKey;
   124 	typedef CSegmentCostCacheT<CachedData> Cache;
   136 	typedef CSegmentCostCacheT<CachedData> Cache;
   126 protected:
   138 protected:
   127 	Cache&      m_global_cache;
   139 	Cache&      m_global_cache;
   128 
   140 
   129 	FORCEINLINE CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
   141 	FORCEINLINE CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
   130 
   142 
       
   143 	/// to access inherited path finder
   131 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
   144 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
   132 
   145 
   133 	FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
   146 	FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
   134 
   147 
   135 	FORCEINLINE static Cache& stGetGlobalCache()
   148 	FORCEINLINE static Cache& stGetGlobalCache()
   157 			pC = new Cache();
   170 			pC = new Cache();
   158 		return *pC;
   171 		return *pC;
   159 	}
   172 	}
   160 
   173 
   161 public:
   174 public:
       
   175 	/** Called by YAPF to attach cached or local segment cost data to the given node.
       
   176 	*   @return true if globally cached data were used or false if local data was used */
   162 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
   177 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
   163 	{
   178 	{
   164 		if (!Yapf().CanUseGlobalCache(n)) {
   179 		if (!Yapf().CanUseGlobalCache(n)) {
   165 			return Tlocal::PfNodeCacheFetch(n);
   180 			return Tlocal::PfNodeCacheFetch(n);
   166 		}
   181 		}
   169 		CachedData& item = m_global_cache.Get(key, &found);
   184 		CachedData& item = m_global_cache.Get(key, &found);
   170 		Yapf().ConnectNodeToCachedData(n, item);
   185 		Yapf().ConnectNodeToCachedData(n, item);
   171 		return found;
   186 		return found;
   172 	};
   187 	};
   173 
   188 
       
   189 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
       
   190 	*   Current cache implementation doesn't use that. */
   174 	FORCEINLINE void PfNodeCacheFlush(Node& n)
   191 	FORCEINLINE void PfNodeCacheFlush(Node& n)
   175 	{
   192 	{
   176 	};
   193 	};
   177 
   194 
   178 };
   195 };