OpenTTD

yapf_node.hpp
Go to the documentation of this file.
1 /* $Id: yapf_node.hpp 27363 2015-08-08 13:19:38Z alberth $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef YAPF_NODE_HPP
13 #define YAPF_NODE_HPP
14
16 struct CYapfNodeKeyExitDir {
17 TileIndex m_tile;
18 Trackdir m_td;
19 DiagDirection m_exitdir;
20
21 inline void Set(TileIndex tile, Trackdir td)
22 {
23 m_tile = tile;
24 m_td = td;
25 m_exitdir = (m_td == INVALID_TRACKDIR) ? INVALID_DIAGDIR : TrackdirToExitdir(m_td);
26 }
27
28 inline int CalcHash() const
29 {
30 return m_exitdir | (m_tile << 2);
31 }
32
33 inline bool operator==(const CYapfNodeKeyExitDir &other) const
34 {
35 return m_tile == other.m_tile && m_exitdir == other.m_exitdir;
36 }
37
38 void Dump(DumpTarget &dmp) const
39 {
40 dmp.WriteTile("m_tile", m_tile);
41 dmp.WriteEnumT("m_td", m_td);
42 dmp.WriteEnumT("m_exitdir", m_exitdir);
43 }
44 };
45
46 struct CYapfNodeKeyTrackDir : public CYapfNodeKeyExitDir
47 {
48 inline int CalcHash() const
49 {
50 return m_td | (m_tile << 4);
51 }
52
53 inline bool operator==(const CYapfNodeKeyTrackDir &other) const
54 {
55 return m_tile == other.m_tile && m_td == other.m_td;
56 }
57 };
58
60 template <class Tkey_, class Tnode>
61 struct CYapfNodeT {
62 typedef Tkey_ Key;
63 typedef Tnode Node;
64
65 Tkey_ m_key;
66 Node *m_hash_next;
67 Node *m_parent;
68 int m_cost;
69 int m_estimate;
70
71 inline void Set(Node *parent, TileIndex tile, Trackdir td, bool is_choice)
72 {
73 m_key.Set(tile, td);
74 m_hash_next = NULL;
75 m_parent = parent;
76 m_cost = 0;
77 m_estimate = 0;
78 }
79
80 inline Node *GetHashNext()
81 {
82 return m_hash_next;
83 }
84
85 inline void SetHashNext(Node *pNext)
86 {
87 m_hash_next = pNext;
88 }
89
90 inline TileIndex GetTile() const
91 {
92 return m_key.m_tile;
93 }
94
95 inline Trackdir GetTrackdir() const
96 {
97 return m_key.m_td;
98 }
99
100 inline const Tkey_& GetKey() const
101 {
102 return m_key;
103 }
104
105 inline int GetCost() const
106 {
107 return m_cost;
108 }
109
110 inline int GetCostEstimate() const
111 {
112 return m_estimate;
113 }
114
115 inline bool operator<(const Node &other) const
116 {
117 return m_estimate < other.m_estimate;
118 }
119
120 void Dump(DumpTarget &dmp) const
121 {
122 dmp.WriteStructT("m_key", &m_key);
123 dmp.WriteStructT("m_parent", m_parent);
124 dmp.WriteLine("m_cost = %d", m_cost);
125 dmp.WriteLine("m_estimate = %d", m_estimate);
126 }
127 };
128
129 #endif /* YAPF_NODE_HPP */
INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:87
DumpTarget::WriteLine
void CDECL WriteLine(const char *format,...) WARN_FORMAT(2
Write a line with indent at the beginning and <LF> at the end.
Definition: dbg_helpers.cpp:121
DumpTarget::WriteEnumT
void WriteEnumT(const char *name, E e)
Dump given enum value (as a number and as named value)
Definition: dbg_helpers.h:145
DumpTarget::WriteTile
void WriteTile(const char *name, TileIndex t)
Write name & TileIndex to the output.
Definition: dbg_helpers.cpp:139
TrackdirToExitdir
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:427
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
operator==
bool operator==(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Compare two MultiMap iterators.
Definition: multimap.hpp:205
CYapfNodeKeyTrackDir
Definition: yapf_node.hpp:46
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:80
DumpTarget::WriteStructT
void WriteStructT(const char *name, const S *s)
Dump nested object (or only its name if this instance is already known).
Definition: dbg_helpers.h:154
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
CYapfNodeT
Yapf Node base.
Definition: yapf_node.hpp:61
CYapfNodeKeyExitDir
Yapf Node Key that evaluates hash from (and compares) tile & exit dir.
Definition: yapf_node.hpp:16
INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:93
DumpTarget
Class that represents the dump-into-string target.
Definition: dbg_helpers.h:96