OpenTTD
yapf_costcache.hpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef YAPF_COSTCACHE_HPP
13 #define YAPF_COSTCACHE_HPP
14 
15 #include "../../date_func.h"
16 
22 template <class Types>
24 {
25 public:
26  typedef typename Types::Tpf Tpf;
27  typedef typename Types::NodeList::Titem Node;
28 
33  inline bool PfNodeCacheFetch(Node& n)
34  {
35  return false;
36  }
37 
42  inline void PfNodeCacheFlush(Node& n)
43  {
44  }
45 };
46 
47 
53 template <class Types>
55 {
56 public:
57  typedef typename Types::Tpf Tpf;
58  typedef typename Types::NodeList::Titem Node;
59  typedef typename Node::Key Key;
60  typedef typename Node::CachedData CachedData;
61  typedef typename CachedData::Key CacheKey;
63 
64 protected:
65  LocalCache m_local_cache;
66 
68  inline Tpf& Yapf()
69  {
70  return *static_cast<Tpf*>(this);
71  }
72 
73 public:
78  inline bool PfNodeCacheFetch(Node& n)
79  {
80  CacheKey key(n.GetKey());
81  Yapf().ConnectNodeToCachedData(n, *new (m_local_cache.Append()) CachedData(key));
82  return false;
83  }
84 
89  inline void PfNodeCacheFlush(Node& n)
90  {
91  }
92 };
93 
94 
103 {
105 
106  static void NotifyTrackLayoutChange(TileIndex tile, Track track)
107  {
109  }
110 };
111 
112 
123 template <class Tsegment>
125  : public CSegmentCostCacheBase
126 {
127  static const int C_HASH_BITS = 14;
128 
130  typedef SmallArray<Tsegment> Heap;
131  typedef typename Tsegment::Key Key;
132 
133  HashTable m_map;
134  Heap m_heap;
135 
136  inline CSegmentCostCacheT() {}
137 
139  inline void Flush()
140  {
141  m_map.Clear();
142  m_heap.Clear();
143  }
144 
145  inline Tsegment& Get(Key& key, bool *found)
146  {
147  Tsegment *item = m_map.Find(key);
148  if (item == NULL) {
149  *found = false;
150  item = new (m_heap.Append()) Tsegment(key);
151  m_map.Push(*item);
152  } else {
153  *found = true;
154  }
155  return *item;
156  }
157 };
158 
164 template <class Types>
166  : public CYapfSegmentCostCacheLocalT<Types>
167 {
168 public:
170  typedef typename Types::Tpf Tpf;
171  typedef typename Types::NodeList::Titem Node;
172  typedef typename Node::Key Key;
173  typedef typename Node::CachedData CachedData;
174  typedef typename CachedData::Key CacheKey;
176 
177 protected:
178  Cache& m_global_cache;
179 
180  inline CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
181 
183  inline Tpf& Yapf()
184  {
185  return *static_cast<Tpf*>(this);
186  }
187 
188  inline static Cache& stGetGlobalCache()
189  {
190  static int last_rail_change_counter = 0;
191  static Date last_date = 0;
192  static Cache C;
193 
194  /* some statistics */
195  if (last_date != _date) {
196  last_date = _date;
197  DEBUG(yapf, 2, "Pf time today: %5d ms", _total_pf_time_us / 1000);
198  _total_pf_time_us = 0;
199  }
200 
201  /* delete the cache sometimes... */
202  if (last_rail_change_counter != Cache::s_rail_change_counter) {
203  last_rail_change_counter = Cache::s_rail_change_counter;
204  C.Flush();
205  }
206  return C;
207  }
208 
209 public:
214  inline bool PfNodeCacheFetch(Node& n)
215  {
216  if (!Yapf().CanUseGlobalCache(n)) {
217  return Tlocal::PfNodeCacheFetch(n);
218  }
219  CacheKey key(n.GetKey());
220  bool found;
221  CachedData& item = m_global_cache.Get(key, &found);
222  Yapf().ConnectNodeToCachedData(n, item);
223  return found;
224  }
225 
230  inline void PfNodeCacheFlush(Node& n)
231  {
232  }
233 };
234 
235 #endif /* YAPF_COSTCACHE_HPP */