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

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 {
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
45 struct CSegmentCostCacheBase {
46 static int s_rail_change_counter;
47
48 static void NotifyTrackLayoutChange(TileIndex, Track)
49 {
50 s_rail_change_counter++;
51 }
52};
53
54
65template <class Tsegment>
66 struct CSegmentCostCacheT : public CSegmentCostCacheBase {
67 static constexpr int HASH_BITS = 14;
68
69 using Key = typename Tsegment::Key;
70
71 HashTable<Tsegment, HASH_BITS> map;
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>
103 class CYapfSegmentCostCacheGlobalT {
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;
110 typedef CSegmentCostCacheT<CachedData> Cache;
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
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Definition yapf_costcache.hpp:103
CYapfSegmentCostCacheGlobalT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_costcache.hpp:120
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:143
CYapfSegmentCostCacheGlobalT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_costcache.hpp:105
CYapfSegmentCostCacheGlobalT::Key
Node::Key Key
key to hash tables
Definition yapf_costcache.hpp:107
CYapfSegmentCostCacheGlobalT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_costcache.hpp:106
CYapfSegmentCostCacheNoneT
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Definition yapf_costcache.hpp:23
CYapfSegmentCostCacheNoneT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition yapf_costcache.hpp:32
CYapfSegmentCostCacheNoneT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_costcache.hpp:26
CYapfSegmentCostCacheNoneT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_costcache.hpp:25
HashTable
class HashTable<Titem, HASH_BITS> - simple hash table of pointers allocated elsewhere.
Definition hashtable.hpp:131
HashTable::Push
void Push(Titem &new_item)
add one item - copy it from the given item
Definition hashtable.hpp:236
HashTable::Find
const Titem * Find(const Tkey &key) const
const item search
Definition hashtable.hpp:178
HashTable::Clear
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Definition hashtable.hpp:171
CSegmentCostCacheBase
Base class for segment cost cache providers.
Definition yapf_costcache.hpp:45
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:46
CSegmentCostCacheT
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures.
Definition yapf_costcache.hpp:66
CSegmentCostCacheT::Flush
void Flush()
flush (clear) the cache
Definition yapf_costcache.hpp:77
CSegmentCostCacheT::Key
typename Tsegment::Key Key
key to hash table
Definition yapf_costcache.hpp:69
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