diff options
author | rubidium <rubidium@openttd.org> | 2007-01-02 19:19:48 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-01-02 19:19:48 +0000 |
commit | 013df98f79866a75f367853c9e436f3c5c79f645 (patch) | |
tree | ad4a63860df2626b22f77e7dac712e958bea54cb /src/yapf/yapf_costcache.hpp | |
parent | 3d32fd3f4bfaceb8a48530fbc2f4bd5db2752596 (diff) | |
download | openttd-013df98f79866a75f367853c9e436f3c5c79f645.tar.xz |
(svn r7759) -Merge: makefile rewrite. This merge features:
- A proper ./configure, so everything needs to be configured only once, not for every make.
- Usage of makedepend when available. This greatly reduces the time needed for generating the dependencies.
- A generator for all project files. There is a single file with sources, which is used to generate Makefiles and the project files for MSVC.
- Proper support for OSX universal binaries.
- Object files for non-MSVC compiles are also placed in separate directories, making is faster to switch between debug and release compiles and it does not touch the directory with the source files.
- Functionality to make a bundle of all needed files for for example a nightly or distribution of a binary with all needed GRFs and language files.
Note: as this merge moves almost all files, it is recommended to make a backup of your working copy before updating your working copy.
Diffstat (limited to 'src/yapf/yapf_costcache.hpp')
-rw-r--r-- | src/yapf/yapf_costcache.hpp | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/src/yapf/yapf_costcache.hpp b/src/yapf/yapf_costcache.hpp new file mode 100644 index 000000000..c90d37302 --- /dev/null +++ b/src/yapf/yapf_costcache.hpp @@ -0,0 +1,196 @@ +/* $Id$ */ +#ifndef YAPF_COSTCACHE_HPP +#define YAPF_COSTCACHE_HPP + + +/** CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements + * PfNodeCacheFetch() and PfNodeCacheFlush() callbacks. Used when nodes don't have CachedData + * defined (they don't count with any segment cost caching). + */ +template <class Types> +class CYapfSegmentCostCacheNoneT +{ +public: + typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) + typedef typename Types::NodeList::Titem Node; ///< this will be our node type + + /** Called by YAPF to attach cached or local segment cost data to the given node. + * @return true if globally cached data were used or false if local data was used */ + FORCEINLINE bool PfNodeCacheFetch(Node& n) + { + return false; + }; + + /** Called by YAPF to flush the cached segment cost data back into cache storage. + * Current cache implementation doesn't use that. */ + FORCEINLINE void PfNodeCacheFlush(Node& n) + { + }; +}; + + +/** CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment + * cost caching functionality for yapf. Used when node needs caching, but you don't want to + * cache the segment costs. + */ +template <class Types> +class CYapfSegmentCostCacheLocalT +{ +public: + typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) + 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 CArrayT<CachedData> LocalCache; + +protected: + LocalCache m_local_cache; + + /// to access inherited path finder + FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);} + +public: + /** Called by YAPF to attach cached or local segment cost data to the given node. + * @return true if globally cached data were used or false if local data was used */ + FORCEINLINE bool PfNodeCacheFetch(Node& n) + { + CacheKey key(n.GetKey()); + Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key)); + return false; + }; + + /** Called by YAPF to flush the cached segment cost data back into cache storage. + * Current cache implementation doesn't use that. */ + FORCEINLINE void PfNodeCacheFlush(Node& n) + { + }; +}; + + +/** Base class for segment cost cache providers. Contains global counter + * 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. */ +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) + * 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 */ +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() {} + + /** flush (clear) the cache */ + FORCEINLINE void Flush() {m_map.Clear(); m_heap.Clear();}; + + FORCEINLINE Tsegment& Get(Key& key, bool *found) + { + Tsegment* item = m_map.Find(key); + 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 + * caching functionality to yapf. Using this class as base of your will provide the global + * segment cost caching services for your Nodes. +*/ +template <class Types> +class CYapfSegmentCostCacheGlobalT + : public CYapfSegmentCostCacheLocalT<Types> +{ +public: + typedef CYapfSegmentCostCacheLocalT<Types> Tlocal; + typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) + 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()) {}; + + /// to access inherited path finder + FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);} + + FORCEINLINE static Cache& stGetGlobalCache() + { + static int last_rail_change_counter = 0; + static Date last_date = 0; + static Cache C; + + // some statistics + if (last_date != _date) { + last_date = _date; + DEBUG(yapf, 2, "Pf time today: %5d ms", _total_pf_time_us / 1000); + _total_pf_time_us = 0; + } + + // delete the cache sometimes... + if (last_rail_change_counter != Cache::s_rail_change_counter) { + last_rail_change_counter = Cache::s_rail_change_counter; + C.Flush(); + } + return C; + } + +public: + /** Called by YAPF to attach cached or local segment cost data to the given node. + * @return true if globally cached data were used or false if local data was used */ + 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; + }; + + /** Called by YAPF to flush the cached segment cost data back into cache storage. + * Current cache implementation doesn't use that. */ + FORCEINLINE void PfNodeCacheFlush(Node& n) + { + }; + +}; + + + + +#endif /* YAPF_COSTCACHE_HPP */ |