OpenTTD Source  20241108-master-g80f628063a
nodelist.hpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #ifndef NODELIST_HPP
11 #define NODELIST_HPP
12 
13 #include "../../misc/hashtable.hpp"
14 #include "../../misc/binaryheap.hpp"
15 
21 template <class Titem_, int Thash_bits_open_, int Thash_bits_closed_>
23 public:
24  typedef Titem_ Titem;
25  typedef typename Titem_::Key Key;
26  using CItemArray = std::deque<Titem_>;
30 
31 protected:
37 
38 public:
41  {
42  this->new_node = nullptr;
43  }
44 
47  {
48  }
49 
51  inline int OpenCount()
52  {
53  return this->open_nodes.Count();
54  }
55 
57  inline int ClosedCount()
58  {
59  return this->closed_nodes.Count();
60  }
61 
63  inline Titem_ *CreateNewNode()
64  {
65  if (this->new_node == nullptr) this->new_node = &this->items.emplace_back();
66  return this->new_node;
67  }
68 
70  inline void FoundBestNode(Titem_ &item)
71  {
72  /* for now it is enough to invalidate m_new_node if it is our given node */
73  if (&item == this->new_node) {
74  this->new_node = nullptr;
75  }
76  /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
77  }
78 
80  inline void InsertOpenNode(Titem_ &item)
81  {
82  assert(this->closed_nodes.Find(item.GetKey()) == nullptr);
83  this->open_nodes.Push(item);
84  this->open_queue.Include(&item);
85  if (&item == this->new_node) {
86  this->new_node = nullptr;
87  }
88  }
89 
91  inline Titem_ *GetBestOpenNode()
92  {
93  if (!this->open_queue.IsEmpty()) {
94  return this->open_queue.Begin();
95  }
96  return nullptr;
97  }
98 
100  inline Titem_ *PopBestOpenNode()
101  {
102  if (!this->open_queue.IsEmpty()) {
103  Titem_ *item = this->open_queue.Shift();
104  this->open_nodes.Pop(*item);
105  return item;
106  }
107  return nullptr;
108  }
109 
111  inline Titem_ *FindOpenNode(const Key &key)
112  {
113  Titem_ *item = this->open_nodes.Find(key);
114  return item;
115  }
116 
118  inline Titem_ &PopOpenNode(const Key &key)
119  {
120  Titem_ &item = this->open_nodes.Pop(key);
121  size_t idxPop = this->open_queue.FindIndex(item);
122  this->open_queue.Remove(idxPop);
123  return item;
124  }
125 
127  inline void InsertClosedNode(Titem_ &item)
128  {
129  assert(this->open_nodes.Find(item.GetKey()) == nullptr);
130  this->closed_nodes.Push(item);
131  }
132 
134  inline Titem_ *FindClosedNode(const Key &key)
135  {
136  Titem_ *item = this->closed_nodes.Find(key);
137  return item;
138  }
139 
141  inline int TotalCount()
142  {
143  return this->items.Length();
144  }
145 
147  inline Titem_ &ItemAt(int idx)
148  {
149  return this->items[idx];
150  }
151 
153  template <class D> void Dump(D &dmp) const
154  {
155  dmp.WriteStructT("data", &this->items);
156  }
157 };
158 
159 #endif /* NODELIST_HPP */
void Remove(size_t index)
Remove item at given index from the priority queue.
Definition: binaryheap.hpp:233
T * Begin()
Get the smallest item in the binary tree.
Definition: binaryheap.hpp:172
void Include(T *new_item)
Insert new item into the priority queue, maintaining heap order.
Definition: binaryheap.hpp:195
size_t FindIndex(const T &item) const
Search for an item in the priority queue.
Definition: binaryheap.hpp:263
bool IsEmpty() const
Test if the priority queue is empty.
Definition: binaryheap.hpp:162
T * Shift()
Remove and return the smallest (and also first) item from the priority queue.
Definition: binaryheap.hpp:210
Hash table based node list multi-container class.
Definition: nodelist.hpp:22
CPriorityQueue open_queue
Priority queue of pointers to open item data.
Definition: nodelist.hpp:35
CItemArray items
Here we store full item data (Titem_).
Definition: nodelist.hpp:32
Titem_ & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition: nodelist.hpp:118
std::deque< Titem_ > CItemArray
Type that we will use as item container.
Definition: nodelist.hpp:26
int TotalCount()
The number of items.
Definition: nodelist.hpp:141
Titem * new_node
New open node under construction.
Definition: nodelist.hpp:36
Titem_ Titem
Make #Titem_ visible from outside of class.
Definition: nodelist.hpp:24
void InsertOpenNode(Titem_ &item)
insert given item as open node (into m_open and m_open_queue)
Definition: nodelist.hpp:80
~CNodeList_HashTableT()
destructor
Definition: nodelist.hpp:46
int OpenCount()
return number of open nodes
Definition: nodelist.hpp:51
COpenList open_nodes
Hash table of pointers to open item data.
Definition: nodelist.hpp:33
Titem_ * PopBestOpenNode()
remove and return the best open node
Definition: nodelist.hpp:100
HashTable< Titem_, Thash_bits_open_ > COpenList
How pointers to open nodes will be stored.
Definition: nodelist.hpp:27
Titem_ * GetBestOpenNode()
return the best open node
Definition: nodelist.hpp:91
Titem_ & ItemAt(int idx)
Get a particular item.
Definition: nodelist.hpp:147
CClosedList closed_nodes
Hash table of pointers to closed item data.
Definition: nodelist.hpp:34
void Dump(D &dmp) const
Helper for creating output of this array.
Definition: nodelist.hpp:153
Titem_ * FindOpenNode(const Key &key)
return the open node specified by a key or nullptr if not found
Definition: nodelist.hpp:111
void InsertClosedNode(Titem_ &item)
close node
Definition: nodelist.hpp:127
Titem_::Key Key
Make Titem_::Key a property of this class.
Definition: nodelist.hpp:25
Titem_ * FindClosedNode(const Key &key)
return the closed node specified by a key or nullptr if not found
Definition: nodelist.hpp:134
void FoundBestNode(Titem_ &item)
Notify the nodelist that we don't want to discard the given node.
Definition: nodelist.hpp:70
int ClosedCount()
return number of closed nodes
Definition: nodelist.hpp:57
CBinaryHeapT< Titem_ > CPriorityQueue
How the priority queue will be managed.
Definition: nodelist.hpp:29
HashTable< Titem_, Thash_bits_closed_ > CClosedList
How pointers to closed nodes will be stored.
Definition: nodelist.hpp:28
Titem_ * CreateNewNode()
allocate new data item from items
Definition: nodelist.hpp:63
CNodeList_HashTableT()
default constructor
Definition: nodelist.hpp:40
const Titem * Find(const Tkey &key) const
const item search
Definition: hashtable.hpp:180
void Push(Titem &new_item)
add one item - copy it from the given item
Definition: hashtable.hpp:238
int Count() const
item count
Definition: hashtable.hpp:167
Titem & Pop(const Tkey &key)
non-const item search & removal
Definition: hashtable.hpp:210