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

OpenTTD Source 15.0-beta1

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>
23 class CYapfSegmentCostCacheNoneT
24{
25public:
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
46 struct CSegmentCostCacheBase
47{
48 static int s_rail_change_counter;
49
50 static void NotifyTrackLayoutChange(TileIndex, Track)
51 {
52 s_rail_change_counter++;
53 }
54};
55
56
67template <class Tsegment>
68 struct CSegmentCostCacheT : public CSegmentCostCacheBase {
69 static constexpr int HASH_BITS = 14;
70
71 using Key = typename Tsegment::Key;
72
73 HashTable<Tsegment, HASH_BITS> map;
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
104template <class Types>
105 class CYapfSegmentCostCacheGlobalT {
106public:
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;
112 typedef CSegmentCostCacheT<CachedData> Cache;
113 using LocalCache = std::deque<CachedData>;
114
115protected:
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
140public:
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
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Definition yapf_costcache.hpp:105
CYapfSegmentCostCacheGlobalT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_costcache.hpp:122
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:145
CYapfSegmentCostCacheGlobalT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_costcache.hpp:107
CYapfSegmentCostCacheGlobalT::Key
Node::Key Key
key to hash tables
Definition yapf_costcache.hpp:109
CYapfSegmentCostCacheGlobalT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_costcache.hpp:108
CYapfSegmentCostCacheNoneT
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Definition yapf_costcache.hpp:24
CYapfSegmentCostCacheNoneT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition yapf_costcache.hpp:33
CYapfSegmentCostCacheNoneT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_costcache.hpp:27
CYapfSegmentCostCacheNoneT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_costcache.hpp:26
HashTable
class HashTable<Titem, HASH_BITS> - simple hash table of pointers allocated elsewhere.
Definition hashtable.hpp:133
HashTable::Push
void Push(Titem &new_item)
add one item - copy it from the given item
Definition hashtable.hpp:238
HashTable::Find
const Titem * Find(const Tkey &key) const
const item search
Definition hashtable.hpp:180
HashTable::Clear
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Definition hashtable.hpp:173
CSegmentCostCacheBase
Base class for segment cost cache providers.
Definition yapf_costcache.hpp:47
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:48
CSegmentCostCacheT
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures.
Definition yapf_costcache.hpp:68
CSegmentCostCacheT::Flush
void Flush()
flush (clear) the cache
Definition yapf_costcache.hpp:79
CSegmentCostCacheT::Key
typename Tsegment::Key Key
key to hash table
Definition yapf_costcache.hpp:71
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Track
Track
These are used to specify a single track.
Definition track_type.h:19