OpenTTD
yapf_costcache.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef YAPF_COSTCACHE_HPP
11 #define YAPF_COSTCACHE_HPP
12 
13 #include "../../date_func.h"
14 
20 template <class Types>
22 {
23 public:
24  typedef typename Types::Tpf Tpf;
25  typedef typename Types::NodeList::Titem Node;
26 
31  inline bool PfNodeCacheFetch(Node &n)
32  {
33  return false;
34  }
35 
40  inline void PfNodeCacheFlush(Node &n)
41  {
42  }
43 };
44 
45 
51 template <class Types>
53 {
54 public:
55  typedef typename Types::Tpf Tpf;
56  typedef typename Types::NodeList::Titem Node;
57  typedef typename Node::Key Key;
58  typedef typename Node::CachedData CachedData;
59  typedef typename CachedData::Key CacheKey;
61 
62 protected:
63  LocalCache m_local_cache;
64 
66  inline Tpf& Yapf()
67  {
68  return *static_cast<Tpf *>(this);
69  }
70 
71 public:
76  inline bool PfNodeCacheFetch(Node &n)
77  {
78  CacheKey key(n.GetKey());
79  Yapf().ConnectNodeToCachedData(n, *new (m_local_cache.Append()) CachedData(key));
80  return false;
81  }
82 
87  inline void PfNodeCacheFlush(Node &n)
88  {
89  }
90 };
91 
92 
101 {
103 
104  static void NotifyTrackLayoutChange(TileIndex tile, Track track)
105  {
106  s_rail_change_counter++;
107  }
108 };
109 
110 
121 template <class Tsegment>
123  static const int C_HASH_BITS = 14;
124 
126  typedef SmallArray<Tsegment> Heap;
127  typedef typename Tsegment::Key Key;
128 
129  HashTable m_map;
130  Heap m_heap;
131 
132  inline CSegmentCostCacheT() {}
133 
135  inline void Flush()
136  {
137  m_map.Clear();
138  m_heap.Clear();
139  }
140 
141  inline Tsegment& Get(Key &key, bool *found)
142  {
143  Tsegment *item = m_map.Find(key);
144  if (item == nullptr) {
145  *found = false;
146  item = new (m_heap.Append()) Tsegment(key);
147  m_map.Push(*item);
148  } else {
149  *found = true;
150  }
151  return *item;
152  }
153 };
154 
160 template <class Types>
162 public:
164  typedef typename Types::Tpf Tpf;
165  typedef typename Types::NodeList::Titem Node;
166  typedef typename Node::Key Key;
167  typedef typename Node::CachedData CachedData;
168  typedef typename CachedData::Key CacheKey;
170 
171 protected:
172  Cache &m_global_cache;
173 
174  inline CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
175 
177  inline Tpf& Yapf()
178  {
179  return *static_cast<Tpf *>(this);
180  }
181 
182  inline static Cache& stGetGlobalCache()
183  {
184  static int last_rail_change_counter = 0;
185  static Date last_date = 0;
186  static Cache C;
187 
188  /* some statistics */
189  if (last_date != _date) {
190  last_date = _date;
191  DEBUG(yapf, 2, "Pf time today: %5d ms", _total_pf_time_us / 1000);
192  _total_pf_time_us = 0;
193  }
194 
195  /* delete the cache sometimes... */
196  if (last_rail_change_counter != Cache::s_rail_change_counter) {
197  last_rail_change_counter = Cache::s_rail_change_counter;
198  C.Flush();
199  }
200  return C;
201  }
202 
203 public:
208  inline bool PfNodeCacheFetch(Node &n)
209  {
210  if (!Yapf().CanUseGlobalCache(n)) {
211  return Tlocal::PfNodeCacheFetch(n);
212  }
213  CacheKey key(n.GetKey());
214  bool found;
215  CachedData &item = m_global_cache.Get(key, &found);
216  Yapf().ConnectNodeToCachedData(n, item);
217  return found;
218  }
219 
224  inline void PfNodeCacheFlush(Node &n)
225  {
226  }
227 };
228 
229 #endif /* YAPF_COSTCACHE_HPP */
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Types::NodeList::Titem Node
this will be our node type
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
void Push(Titem_ &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:247
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures...
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Tsegment::Key Key
key to hash table
T * Append()
allocate but not construct new item
Definition: array.hpp:74
void Clear()
Clear (destroy) all items.
Definition: array.hpp:48
Types::NodeList::Titem Node
this will be our node type
static int s_rail_change_counter
if any track changes, this counter is incremented - that will invalidate segment cost cache ...
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
const Titem_ * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:189
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::NodeList::Titem Node
this will be our node type
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Definition: hashtable.hpp:183
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Tpf & Yapf()
to access inherited path finder
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Tpf & Yapf()
to access inherited path finder
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Base class for segment cost cache providers.
CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment cost caching ...
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Track
These are used to specify a single track.
Definition: track_type.h:19
void Flush()
flush (clear) the cache
Node::Key Key
key to hash tables
int32 Date
The type to store our dates in.
Definition: date_type.h:14
Date _date
Current date in days (day counter)
Definition: date.cpp:26
Node::Key Key
key to hash tables
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.