OpenTTD Source  20241121-master-g67a0fccfad
yapf_node.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_NODE_HPP
11 #define YAPF_NODE_HPP
12 
13 #include "../../track_func.h"
14 #include "../../misc/dbg_helpers.h"
15 
18  TileIndex tile;
19  Trackdir td;
20  DiagDirection exitdir;
21 
22  inline void Set(TileIndex tile, Trackdir td)
23  {
24  this->tile = tile;
25  this->td = td;
26  this->exitdir = (this->td == INVALID_TRACKDIR) ? INVALID_DIAGDIR : TrackdirToExitdir(this->td);
27  }
28 
29  inline int CalcHash() const
30  {
31  return this->exitdir | (this->tile.base() << 2);
32  }
33 
34  inline bool operator==(const CYapfNodeKeyExitDir &other) const
35  {
36  return this->tile == other.tile && this->exitdir == other.exitdir;
37  }
38 
39  void Dump(DumpTarget &dmp) const
40  {
41  dmp.WriteTile("tile", this->tile);
42  dmp.WriteEnumT("td", this->td);
43  dmp.WriteEnumT("exitdir", this->exitdir);
44  }
45 };
46 
48 {
49  inline int CalcHash() const
50  {
51  return this->td | (this->tile.base() << 4);
52  }
53 
54  inline bool operator==(const CYapfNodeKeyTrackDir &other) const
55  {
56  return this->tile == other.tile && this->td == other.td;
57  }
58 };
59 
61 template <class Tkey_, class Tnode>
62 struct CYapfNodeT {
63  typedef Tkey_ Key;
64  typedef Tnode Node;
65 
66  Tkey_ key;
67  Node *hash_next;
68  Node *parent;
69  int cost;
70  int estimate;
71  bool is_choice;
72 
73  inline void Set(Node *parent, TileIndex tile, Trackdir td, bool is_choice)
74  {
75  this->key.Set(tile, td);
76  this->hash_next = nullptr;
77  this->parent = parent;
78  this->cost = 0;
79  this->estimate = 0;
80  this->is_choice = is_choice;
81  }
82 
83  inline Node *GetHashNext()
84  {
85  return this->hash_next;
86  }
87 
88  inline void SetHashNext(Node *pNext)
89  {
90  this->hash_next = pNext;
91  }
92 
93  inline TileIndex GetTile() const
94  {
95  return this->key.tile;
96  }
97 
98  inline Trackdir GetTrackdir() const
99  {
100  return this->key.td;
101  }
102 
103  inline const Tkey_ &GetKey() const
104  {
105  return this->key;
106  }
107 
108  inline int GetCost() const
109  {
110  return this->cost;
111  }
112 
113  inline int GetCostEstimate() const
114  {
115  return this->estimate;
116  }
117 
118  inline bool GetIsChoice() const
119  {
120  return this->is_choice;
121  }
122 
123  inline bool operator<(const Node &other) const
124  {
125  return this->estimate < other.estimate;
126  }
127 
128  void Dump(DumpTarget &dmp) const
129  {
130  dmp.WriteStructT("key", &this->key);
131  dmp.WriteStructT("parent", this->parent);
132  dmp.WriteValue("cost", this->cost);
133  dmp.WriteValue("estimate", this->estimate);
134  }
135 };
136 
137 #endif /* YAPF_NODE_HPP */
DiagDirection
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Yapf Node Key that evaluates hash from (and compares) tile & exit dir.
Definition: yapf_node.hpp:17
Yapf Node base.
Definition: yapf_node.hpp:62
Class that represents the dump-into-string target.
Definition: dbg_helpers.h:95
void WriteEnumT(const std::string &name, E e)
Dump given enum value (as a number and as named value)
Definition: dbg_helpers.h:138
void WriteValue(const std::string &name, int value)
Write 'name = value' with indent and new-line.
void WriteTile(const std::string &name, TileIndex t)
Write name & TileIndex to the output.
void WriteStructT(const std::string &name, const S *s)
Dump nested object (or only its name if this instance is already known).
Definition: dbg_helpers.h:147
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:439
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:86