OpenTTD Source 20260421-master-gc2fbc6fdeb
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NODELIST_HPP
11#define NODELIST_HPP
12
15
20template <class Titem, int Thash_bits_open, int Thash_bits_closed>
21class NodeList {
22public:
23 using Item = Titem;
24 using Key = typename Titem::Key;
25
26protected:
27 std::deque<Titem> items;
31 Titem *new_node;
32
33public:
36 {
37 this->new_node = nullptr;
38 }
39
44 inline int OpenCount()
45 {
46 return this->open_nodes.Count();
47 }
48
53 inline int ClosedCount()
54 {
55 return this->closed_nodes.Count();
56 }
57
62 inline int TotalCount()
63 {
64 return this->items.Length();
65 }
66
71 inline Titem &CreateNewNode()
72 {
73 if (this->new_node == nullptr) this->new_node = &this->items.emplace_back();
74 return *this->new_node;
75 }
76
81 inline void FoundBestNode(Titem &item)
82 {
83 /* for now it is enough to invalidate new_node if it is our given node */
84 if (&item == this->new_node) {
85 this->new_node = nullptr;
86 }
87 /* TODO: do we need to store best nodes found in some extra list/array? Probably not now. */
88 }
89
94 inline void InsertOpenNode(Titem &item)
95 {
96 assert(this->closed_nodes.Find(item.GetKey()) == nullptr);
97 this->open_nodes.Push(item);
98 this->open_queue.Include(&item);
99 if (&item == this->new_node) {
100 this->new_node = nullptr;
101 }
102 }
103
108 inline Titem *GetBestOpenNode()
109 {
110 if (!this->open_queue.IsEmpty()) {
111 return this->open_queue.Begin();
112 }
113 return nullptr;
114 }
115
120 inline Titem *PopBestOpenNode()
121 {
122 if (!this->open_queue.IsEmpty()) {
123 Titem *item = this->open_queue.Shift();
124 this->open_nodes.Pop(*item);
125 return item;
126 }
127 return nullptr;
128 }
129
135 inline Titem *FindOpenNode(const Key &key)
136 {
137 return this->open_nodes.Find(key);
138 }
139
145 inline Titem &PopOpenNode(const Key &key)
146 {
147 Titem &item = this->open_nodes.Pop(key);
148 size_t index = this->open_queue.FindIndex(item);
149 this->open_queue.Remove(index);
150 return item;
151 }
152
157 inline void InsertClosedNode(Titem &item)
158 {
159 assert(this->open_nodes.Find(item.GetKey()) == nullptr);
160 this->closed_nodes.Push(item);
161 }
162
168 inline Titem *FindClosedNode(const Key &key)
169 {
170 return this->closed_nodes.Find(key);
171 }
172
178 inline Titem &ItemAt(int index)
179 {
180 return this->items[index];
181 }
182
187 template <class D>
188 void Dump(D &dmp) const
189 {
190 dmp.WriteStructT("data", &this->items);
191 }
192};
193
194#endif /* NODELIST_HPP */
Binary heap implementation.
Binary Heap as C++ template.
void Remove(size_t index)
Remove item at given index from the priority queue.
void Include(T *new_item)
Insert new item into the priority queue, maintaining heap order.
size_t FindIndex(const T &item) const
Search for an item in the priority queue.
T * Shift()
Remove and return the smallest (and also first) item from the priority queue.
bool IsEmpty() const
Test if the priority queue is empty.
T * Begin()
Get the smallest item in the binary tree.
class HashTable<Titem, HASH_BITS> - simple hash table of pointers allocated elsewhere.
void Push(Titem &new_item)
Add an item that may not exist.
const Titem * Find(const Tkey &key) const
Try to find an item by key.
int Count() const
Get the number of elements.
Titem & Pop(const Tkey &key)
Remove an item by key that must exist.
HashTable< CYapfRailNode, Thash_bits_open > open_nodes
Definition nodelist.hpp:28
CBinaryHeapT< CYapfRailNode > open_queue
Definition nodelist.hpp:30
void Dump(D &dmp) const
Helper for creating output of this array.
Definition nodelist.hpp:188
NodeList()
default constructor
Definition nodelist.hpp:35
void InsertClosedNode(Titem &item)
Insert the given item into the closed nodes set.
Definition nodelist.hpp:157
Titem * FindClosedNode(const Key &key)
Find a closed node by its key.
Definition nodelist.hpp:168
int TotalCount()
Get the total node count.
Definition nodelist.hpp:62
void InsertOpenNode(Titem &item)
Insert given item as open node (into open_nodes and open_queue).
Definition nodelist.hpp:94
int OpenCount()
Get open node count.
Definition nodelist.hpp:44
Titem * PopBestOpenNode()
Remove and return the best open node.
Definition nodelist.hpp:120
std::deque< CYapfRailNode > items
Definition nodelist.hpp:27
int ClosedCount()
Get closed node count.
Definition nodelist.hpp:53
Titem & CreateNewNode()
Allocate new data item from items.
Definition nodelist.hpp:71
Titem * GetBestOpenNode()
Get the open node at the begin of the open queue.
Definition nodelist.hpp:108
Titem & ItemAt(int index)
Get a particular item.
Definition nodelist.hpp:178
Titem & PopOpenNode(const Key &key)
Find and remove an open node by key.
Definition nodelist.hpp:145
void FoundBestNode(Titem &item)
Notify the nodelist that we don't want to discard the given node.
Definition nodelist.hpp:81
HashTable< CYapfRailNode, Thash_bits_closed > closed_nodes
Definition nodelist.hpp:29
Titem * FindOpenNode(const Key &key)
Find an open node by key.
Definition nodelist.hpp:135
Hash table support.