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