yapf/yapf_costcache.hpp
changeset 4549 106ed18a7675
parent 4297 d0311b62255c
child 5015 63f05008fabe
equal deleted inserted replaced
4548:6165e12570bf 4549:106ed18a7675
     2 #ifndef  YAPF_COSTCACHE_HPP
     2 #ifndef  YAPF_COSTCACHE_HPP
     3 #define  YAPF_COSTCACHE_HPP
     3 #define  YAPF_COSTCACHE_HPP
     4 
     4 
     5 
     5 
     6 /** CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements
     6 /** CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements
     7 PfNodeCacheFetch() and PfNodeCacheFlush() callbacks. Used when nodes don't have CachedData
     7  * PfNodeCacheFetch() and PfNodeCacheFlush() callbacks. Used when nodes don't have CachedData
     8 defined (they don't count with any segment cost caching).
     8  * defined (they don't count with any segment cost caching).
     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;              ///< the pathfinder class (derived from THIS class)
    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.
    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 */
    18 	 *  @return true if globally cached data were used or false if local data was used */
    19 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    19 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    20 	{
    20 	{
    21 		return false;
    21 		return false;
    22 	};
    22 	};
    23 
    23 
    24 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
    24 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
    25 	*   Current cache implementation doesn't use that. */
    25 	 *  Current cache implementation doesn't use that. */
    26 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    26 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    27 	{
    27 	{
    28 	};
    28 	};
    29 };
    29 };
    30 
    30 
    31 
    31 
    32 /** CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment
    32 /** CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment
    33 cost caching functionality for yapf. Used when node needs caching, but you don't want to
    33  * cost caching functionality for yapf. Used when node needs caching, but you don't want to
    34 cache the segment costs.
    34  * cache the segment costs.
    35 */
    35  */
    36 template <class Types>
    36 template <class Types>
    37 class CYapfSegmentCostCacheLocalT
    37 class CYapfSegmentCostCacheLocalT
    38 {
    38 {
    39 public:
    39 public:
    40 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
    40 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
    50 	/// to access inherited path finder
    50 	/// to access inherited path finder
    51 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
    51 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
    52 
    52 
    53 public:
    53 public:
    54 	/** Called by YAPF to attach cached or local segment cost data to the given node.
    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 */
    55 	 *  @return true if globally cached data were used or false if local data was used */
    56 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    56 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
    57 	{
    57 	{
    58 		CacheKey key(n.GetKey());
    58 		CacheKey key(n.GetKey());
    59 		Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
    59 		Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
    60 		return false;
    60 		return false;
    61 	};
    61 	};
    62 
    62 
    63 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
    63 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
    64 	*   Current cache implementation doesn't use that. */
    64 	 *  Current cache implementation doesn't use that. */
    65 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    65 	FORCEINLINE void PfNodeCacheFlush(Node& n)
    66 	{
    66 	{
    67 	};
    67 	};
    68 };
    68 };
    69 
    69 
    70 
    70 
    71 /** Base class for segment cost cache providers. Contains global counter
    71 /** Base class for segment cost cache providers. Contains global counter
    72 *   of track layout changes and static notification function called whenever
    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
    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
    74  *  to be shared between all rail YAPF types (one shared counter, one notification
    75 *   function. */
    75  *  function. */
    76 struct CSegmentCostCacheBase
    76 struct CSegmentCostCacheBase
    77 {
    77 {
    78 	static int   s_rail_change_counter;
    78 	static int   s_rail_change_counter;
    79 
    79 
    80 	static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
    80 	static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
    81 };
    81 };
    82 
    82 
    83 
    83 
    84 /** CSegmentCostCacheT - template class providing hash-map and storage (heap)
    84 /** CSegmentCostCacheT - template class providing hash-map and storage (heap)
    85     of Tsegment structures. Each rail node contains pointer to the segment
    85  *  of Tsegment structures. Each rail node contains pointer to the segment
    86     that contains cached (or non-cached) segment cost information. Nodes can
    86  *  that contains cached (or non-cached) segment cost information. Nodes can
    87     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
    88     be always the same (TileIndex + DiagDirection) that represent the beginning
    88  *  be always the same (TileIndex + DiagDirection) that represent the beginning
    89     of the segment (origin tile and exit-dir from this tile).
    89  *  of the segment (origin tile and exit-dir from this tile).
    90     Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
    90  *  Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
    91     Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
    91  *  Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
    92 template <class Tsegment>
    92 template <class Tsegment>
    93 struct CSegmentCostCacheT
    93 struct CSegmentCostCacheT
    94 	: public CSegmentCostCacheBase
    94 	: public CSegmentCostCacheBase
    95 {
    95 {
    96 	enum {c_hash_bits = 14};
    96 	enum {c_hash_bits = 14};
   117 		return *item;
   117 		return *item;
   118 	}
   118 	}
   119 };
   119 };
   120 
   120 
   121 /** CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost
   121 /** CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost
   122     caching functionality to yapf. Using this class as base of your will provide the global
   122  *  caching functionality to yapf. Using this class as base of your will provide the global
   123 		segment cost caching services for your Nodes.
   123  *  segment cost caching services for your Nodes.
   124 */
   124 */
   125 template <class Types>
   125 template <class Types>
   126 class CYapfSegmentCostCacheGlobalT
   126 class CYapfSegmentCostCacheGlobalT
   127 	: public CYapfSegmentCostCacheLocalT<Types>
   127 	: public CYapfSegmentCostCacheLocalT<Types>
   128 {
   128 {
   171 		return *pC;
   171 		return *pC;
   172 	}
   172 	}
   173 
   173 
   174 public:
   174 public:
   175 	/** Called by YAPF to attach cached or local segment cost data to the given node.
   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 */
   176 	 *  @return true if globally cached data were used or false if local data was used */
   177 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
   177 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
   178 	{
   178 	{
   179 		if (!Yapf().CanUseGlobalCache(n)) {
   179 		if (!Yapf().CanUseGlobalCache(n)) {
   180 			return Tlocal::PfNodeCacheFetch(n);
   180 			return Tlocal::PfNodeCacheFetch(n);
   181 		}
   181 		}
   185 		Yapf().ConnectNodeToCachedData(n, item);
   185 		Yapf().ConnectNodeToCachedData(n, item);
   186 		return found;
   186 		return found;
   187 	};
   187 	};
   188 
   188 
   189 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
   189 	/** Called by YAPF to flush the cached segment cost data back into cache storage.
   190 	*   Current cache implementation doesn't use that. */
   190 	 *  Current cache implementation doesn't use that. */
   191 	FORCEINLINE void PfNodeCacheFlush(Node& n)
   191 	FORCEINLINE void PfNodeCacheFlush(Node& n)
   192 	{
   192 	{
   193 	};
   193 	};
   194 
   194 
   195 };
   195 };