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:
40 
41 public:
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 */