OpenTTD Source: src/pathfinder/yapf/yapf_costcache.hpp Source File

OpenTTD Source 12.2

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>
21 class CYapfSegmentCostCacheNoneT
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>
52 class CYapfSegmentCostCacheLocalT
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;
60 typedef SmallArray<CachedData> LocalCache;
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
100 struct CSegmentCostCacheBase
101 {
102 static int s_rail_change_counter;
103
104 static void NotifyTrackLayoutChange(TileIndex tile, Track track)
105 {
106 s_rail_change_counter++;
107 }
108 };
109
110
121 template <class Tsegment>
122 struct CSegmentCostCacheT : public CSegmentCostCacheBase {
123 static const int C_HASH_BITS = 14;
124
125 typedef CHashTableT<Tsegment, C_HASH_BITS> HashTable;
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>
161 class CYapfSegmentCostCacheGlobalT : public CYapfSegmentCostCacheLocalT<Types> {
162 public:
163 typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
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;
169 typedef CSegmentCostCacheT<CachedData> Cache;
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 Cache C;
186
187 /* delete the cache sometimes... */
188 if (last_rail_change_counter != Cache::s_rail_change_counter) {
189 last_rail_change_counter = Cache::s_rail_change_counter;
190 C.Flush();
191 }
192 return C;
193 }
194
195 public:
200 inline bool PfNodeCacheFetch(Node &n)
201 {
202 if (!Yapf().CanUseGlobalCache(n)) {
203 return Tlocal::PfNodeCacheFetch(n);
204 }
205 CacheKey key(n.GetKey());
206 bool found;
207 CachedData &item = m_global_cache.Get(key, &found);
208 Yapf().ConnectNodeToCachedData(n, item);
209 return found;
210 }
211
216 inline void PfNodeCacheFlush(Node &n)
217 {
218 }
219 };
220
221 #endif /* YAPF_COSTCACHE_HPP */
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
CYapfSegmentCostCacheLocalT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_costcache.hpp:55
CYapfSegmentCostCacheNoneT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_costcache.hpp:25
SmallArray::Append
T * Append()
allocate but not construct new item
Definition: array.hpp:74
CYapfSegmentCostCacheGlobalT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_costcache.hpp:164
CYapfSegmentCostCacheGlobalT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_costcache.hpp:177
CYapfSegmentCostCacheLocalT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_costcache.hpp:56
CSegmentCostCacheT::Flush
void Flush()
flush (clear) the cache
Definition: yapf_costcache.hpp:135
CHashTableT::Find
const Titem_ * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:189
SmallArray::Clear
void Clear()
Clear (destroy) all items.
Definition: array.hpp:48
CYapfSegmentCostCacheNoneT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition: yapf_costcache.hpp:31
CSegmentCostCacheT::Key
Tsegment::Key Key
key to hash table
Definition: yapf_costcache.hpp:127
CYapfSegmentCostCacheGlobalT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition: yapf_costcache.hpp:200
CYapfSegmentCostCacheGlobalT
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Definition: yapf_costcache.hpp:161
CYapfSegmentCostCacheLocalT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &n)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition: yapf_costcache.hpp:76
CSegmentCostCacheT
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures.
Definition: yapf_costcache.hpp:122
CHashTableT::Clear
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Definition: hashtable.hpp:183
CYapfSegmentCostCacheGlobalT::Key
Node::Key Key
key to hash tables
Definition: yapf_costcache.hpp:166
CYapfSegmentCostCacheGlobalT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_costcache.hpp:165
CSegmentCostCacheBase::s_rail_change_counter
static int s_rail_change_counter
if any track changes, this counter is incremented - that will invalidate segment cost cache
Definition: yapf_costcache.hpp:102
CYapfSegmentCostCacheNoneT
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Definition: yapf_costcache.hpp:21
SmallArray< CachedData >
CSegmentCostCacheBase
Base class for segment cost cache providers.
Definition: yapf_costcache.hpp:100
CYapfSegmentCostCacheLocalT::Key
Node::Key Key
key to hash tables
Definition: yapf_costcache.hpp:57
CYapfSegmentCostCacheNoneT::PfNodeCacheFlush
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
Definition: yapf_costcache.hpp:40
CYapfSegmentCostCacheLocalT
CYapfSegmentCostCacheLocalT - the yapf cost cache provider that implements fake segment cost caching ...
Definition: yapf_costcache.hpp:52
CYapfSegmentCostCacheLocalT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_costcache.hpp:66
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
CYapfSegmentCostCacheLocalT::PfNodeCacheFlush
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
Definition: yapf_costcache.hpp:87
CYapfSegmentCostCacheGlobalT::PfNodeCacheFlush
void PfNodeCacheFlush(Node &n)
Called by YAPF to flush the cached segment cost data back into cache storage.
Definition: yapf_costcache.hpp:216
CHashTableT::Push
void Push(Titem_ &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:247
CYapfSegmentCostCacheNoneT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_costcache.hpp:24
CHashTableT< Tsegment, C_HASH_BITS >