OpenTTD Source  20241120-master-g6d3adc6169
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 "../../misc/hashtable.hpp"
14 #include "../../tile_type.h"
15 #include "../../track_type.h"
16 
22 template <class Types>
24 {
25 public:
26  typedef typename Types::Tpf Tpf;
27  typedef typename Types::NodeList::Item Node;
28 
33  inline bool PfNodeCacheFetch(Node &)
34  {
35  return false;
36  }
37 };
38 
47 {
49 
50  static void NotifyTrackLayoutChange(TileIndex, Track)
51  {
53  }
54 };
55 
56 
67 template <class Tsegment>
69  static constexpr int HASH_BITS = 14;
70 
71  using Key = typename Tsegment::Key;
72 
74  std::deque<Tsegment> heap;
75 
76  inline CSegmentCostCacheT() {}
77 
79  inline void Flush()
80  {
81  this->map.Clear();
82  this->heap.clear();
83  }
84 
85  inline Tsegment &Get(Key &key, bool *found)
86  {
87  Tsegment *item = this->map.Find(key);
88  if (item == nullptr) {
89  *found = false;
90  item = &this->heap.emplace_back(key);
91  this->map.Push(*item);
92  } else {
93  *found = true;
94  }
95  return *item;
96  }
97 };
98 
104 template <class Types>
106 public:
107  typedef typename Types::Tpf Tpf;
108  typedef typename Types::NodeList::Item Node;
109  typedef typename Node::Key Key;
110  typedef typename Node::CachedData CachedData;
111  typedef typename CachedData::Key CacheKey;
113  using LocalCache = std::deque<CachedData>;
114 
115 protected:
116  Cache &global_cache;
117  LocalCache local_cache;
118 
119  inline CYapfSegmentCostCacheGlobalT() : global_cache(stGetGlobalCache()) {};
120 
122  inline Tpf &Yapf()
123  {
124  return *static_cast<Tpf *>(this);
125  }
126 
127  inline static Cache &stGetGlobalCache()
128  {
129  static int last_rail_change_counter = 0;
130  static Cache C;
131 
132  /* delete the cache sometimes... */
133  if (last_rail_change_counter != Cache::s_rail_change_counter) {
134  last_rail_change_counter = Cache::s_rail_change_counter;
135  C.Flush();
136  }
137  return C;
138  }
139 
140 public:
145  inline bool PfNodeCacheFetch(Node &n)
146  {
147  CacheKey key(n.GetKey());
148 
149  if (!Yapf().CanUseGlobalCache(n)) {
150  Yapf().ConnectNodeToCachedData(n, this->local_cache.emplace_back(key));
151  return false;
152  }
153 
154  bool found;
155  CachedData &item = this->global_cache.Get(key, &found);
156  Yapf().ConnectNodeToCachedData(n, item);
157  return found;
158  }
159 };
160 
161 #endif /* YAPF_COSTCACHE_HPP */
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Node::Key Key
key to hash tables
Tpf & Yapf()
to access inherited path finder
Types::NodeList::Item Node
this will be our node type
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
bool PfNodeCacheFetch(Node &)
Called by YAPF to attach cached or local segment cost data to the given node.
Types::NodeList::Item Node
this will be our node type
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
const Titem * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:180
void Push(Titem &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:238
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Definition: hashtable.hpp:173
Base class for segment cost cache providers.
static int s_rail_change_counter
if any track changes, this counter is incremented - that will invalidate segment cost cache
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures.
void Flush()
flush (clear) the cache
typename Tsegment::Key Key
key to hash table
Track
These are used to specify a single track.
Definition: track_type.h:19