From 848464c7721bf7535f33193c46a7024ddd41da06 Mon Sep 17 00:00:00 2001 From: cirdan Date: Tue, 28 Jan 2014 21:28:03 +0100 Subject: [PATCH] Inline yapf_costcache.hpp into yapf_rail.cpp Inline yapf_costcache.hpp into yapf_rail.cpp, which was the only source file that required it. --- projects/openttd_vs100.vcxproj | 1 - projects/openttd_vs100.vcxproj.filters | 3 - projects/openttd_vs80.vcproj | 4 - projects/openttd_vs90.vcproj | 4 - source.list | 1 - src/pathfinder/yapf/yapf.hpp | 2 - src/pathfinder/yapf/yapf_costcache.hpp | 160 --------------------------------- src/pathfinder/yapf/yapf_rail.cpp | 144 +++++++++++++++++++++++++++++ src/train_cmd.cpp | 1 + 9 files changed, 145 insertions(+), 175 deletions(-) delete mode 100644 src/pathfinder/yapf/yapf_costcache.hpp diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 2fa77c313..3c97bfee0 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -2048,7 +2048,6 @@ - diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index e6b199898..9d6cb996b 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -3000,9 +3000,6 @@ YAPF - - YAPF - YAPF diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 6073be652..396878c1d 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -12327,10 +12327,6 @@ > - - diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 24d5fb87f..8adb8e7d5 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -12324,10 +12324,6 @@ > - - diff --git a/source.list b/source.list index 0c6c02bff..11d275ff7 100644 --- a/source.list +++ b/source.list @@ -1061,7 +1061,6 @@ pathfinder/yapf/yapf.hpp pathfinder/yapf/yapf_base.hpp pathfinder/yapf/yapf_cache.h pathfinder/yapf/yapf_common.hpp -pathfinder/yapf/yapf_costcache.hpp pathfinder/yapf/yapf_node.hpp pathfinder/yapf/yapf_node_rail.hpp pathfinder/yapf/yapf_node_road.hpp diff --git a/src/pathfinder/yapf/yapf.hpp b/src/pathfinder/yapf/yapf.hpp index c41adb60b..d3e42fff4 100644 --- a/src/pathfinder/yapf/yapf.hpp +++ b/src/pathfinder/yapf/yapf.hpp @@ -26,7 +26,5 @@ #include "yapf_base.hpp" #include "yapf_node.hpp" #include "yapf_common.hpp" -#include "yapf_costcache.hpp" - #endif /* YAPF_HPP */ diff --git a/src/pathfinder/yapf/yapf_costcache.hpp b/src/pathfinder/yapf/yapf_costcache.hpp deleted file mode 100644 index 71852c608..000000000 --- a/src/pathfinder/yapf/yapf_costcache.hpp +++ /dev/null @@ -1,160 +0,0 @@ -/* $Id$ */ - -/* - * This file is part of OpenTTD. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . - */ - -/** @file yapf_costcache.hpp Caching of segment costs. */ - -#ifndef YAPF_COSTCACHE_HPP -#define YAPF_COSTCACHE_HPP - -#include "../../date_func.h" -#include "../../misc/array.hpp" -#include "../../misc/hashtable.hpp" - -/** - * 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 -struct CSegmentCostCacheT - : public CSegmentCostCacheBase -{ - static const int C_HASH_BITS = 14; - - typedef CHashTableT HashTable; - typedef SmallArray Heap; - typedef typename Tsegment::Key Key; ///< key to hash table - - HashTable m_map; - Heap m_heap; - - inline CSegmentCostCacheT() {} - - /** flush (clear) the cache */ - inline void Flush() - { - m_map.Clear(); - m_heap.Clear(); - } - - inline Tsegment& Get(Key& key, bool *found) - { - Tsegment *item = m_map.Find(key); - if (item == NULL) { - *found = false; - item = new (m_heap.Append()) 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 CYapfSegmentCostCacheGlobalT -{ -public: - typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) - typedef typename Types::Astar::Node 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 Cache; - typedef SmallArray LocalCache; - -protected: - Cache& m_global_cache; - LocalCache m_local_cache; - - inline CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {}; - - /** to access inherited path finder */ - inline Tpf& Yapf() - { - return *static_cast(this); - } - - inline 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 - */ - inline bool PfNodeCacheFetch(Node& n) - { - CacheKey key(n.GetKey()); - if (!Yapf().CanUseGlobalCache(n)) { - Yapf().ConnectNodeToCachedData(n, *new (m_local_cache.Append()) CachedData(key)); - return false; - } - 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. - */ - inline void PfNodeCacheFlush(Node& n) - { - } -}; - -#endif /* YAPF_COSTCACHE_HPP */ diff --git a/src/pathfinder/yapf/yapf_rail.cpp b/src/pathfinder/yapf/yapf_rail.cpp index f082db2cc..05bea53da 100644 --- a/src/pathfinder/yapf/yapf_rail.cpp +++ b/src/pathfinder/yapf/yapf_rail.cpp @@ -18,9 +18,12 @@ #include "../../newgrf_station.h" #include "../../station_func.h" #include "../../misc/blob.hpp" +#include "../../misc/array.hpp" +#include "../../misc/hashtable.hpp" #include "../../map/slope.h" #include "../../pbs.h" #include "../../waypoint_base.h" +#include "../../date_func.h" #define DEBUG_YAPF_CACHE 0 @@ -42,6 +45,147 @@ template void DumpState(Tpf &pf1, Tpf &pf2) int _total_pf_time_us = 0; +/** + * 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 +struct CSegmentCostCacheT + : public CSegmentCostCacheBase +{ + static const int C_HASH_BITS = 14; + + typedef CHashTableT HashTable; + typedef SmallArray Heap; + typedef typename Tsegment::Key Key; ///< key to hash table + + HashTable m_map; + Heap m_heap; + + inline CSegmentCostCacheT() {} + + /** flush (clear) the cache */ + inline void Flush() + { + m_map.Clear(); + m_heap.Clear(); + } + + inline Tsegment& Get(Key& key, bool *found) + { + Tsegment *item = m_map.Find(key); + if (item == NULL) { + *found = false; + item = new (m_heap.Append()) 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 CYapfSegmentCostCacheGlobalT +{ +public: + typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) + typedef typename Types::Astar::Node 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 Cache; + typedef SmallArray LocalCache; + +protected: + Cache& m_global_cache; + LocalCache m_local_cache; + + inline CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {}; + + /** to access inherited path finder */ + inline Tpf& Yapf() + { + return *static_cast(this); + } + + inline 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 + */ + inline bool PfNodeCacheFetch(Node& n) + { + CacheKey key(n.GetKey()); + if (!Yapf().CanUseGlobalCache(n)) { + Yapf().ConnectNodeToCachedData(n, *new (m_local_cache.Append()) CachedData(key)); + return false; + } + 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. + */ + inline void PfNodeCacheFlush(Node& n) + { + } +}; + + template class CYapfRailT { diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index c4533778d..e6243de5a 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -37,6 +37,7 @@ #include "signal_func.h" #include "station_func.h" #include "newgrf_debug.h" +#include "date_func.h" #include "table/strings.h" #include "table/train_cmd.h" -- 2.11.4.GIT