OpenTTD: src/pathfinder/yapf/nodelist.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

nodelist.hpp
Go to the documentation of this file.
1 /* $Id: nodelist.hpp 23640 2011-12-20 17:57:56Z truebrain $ */
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 NODELIST_HPP
13 #define NODELIST_HPP
14
15 #include "../../misc/array.hpp"
16 #include "../../misc/hashtable.hpp"
17 #include "../../misc/binaryheap.hpp"
18
24 template <class Titem_, int Thash_bits_open_, int Thash_bits_closed_>
25 class CNodeList_HashTableT {
26 public:
28 typedef Titem_ Titem;
30 typedef typename Titem_::Key Key;
32 typedef SmallArray<Titem_, 65536, 256> CItemArray;
34 typedef CHashTableT<Titem_, Thash_bits_open_ > COpenList;
36 typedef CHashTableT<Titem_, Thash_bits_closed_> CClosedList;
38 typedef CBinaryHeapT<Titem_> CPriorityQueue;
39
40 protected:
42 CItemArray m_arr;
44 COpenList m_open;
46 CClosedList m_closed;
48 CPriorityQueue m_open_queue;
50 Titem *m_new_node;
51 public:
53 CNodeList_HashTableT()
54 : m_open_queue(2048)
55 {
56 m_new_node = NULL;
57 }
58
60 ~CNodeList_HashTableT()
61 {
62 }
63
65 inline int OpenCount()
66 {
67 return m_open.Count();
68 }
69
71 inline int ClosedCount()
72 {
73 return m_closed.Count();
74 }
75
77 inline Titem_ *CreateNewNode()
78 {
79 if (m_new_node == NULL) m_new_node = m_arr.AppendC();
80 return m_new_node;
81 }
82
84 inline void FoundBestNode(Titem_& item)
85 {
86 /* for now it is enough to invalidate m_new_node if it is our given node */
87 if (&item == m_new_node) {
88 m_new_node = NULL;
89 }
90 /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
91 }
92
94 inline void InsertOpenNode(Titem_& item)
95 {
96 assert(m_closed.Find(item.GetKey()) == NULL);
97 m_open.Push(item);
98 m_open_queue.Include(&item);
99 if (&item == m_new_node) {
100 m_new_node = NULL;
101 }
102 }
103
105 inline Titem_ *GetBestOpenNode()
106 {
107 if (!m_open_queue.IsEmpty()) {
108 return m_open_queue.Begin();
109 }
110 return NULL;
111 }
112
114 inline Titem_ *PopBestOpenNode()
115 {
116 if (!m_open_queue.IsEmpty()) {
117 Titem_ *item = m_open_queue.Shift();
118 m_open.Pop(*item);
119 return item;
120 }
121 return NULL;
122 }
123
125 inline Titem_ *FindOpenNode(const Key& key)
126 {
127 Titem_ *item = m_open.Find(key);
128 return item;
129 }
130
132 inline Titem_& PopOpenNode(const Key& key)
133 {
134 Titem_& item = m_open.Pop(key);
135 uint idxPop = m_open_queue.FindIndex(item);
136 m_open_queue.Remove(idxPop);
137 return item;
138 }
139
141 inline void InsertClosedNode(Titem_& item)
142 {
143 assert(m_open.Find(item.GetKey()) == NULL);
144 m_closed.Push(item);
145 }
146
148 inline Titem_ *FindClosedNode(const Key& key)
149 {
150 Titem_ *item = m_closed.Find(key);
151 return item;
152 }
153
155 inline int TotalCount() {return m_arr.Length();}
157 inline Titem_& ItemAt(int idx) {return m_arr[idx];}
158
160 template <class D> void Dump(D &dmp) const
161 {
162 dmp.WriteStructT("m_arr", &m_arr);
163 }
164 };
165
166 #endif /* NODELIST_HPP */