OpenTTD Source 20250829-master-g5c285f3e0c
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
22template <class Types>
24public:
25 typedef typename Types::Tpf Tpf;
26 typedef typename Types::NodeList::Item Node;
27
32 inline bool PfNodeCacheFetch(Node &)
33 {
34 return false;
35 }
36};
37
47
48 static void NotifyTrackLayoutChange(TileIndex, Track)
49 {
51 }
52};
53
54
65template <class Tsegment>
67 static constexpr int HASH_BITS = 14;
68
69 using Key = typename Tsegment::Key;
70
72 std::deque<Tsegment> heap;
73
74 inline CSegmentCostCacheT() {}
75
77 inline void Flush()
78 {
79 this->map.Clear();
80 this->heap.clear();
81 }
82
83 inline Tsegment &Get(Key &key, bool *found)
84 {
85 Tsegment *item = this->map.Find(key);
86 if (item == nullptr) {
87 *found = false;
88 item = &this->heap.emplace_back(key);
89 this->map.Push(*item);
90 } else {
91 *found = true;
92 }
93 return *item;
94 }
95};
96
102template <class Types>
104public:
105 typedef typename Types::Tpf Tpf;
106 typedef typename Types::NodeList::Item Node;
107 typedef typename Node::Key Key;
108 typedef typename Node::CachedData CachedData;
109 typedef typename CachedData::Key CacheKey;
111 using LocalCache = std::deque<CachedData>;
112
113protected:
114 Cache &global_cache;
115 LocalCache local_cache;
116
117 inline CYapfSegmentCostCacheGlobalT() : global_cache(stGetGlobalCache()) {};
118
120 inline Tpf &Yapf()
121 {
122 return *static_cast<Tpf *>(this);
123 }
124
125 static inline Cache &stGetGlobalCache()
126 {
127 static int last_rail_change_counter = 0;
128 static Cache C;
129
130 /* delete the cache sometimes... */
131 if (last_rail_change_counter != Cache::s_rail_change_counter) {
132 last_rail_change_counter = Cache::s_rail_change_counter;
133 C.Flush();
134 }
135 return C;
136 }
137
138public:
143 inline bool PfNodeCacheFetch(Node &n)
144 {
145 CacheKey key(n.GetKey());
146
147 if (!Yapf().CanUseGlobalCache(n)) {
148 Yapf().ConnectNodeToCachedData(n, this->local_cache.emplace_back(key));
149 return false;
150 }
151
152 bool found;
153 CachedData &item = this->global_cache.Get(key, &found);
154 Yapf().ConnectNodeToCachedData(n, item);
155 return found;
156 }
157};
158
159#endif /* YAPF_COSTCACHE_HPP */
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Tpf & Yapf()
to access inherited path finder
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
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)
class HashTable<Titem, HASH_BITS> - simple hash table of pointers allocated elsewhere.
void Push(Titem &new_item)
add one item - copy it from the given item
const Titem * Find(const Tkey &key) const
const item search
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
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