src/yapf/yapf_costcache.hpp
author skidd13
Sun, 02 Dec 2007 21:43:16 +0000
changeset 8004 1c54bc6f4bdf
parent 6121 2aae24b0881f
child 8140 0d0d8c94f84b
permissions -rw-r--r--
(svn r11563) -Codechange: Align the preprocessor code in stdafx.h with tabs
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
6121
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5475
diff changeset
     2
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5475
diff changeset
     3
/** @file yapf_costcache.hpp */
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5475
diff changeset
     4
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
#ifndef  YAPF_COSTCACHE_HPP
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
#define  YAPF_COSTCACHE_HPP
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     8
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
/** CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    10
 * PfNodeCacheFetch() and PfNodeCacheFlush() callbacks. Used when nodes don't have CachedData
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    11
 * defined (they don't count with any segment cost caching).
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    12
 */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    13
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
class CYapfSegmentCostCacheNoneT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    16
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    17
	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    20
	/** Called by YAPF to attach cached or local segment cost data to the given node.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    21
	 *  @return true if globally cached data were used or false if local data was used */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    22
	FORCEINLINE bool PfNodeCacheFetch(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    23
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    24
		return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    27
	/** Called by YAPF to flush the cached segment cost data back into cache storage.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    28
	 *  Current cache implementation doesn't use that. */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    29
	FORCEINLINE void PfNodeCacheFlush(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    30
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    31
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
/** CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    36
 * cost caching functionality for yapf. Used when node needs caching, but you don't want to
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    37
 * cache the segment costs.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    38
 */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
class CYapfSegmentCostCacheLocalT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    41
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    42
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    43
	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    45
	typedef typename Node::Key Key;               ///< key to hash tables
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
	typedef typename Node::CachedData CachedData;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
	typedef typename CachedData::Key CacheKey;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
	typedef CArrayT<CachedData> LocalCache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
protected:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    51
	LocalCache      m_local_cache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    53
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    56
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    57
	/** Called by YAPF to attach cached or local segment cost data to the given node.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    58
	 *  @return true if globally cached data were used or false if local data was used */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    59
	FORCEINLINE bool PfNodeCacheFetch(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
		CacheKey key(n.GetKey());
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
		Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    63
		return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    65
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    66
	/** Called by YAPF to flush the cached segment cost data back into cache storage.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    67
	 *  Current cache implementation doesn't use that. */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
	FORCEINLINE void PfNodeCacheFlush(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    74
/** Base class for segment cost cache providers. Contains global counter
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    75
 *  of track layout changes and static notification function called whenever
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    76
 *  the track layout changes. It is implemented as base class because it needs
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    77
 *  to be shared between all rail YAPF types (one shared counter, one notification
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    78
 *  function. */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
struct CSegmentCostCacheBase
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    81
	static int   s_rail_change_counter;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
	static void NotifyTrackLayoutChange(TileIndex tile, Track track) {s_rail_change_counter++;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    85
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    87
/** CSegmentCostCacheT - template class providing hash-map and storage (heap)
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    88
 *  of Tsegment structures. Each rail node contains pointer to the segment
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    89
 *  that contains cached (or non-cached) segment cost information. Nodes can
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    90
 *  differ by key type, but they use the same segment type. Segment key should
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    91
 *  be always the same (TileIndex + DiagDirection) that represent the beginning
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    92
 *  of the segment (origin tile and exit-dir from this tile).
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    93
 *  Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
    94
 *  Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    95
template <class Tsegment>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
struct CSegmentCostCacheT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    97
	: public CSegmentCostCacheBase
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    99
	enum {c_hash_bits = 14};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   100
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   101
	typedef CHashTableT<Tsegment, c_hash_bits> HashTable;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   102
	typedef CArrayT<Tsegment> Heap;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   103
	typedef typename Tsegment::Key Key;    ///< key to hash table
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   104
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   105
	HashTable    m_map;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
	Heap         m_heap;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   107
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   108
	FORCEINLINE CSegmentCostCacheT() {}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   109
5129
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   110
	/** flush (clear) the cache */
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   111
	FORCEINLINE void Flush() {m_map.Clear(); m_heap.Clear();};
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   112
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   113
	FORCEINLINE Tsegment& Get(Key& key, bool *found)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   114
	{
5083
102fef5c5c9c (svn r7147) -CodeChange: Don't use references if they can refer to NULL (Tron)
KUDr
parents: 5015
diff changeset
   115
		Tsegment* item = m_map.Find(key);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   116
		if (item == NULL) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   117
			*found = false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
			item = new (&m_heap.AddNC()) Tsegment(key);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
			m_map.Push(*item);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
		} else {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   121
			*found = true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
		return *item;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   124
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
/** CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
   128
 *  caching functionality to yapf. Using this class as base of your will provide the global
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
   129
 *  segment cost caching services for your Nodes.
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
*/
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   132
class CYapfSegmentCostCacheGlobalT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   133
	: public CYapfSegmentCostCacheLocalT<Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   134
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
public:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
	typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   137
	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
	typedef typename Node::Key Key;    ///< key to hash tables
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   140
	typedef typename Node::CachedData CachedData;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
	typedef typename CachedData::Key CacheKey;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   142
	typedef CSegmentCostCacheT<CachedData> Cache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   143
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   144
protected:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   145
	Cache&      m_global_cache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
	FORCEINLINE CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   148
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   149
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
	FORCEINLINE static Cache& stGetGlobalCache()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
		static int last_rail_change_counter = 0;
4297
d0311b62255c (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 3914
diff changeset
   155
		static Date last_date = 0;
5129
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   156
		static Cache C;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   157
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   158
		// some statistics
4297
d0311b62255c (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 3914
diff changeset
   159
		if (last_date != _date) {
d0311b62255c (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 3914
diff changeset
   160
			last_date = _date;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5129
diff changeset
   161
			DEBUG(yapf, 2, "Pf time today: %5d ms", _total_pf_time_us / 1000);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   162
			_total_pf_time_us = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   163
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   164
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   165
		// delete the cache sometimes...
5129
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   166
		if (last_rail_change_counter != Cache::s_rail_change_counter) {
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   167
			last_rail_change_counter = Cache::s_rail_change_counter;
5129
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   168
			C.Flush();
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   169
		}
5129
46a8cf87b546 (svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents: 5083
diff changeset
   170
		return C;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   171
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   172
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   174
	/** Called by YAPF to attach cached or local segment cost data to the given node.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
   175
	 *  @return true if globally cached data were used or false if local data was used */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   176
	FORCEINLINE bool PfNodeCacheFetch(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   178
		if (!Yapf().CanUseGlobalCache(n)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
			return Tlocal::PfNodeCacheFetch(n);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
		CacheKey key(n.GetKey());
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
		bool found;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
		CachedData& item = m_global_cache.Get(key, &found);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   184
		Yapf().ConnectNodeToCachedData(n, item);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
		return found;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   186
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   187
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   188
	/** Called by YAPF to flush the cached segment cost data back into cache storage.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4297
diff changeset
   189
	 *  Current cache implementation doesn't use that. */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   190
	FORCEINLINE void PfNodeCacheFlush(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   191
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
	};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   193
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   194
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   195
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   196
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   197
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   198
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
#endif /* YAPF_COSTCACHE_HPP */