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/>.
8 /** @file nodelist.hpp List of nodes used for the A-star pathfinder. */
13 #include "../../misc/array.hpp"
14 #include "../../misc/hashtable.hpp"
15 #include "../../misc/binaryheap.hpp"
18 * Hash table based node list multi-container class.
19 * Implements open list, closed list and priority queue for A-star
22 template <class Titem_
, int Thash_bits_open_
, int Thash_bits_closed_
>
23 class CNodeList_HashTableT
{
25 typedef Titem_ Titem
; ///< Make #Titem_ visible from outside of class.
26 typedef typename
Titem_::Key Key
; ///< Make Titem_::Key a property of this class.
27 typedef SmallArray
<Titem_
, 65536, 256> CItemArray
; ///< Type that we will use as item container.
28 typedef CHashTableT
<Titem_
, Thash_bits_open_
> COpenList
; ///< How pointers to open nodes will be stored.
29 typedef CHashTableT
<Titem_
, Thash_bits_closed_
> CClosedList
; ///< How pointers to closed nodes will be stored.
30 typedef CBinaryHeapT
<Titem_
> CPriorityQueue
; ///< How the priority queue will be managed.
33 CItemArray m_arr
; ///< Here we store full item data (Titem_).
34 COpenList m_open
; ///< Hash table of pointers to open item data.
35 CClosedList m_closed
; ///< Hash table of pointers to closed item data.
36 CPriorityQueue m_open_queue
; ///< Priority queue of pointers to open item data.
37 Titem
*m_new_node
; ///< New open node under construction.
40 /** default constructor */
41 CNodeList_HashTableT() : m_open_queue(2048)
47 ~CNodeList_HashTableT()
51 /** return number of open nodes */
52 inline int OpenCount()
54 return m_open
.Count();
57 /** return number of closed nodes */
58 inline int ClosedCount()
60 return m_closed
.Count();
63 /** allocate new data item from m_arr */
64 inline Titem_
*CreateNewNode()
66 if (m_new_node
== nullptr) m_new_node
= m_arr
.AppendC();
70 /** Notify the nodelist that we don't want to discard the given node. */
71 inline void FoundBestNode(Titem_
&item
)
73 /* for now it is enough to invalidate m_new_node if it is our given node */
74 if (&item
== m_new_node
) {
77 /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
80 /** insert given item as open node (into m_open and m_open_queue) */
81 inline void InsertOpenNode(Titem_
&item
)
83 assert(m_closed
.Find(item
.GetKey()) == nullptr);
85 m_open_queue
.Include(&item
);
86 if (&item
== m_new_node
) {
91 /** return the best open node */
92 inline Titem_
*GetBestOpenNode()
94 if (!m_open_queue
.IsEmpty()) {
95 return m_open_queue
.Begin();
100 /** remove and return the best open node */
101 inline Titem_
*PopBestOpenNode()
103 if (!m_open_queue
.IsEmpty()) {
104 Titem_
*item
= m_open_queue
.Shift();
111 /** return the open node specified by a key or nullptr if not found */
112 inline Titem_
*FindOpenNode(const Key
&key
)
114 Titem_
*item
= m_open
.Find(key
);
118 /** remove and return the open node specified by a key */
119 inline Titem_
& PopOpenNode(const Key
&key
)
121 Titem_
&item
= m_open
.Pop(key
);
122 uint idxPop
= m_open_queue
.FindIndex(item
);
123 m_open_queue
.Remove(idxPop
);
128 inline void InsertClosedNode(Titem_
&item
)
130 assert(m_open
.Find(item
.GetKey()) == nullptr);
134 /** return the closed node specified by a key or nullptr if not found */
135 inline Titem_
*FindClosedNode(const Key
&key
)
137 Titem_
*item
= m_closed
.Find(key
);
141 /** The number of items. */
142 inline int TotalCount()
144 return m_arr
.Length();
147 /** Get a particular item. */
148 inline Titem_
& ItemAt(int idx
)
153 /** Helper for creating output of this array. */
154 template <class D
> void Dump(D
&dmp
) const
156 dmp
.WriteStructT("m_arr", &m_arr
);
160 #endif /* NODELIST_HPP */