OpenTTD

nodelist.hpp
Go to the documentation of this file.
1 /* $Id: nodelist.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 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:
27 typedef Titem_ Titem;
28 typedef typename Titem_::Key Key;
29 typedef SmallArray<Titem_, 65536, 256> CItemArray;
30 typedef CHashTableT<Titem_, Thash_bits_open_ > COpenList;
31 typedef CHashTableT<Titem_, Thash_bits_closed_> CClosedList;
32 typedef CBinaryHeapT<Titem_> CPriorityQueue;
33
34 protected:
35 CItemArray m_arr;
36 COpenList m_open;
37 CClosedList m_closed;
38 CPriorityQueue m_open_queue;
39 Titem *m_new_node;
40
41 public:
43 CNodeList_HashTableT() : m_open_queue(2048)
44 {
45 m_new_node = NULL;
46 }
47
49 ~CNodeList_HashTableT()
50 {
51 }
52
54 inline int OpenCount()
55 {
56 return m_open.Count();
57 }
58
60 inline int ClosedCount()
61 {
62 return m_closed.Count();
63 }
64
66 inline Titem_ *CreateNewNode()
67 {
68 if (m_new_node == NULL) m_new_node = m_arr.AppendC();
69 return m_new_node;
70 }
71
73 inline void FoundBestNode(Titem_ &item)
74 {
75 /* for now it is enough to invalidate m_new_node if it is our given node */
76 if (&item == m_new_node) {
77 m_new_node = NULL;
78 }
79 /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
80 }
81
83 inline void InsertOpenNode(Titem_ &item)
84 {
85 assert(m_closed.Find(item.GetKey()) == NULL);
86 m_open.Push(item);
87 m_open_queue.Include(&item);
88 if (&item == m_new_node) {
89 m_new_node = NULL;
90 }
91 }
92
94 inline Titem_ *GetBestOpenNode()
95 {
96 if (!m_open_queue.IsEmpty()) {
97 return m_open_queue.Begin();
98 }
99 return NULL;
100 }
101
103 inline Titem_ *PopBestOpenNode()
104 {
105 if (!m_open_queue.IsEmpty()) {
106 Titem_ *item = m_open_queue.Shift();
107 m_open.Pop(*item);
108 return item;
109 }
110 return NULL;
111 }
112
114 inline Titem_ *FindOpenNode(const Key &key)
115 {
116 Titem_ *item = m_open.Find(key);
117 return item;
118 }
119
121 inline Titem_& PopOpenNode(const Key &key)
122 {
123 Titem_ &item = m_open.Pop(key);
124 uint idxPop = m_open_queue.FindIndex(item);
125 m_open_queue.Remove(idxPop);
126 return item;
127 }
128
130 inline void InsertClosedNode(Titem_ &item)
131 {
132 assert(m_open.Find(item.GetKey()) == NULL);
133 m_closed.Push(item);
134 }
135
137 inline Titem_ *FindClosedNode(const Key &key)
138 {
139 Titem_ *item = m_closed.Find(key);
140 return item;
141 }
142
144 inline int TotalCount()
145 {
146 return m_arr.Length();
147 }
148
150 inline Titem_& ItemAt(int idx)
151 {
152 return m_arr[idx];
153 }
154
156 template <class D> void Dump(D &dmp) const
157 {
158 dmp.WriteStructT("m_arr", &m_arr);
159 }
160 };
161
162 #endif /* NODELIST_HPP */
CNodeList_HashTableT::m_closed
CClosedList m_closed
Hash table of pointers to closed item data.
Definition: nodelist.hpp:37
CNodeList_HashTableT::ClosedCount
int ClosedCount()
return number of closed nodes
Definition: nodelist.hpp:60
CBinaryHeapT::Begin
T * Begin()
Get the smallest item in the binary tree.
Definition: binaryheap.hpp:190
CHashTableT< Titem_, Thash_bits_open_ >
CHashTableT::Push
void Push(Titem_ &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:251
CNodeList_HashTableT::m_arr
CItemArray m_arr
Here we store full item data (Titem_).
Definition: nodelist.hpp:35
CNodeList_HashTableT::OpenCount
int OpenCount()
return number of open nodes
Definition: nodelist.hpp:54
CNodeList_HashTableT::CPriorityQueue
CBinaryHeapT< Titem_ > CPriorityQueue
How the priority queue will be managed.
Definition: nodelist.hpp:32
CNodeList_HashTableT::CreateNewNode
Titem_ * CreateNewNode()
allocate new data item from m_arr
Definition: nodelist.hpp:66
CHashTableT::Pop
Titem_ & Pop(const Tkey &key)
non-const item search & removal
Definition: hashtable.hpp:223
CNodeList_HashTableT::m_new_node
Titem * m_new_node
New open node under construction.
Definition: nodelist.hpp:39
CBinaryHeapT::IsEmpty
bool IsEmpty() const
Test if the priority queue is empty.
Definition: binaryheap.hpp:170
CNodeList_HashTableT::m_open
COpenList m_open
Hash table of pointers to open item data.
Definition: nodelist.hpp:36
CNodeList_HashTableT::CClosedList
CHashTableT< Titem_, Thash_bits_closed_ > CClosedList
How pointers to closed nodes will be stored.
Definition: nodelist.hpp:31
CNodeList_HashTableT::GetBestOpenNode
Titem_ * GetBestOpenNode()
return the best open node
Definition: nodelist.hpp:94
CNodeList_HashTableT::CItemArray
SmallArray< Titem_, 65536, 256 > CItemArray
Type that we will use as item container.
Definition: nodelist.hpp:29
CNodeList_HashTableT::Key
Titem_::Key Key
Make Titem_::Key a property of #HashTable.
Definition: nodelist.hpp:28
CNodeList_HashTableT::PopBestOpenNode
Titem_ * PopBestOpenNode()
remove and return the best open node
Definition: nodelist.hpp:103
CHashTableT::Find
const Titem_ * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:193
SmallArray::Length
uint Length() const
Return actual number of items.
Definition: array.hpp:56
SmallArray< Titem_, 65536, 256 >
CNodeList_HashTableT::Titem
Titem_ Titem
Make #Titem_ visible from outside of class.
Definition: nodelist.hpp:27
CNodeList_HashTableT::Dump
void Dump(D &dmp) const
Helper for creating output of this array.
Definition: nodelist.hpp:156
SmallArray::AppendC
T * AppendC()
allocate and construct new item
Definition: array.hpp:82
CNodeList_HashTableT
Hash table based node list multi-container class.
Definition: nodelist.hpp:25
CNodeList_HashTableT::COpenList
CHashTableT< Titem_, Thash_bits_open_ > COpenList
How pointers to open nodes will be stored.
Definition: nodelist.hpp:30
CHashTableT::Count
int Count() const
item count
Definition: hashtable.hpp:181
CBinaryHeapT::FindIndex
uint FindIndex(const T &item) const
Search for an item in the priority queue.
Definition: binaryheap.hpp:284
CBinaryHeapT::Shift
T * Shift()
Remove and return the smallest (and also first) item from the priority queue.
Definition: binaryheap.hpp:234
CNodeList_HashTableT::~CNodeList_HashTableT
~CNodeList_HashTableT()
destructor
Definition: nodelist.hpp:49
CBinaryHeapT< Titem_ >
CNodeList_HashTableT::FoundBestNode
void FoundBestNode(Titem_ &item)
Notify the nodelist that we don&#39;t want to discard the given node.
Definition: nodelist.hpp:73
CBinaryHeapT::Include
void Include(T *new_item)
Insert new item into the priority queue, maintaining heap order.
Definition: binaryheap.hpp:213
CNodeList_HashTableT::TotalCount
int TotalCount()
The number of items.
Definition: nodelist.hpp:144
CNodeList_HashTableT::ItemAt
Titem_ & ItemAt(int idx)
Get a particular item.
Definition: nodelist.hpp:150
CNodeList_HashTableT::m_open_queue
CPriorityQueue m_open_queue
Priority queue of pointers to open item data.
Definition: nodelist.hpp:38
CNodeList_HashTableT::CNodeList_HashTableT
CNodeList_HashTableT()
default constructor
Definition: nodelist.hpp:43
CNodeList_HashTableT::InsertOpenNode
void InsertOpenNode(Titem_ &item)
insert given item as open node (into m_open and m_open_queue)
Definition: nodelist.hpp:83
CNodeList_HashTableT::FindOpenNode
Titem_ * FindOpenNode(const Key &key)
return the open node specified by a key or NULL if not found
Definition: nodelist.hpp:114
CBinaryHeapT::Remove
void Remove(uint index)
Remove item at given index from the priority queue.
Definition: binaryheap.hpp:256
CNodeList_HashTableT::FindClosedNode
Titem_ * FindClosedNode(const Key &key)
return the closed node specified by a key or NULL if not found
Definition: nodelist.hpp:137
CNodeList_HashTableT::PopOpenNode
Titem_ & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition: nodelist.hpp:121
CNodeList_HashTableT::InsertClosedNode
void InsertClosedNode(Titem_ &item)
close node
Definition: nodelist.hpp:130