OpenTTD Source  20240919-master-gdf0233f4c2
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 
20 template <class Types>
22 {
23 public:
24  typedef typename Types::Tpf Tpf;
25  typedef typename Types::NodeList::Titem Node;
26 
31  inline bool PfNodeCacheFetch(Node &)
32  {
33  return false;
34  }
35 };
36 
45 {
47 
48  static void NotifyTrackLayoutChange(TileIndex, Track)
49  {
51  }
52 };
53 
54 
65 template <class Tsegment>
67  static const int C_HASH_BITS = 14;
68 
70  using Heap = std::deque<Tsegment>;
71  typedef typename Tsegment::Key Key;
72 
73  HashTable m_map;
74  Heap m_heap;
75 
76  inline CSegmentCostCacheT() {}
77 
79  inline void Flush()
80  {
81  m_map.Clear();
82  m_heap.clear();
83  }
84 
85  inline Tsegment &Get(Key &key, bool *found)
86  {
87  Tsegment *item = m_map.Find(key);
88  if (item == nullptr) {
89  *found = false;
90  item = &m_heap.emplace_back(key);
91  m_map.Push(*item);
92  } else {
93  *found = true;
94  }
95  return *item;
96  }
97 };
98 
104 template <class Types>
106 public:
107  typedef typename Types::Tpf Tpf;
108  typedef typename Types::NodeList::Titem Node;
109  typedef typename Node::Key Key;
110  typedef typename Node::CachedData CachedData;
111  typedef typename CachedData::Key CacheKey;
113  using LocalCache = std::deque<CachedData>;
114 
115 protected:
116  Cache &m_global_cache;
117  LocalCache m_local_cache;
118 
119  inline CYapfSegmentCostCacheGlobalT() : m_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 
140 public:
145  inline bool PfNodeCacheFetch(Node &n)
146  {
147  CacheKey key(n.GetKey());
148 
149  if (!Yapf().CanUseGlobalCache(n)) {
150  Yapf().ConnectNodeToCachedData(n, m_local_cache.emplace_back(key));
151  return false;
152  }
153 
154  bool found;
155  CachedData &item = m_global_cache.Get(key, &found);
156  Yapf().ConnectNodeToCachedData(n, item);
157  return found;
158  }
159 };
160 
161 #endif /* YAPF_COSTCACHE_HPP */
CYapfSegmentCostCacheNoneT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_costcache.hpp:25
CYapfSegmentCostCacheGlobalT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_costcache.hpp:107
CYapfSegmentCostCacheGlobalT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_costcache.hpp:122
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
CSegmentCostCacheT::Flush
void Flush()
flush (clear) the cache
Definition: yapf_costcache.hpp:79
CYapfSegmentCostCacheNoneT::PfNodeCacheFetch
bool PfNodeCacheFetch(Node &)
Called by YAPF to attach cached or local segment cost data to the given node.
Definition: yapf_costcache.hpp:31
CHashTableT::Find
const Titem_ * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:189
CSegmentCostCacheT::Key
Tsegment::Key Key
key to hash table
Definition: yapf_costcache.hpp:71
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
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Definition: yapf_costcache.hpp:105
CSegmentCostCacheT
CSegmentCostCacheT - template class providing hash-map and storage (heap) of Tsegment structures.
Definition: yapf_costcache.hpp:66
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:109
CYapfSegmentCostCacheGlobalT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_costcache.hpp:108
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
CYapfSegmentCostCacheNoneT
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Definition: yapf_costcache.hpp:21
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
CSegmentCostCacheBase
Base class for segment cost cache providers.
Definition: yapf_costcache.hpp:44
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 >