OpenTTD Source  20241108-master-g80f628063a
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 "../../timer/timer_game_calendar.h"
14 #include "../../misc/hashtable.hpp"
15 #include "../../tile_type.h"
16 #include "../../track_type.h"
17 
23 template <class Types>
25 {
26 public:
27  typedef typename Types::Tpf Tpf;
28  typedef typename Types::NodeList::Titem Node;
29 
34  inline bool PfNodeCacheFetch(Node &)
35  {
36  return false;
37  }
38 };
39 
48 {
50 
51  static void NotifyTrackLayoutChange(TileIndex, Track)
52  {
54  }
55 };
56 
57 
68 template <class Tsegment>
70  static constexpr int HASH_BITS = 14;
71 
72  using Key = typename Tsegment::Key;
73 
75  std::deque<Tsegment> heap;
76 
77  inline CSegmentCostCacheT() {}
78 
80  inline void Flush()
81  {
82  this->map.Clear();
83  this->heap.clear();
84  }
85 
86  inline Tsegment &Get(Key &key, bool *found)
87  {
88  Tsegment *item = this->map.Find(key);
89  if (item == nullptr) {
90  *found = false;
91  item = &this->heap.emplace_back(key);
92  this->map.Push(*item);
93  } else {
94  *found = true;
95  }
96  return *item;
97  }
98 };
99 
105 template <class Types>
107 public:
108  typedef typename Types::Tpf Tpf;
109  typedef typename Types::NodeList::Titem Node;
110  typedef typename Node::Key Key;
111  typedef typename Node::CachedData CachedData;
112  typedef typename CachedData::Key CacheKey;
114  using LocalCache = std::deque<CachedData>;
115 
116 protected:
117  Cache &global_cache;
118  LocalCache local_cache;
119 
120  inline CYapfSegmentCostCacheGlobalT() : global_cache(stGetGlobalCache()) {};
121 
123  inline Tpf &Yapf()
124  {
125  return *static_cast<Tpf *>(this);
126  }
127 
128  inline static Cache &stGetGlobalCache()
129  {
130  static int last_rail_change_counter = 0;
131  static Cache C;
132 
133  /* delete the cache sometimes... */
134  if (last_rail_change_counter != Cache::s_rail_change_counter) {
135  last_rail_change_counter = Cache::s_rail_change_counter;
136  C.Flush();
137  }
138  return C;
139  }
140 
141 public:
146  inline bool PfNodeCacheFetch(Node &n)
147  {
148  CacheKey key(n.GetKey());
149 
150  if (!Yapf().CanUseGlobalCache(n)) {
151  Yapf().ConnectNodeToCachedData(n, this->local_cache.emplace_back(key));
152  return false;
153  }
154 
155  bool found;
156  CachedData &item = this->global_cache.Get(key, &found);
157  Yapf().ConnectNodeToCachedData(n, item);
158  return found;
159  }
160 };
161 
162 #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::Titem 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::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::NodeList::Titem Node
this will be our node type
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