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/>.
10 /** @file nodelist.hpp List of nodes used for the A-star pathfinder. */
15 #include "../../misc/array.hpp"
16 #include "../../misc/hashtable.hpp"
17 #include "../../misc/binaryheap.hpp"
20 * Hash table based node list multi-container class.
21 * Implements open list, closed list and priority queue for A-star
24 template <class Titem_
, int Thash_bits_open_
, int Thash_bits_closed_
>
25 class CNodeList_HashTableT
{
27 typedef Titem_ Titem
; ///< Make #Titem_ visible from outside of class.
28 typedef typename
Titem_::Key Key
; ///< Make Titem_::Key a property of #HashTable.
29 typedef SmallArray
<Titem_
, 65536, 256> CItemArray
; ///< Type that we will use as item container.
30 typedef CHashTableT
<Titem_
, Thash_bits_open_
> COpenList
; ///< How pointers to open nodes will be stored.
31 typedef CHashTableT
<Titem_
, Thash_bits_closed_
> CClosedList
; ///< How pointers to closed nodes will be stored.
32 typedef CBinaryHeapT
<Titem_
> CPriorityQueue
; ///< How the priority queue will be managed.
35 CItemArray m_arr
; ///< Here we store full item data (Titem_).
36 COpenList m_open
; ///< Hash table of pointers to open item data.
37 CClosedList m_closed
; ///< Hash table of pointers to closed item data.
38 CPriorityQueue m_open_queue
; ///< Priority queue of pointers to open item data.
39 Titem
*m_new_node
; ///< New open node under construction.
42 /** default constructor */
43 CNodeList_HashTableT() : m_open_queue(2048)
49 ~CNodeList_HashTableT()
53 /** return number of open nodes */
54 inline int OpenCount()
56 return m_open
.Count();
59 /** return number of closed nodes */
60 inline int ClosedCount()
62 return m_closed
.Count();
65 /** allocate new data item from m_arr */
66 inline Titem_
*CreateNewNode()
68 if (m_new_node
== NULL
) m_new_node
= m_arr
.AppendC();
72 /** Notify the nodelist that we don't want to discard the given node. */
73 inline void FoundBestNode(Titem_
&item
)
75 /* for now it is enough to invalidate m_new_node if it is our given node */
76 if (&item
== m_new_node
) {
79 /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
82 /** insert given item as open node (into m_open and m_open_queue) */
83 inline void InsertOpenNode(Titem_
&item
)
85 assert(m_closed
.Find(item
.GetKey()) == NULL
);
87 m_open_queue
.Include(&item
);
88 if (&item
== m_new_node
) {
93 /** return the best open node */
94 inline Titem_
*GetBestOpenNode()
96 if (!m_open_queue
.IsEmpty()) {
97 return m_open_queue
.Begin();
102 /** remove and return the best open node */
103 inline Titem_
*PopBestOpenNode()
105 if (!m_open_queue
.IsEmpty()) {
106 Titem_
*item
= m_open_queue
.Shift();
113 /** return the open node specified by a key or NULL if not found */
114 inline Titem_
*FindOpenNode(const Key
&key
)
116 Titem_
*item
= m_open
.Find(key
);
120 /** remove and return the open node specified by a key */
121 inline Titem_
& PopOpenNode(const Key
&key
)
123 Titem_
&item
= m_open
.Pop(key
);
124 uint idxPop
= m_open_queue
.FindIndex(item
);
125 m_open_queue
.Remove(idxPop
);
130 inline void InsertClosedNode(Titem_
&item
)
132 assert(m_open
.Find(item
.GetKey()) == NULL
);
136 /** return the closed node specified by a key or NULL if not found */
137 inline Titem_
*FindClosedNode(const Key
&key
)
139 Titem_
*item
= m_closed
.Find(key
);
143 /** The number of items. */
144 inline int TotalCount()
146 return m_arr
.Length();
149 /** Get a particular item. */
150 inline Titem_
& ItemAt(int idx
)
155 /** Helper for creating output of this array. */
156 template <class D
> void Dump(D
&dmp
) const
158 dmp
.WriteStructT("m_arr", &m_arr
);
162 #endif /* NODELIST_HPP */