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_>
26 public:
27  typedef Titem_ Titem;
28  typedef typename Titem_::Key Key;
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 
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 */
CClosedList m_closed
Hash table of pointers to closed item data.
Definition: nodelist.hpp:37
int ClosedCount()
return number of closed nodes
Definition: nodelist.hpp:60
T * Begin()
Get the smallest item in the binary tree.
Definition: binaryheap.hpp:190
void Push(Titem_ &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:251
CItemArray m_arr
Here we store full item data (Titem_).
Definition: nodelist.hpp:35
int OpenCount()
return number of open nodes
Definition: nodelist.hpp:54
CBinaryHeapT< Titem_ > CPriorityQueue
How the priority queue will be managed.
Definition: nodelist.hpp:32
Titem_ * CreateNewNode()
allocate new data item from m_arr
Definition: nodelist.hpp:66
Titem_ & Pop(const Tkey &key)
non-const item search & removal
Definition: hashtable.hpp:223
Titem * m_new_node
New open node under construction.
Definition: nodelist.hpp:39
bool IsEmpty() const
Test if the priority queue is empty.
Definition: binaryheap.hpp:170
COpenList m_open
Hash table of pointers to open item data.
Definition: nodelist.hpp:36
CHashTableT< Titem_, Thash_bits_closed_ > CClosedList
How pointers to closed nodes will be stored.
Definition: nodelist.hpp:31
Titem_ * GetBestOpenNode()
return the best open node
Definition: nodelist.hpp:94
SmallArray< Titem_, 65536, 256 > CItemArray
Type that we will use as item container.
Definition: nodelist.hpp:29
Titem_::Key Key
Make Titem_::Key a property of #HashTable.
Definition: nodelist.hpp:28
Titem_ * PopBestOpenNode()
remove and return the best open node
Definition: nodelist.hpp:103
const Titem_ * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:193
uint Length() const
Return actual number of items.
Definition: array.hpp:56
Titem_ Titem
Make #Titem_ visible from outside of class.
Definition: nodelist.hpp:27
void Dump(D &dmp) const
Helper for creating output of this array.
Definition: nodelist.hpp:156
T * AppendC()
allocate and construct new item
Definition: array.hpp:82
Hash table based node list multi-container class.
Definition: nodelist.hpp:25
CHashTableT< Titem_, Thash_bits_open_ > COpenList
How pointers to open nodes will be stored.
Definition: nodelist.hpp:30
int Count() const
item count
Definition: hashtable.hpp:181
uint FindIndex(const T &item) const
Search for an item in the priority queue.
Definition: binaryheap.hpp:284
T * Shift()
Remove and return the smallest (and also first) item from the priority queue.
Definition: binaryheap.hpp:234
~CNodeList_HashTableT()
destructor
Definition: nodelist.hpp:49
void FoundBestNode(Titem_ &item)
Notify the nodelist that we don&#39;t want to discard the given node.
Definition: nodelist.hpp:73
void Include(T *new_item)
Insert new item into the priority queue, maintaining heap order.
Definition: binaryheap.hpp:213
int TotalCount()
The number of items.
Definition: nodelist.hpp:144
Titem_ & ItemAt(int idx)
Get a particular item.
Definition: nodelist.hpp:150
CPriorityQueue m_open_queue
Priority queue of pointers to open item data.
Definition: nodelist.hpp:38
CNodeList_HashTableT()
default constructor
Definition: nodelist.hpp:43
void InsertOpenNode(Titem_ &item)
insert given item as open node (into m_open and m_open_queue)
Definition: nodelist.hpp:83
Titem_ * FindOpenNode(const Key &key)
return the open node specified by a key or NULL if not found
Definition: nodelist.hpp:114
void Remove(uint index)
Remove item at given index from the priority queue.
Definition: binaryheap.hpp:256
Titem_ * FindClosedNode(const Key &key)
return the closed node specified by a key or NULL if not found
Definition: nodelist.hpp:137
Titem_ & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition: nodelist.hpp:121
void InsertClosedNode(Titem_ &item)
close node
Definition: nodelist.hpp:130