Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef INCLUDED_PRIORITYQUEUE
19 : #define INCLUDED_PRIORITYQUEUE
20 :
21 : /*
22 : * Priority queues for pathfinder.
23 : * (These probably aren't suitable for more general uses.)
24 : */
25 :
26 : #include "lib/posix/posix_types.h"
27 :
28 : #include <cstddef>
29 : #include <functional>
30 : #include <vector>
31 :
32 : #ifdef NDEBUG
33 : #define PRIORITYQUEUE_DEBUG 0
34 : #else
35 : #define PRIORITYQUEUE_DEBUG 1
36 : #endif
37 :
38 : template <typename Item, typename CMP>
39 : struct QueueItemPriority
40 : {
41 13323 : bool operator()(const Item& a, const Item& b) const
42 : {
43 13323 : if (CMP()(b.rank, a.rank)) // higher costs are lower priority
44 : return true;
45 5723 : if (CMP()(a.rank, b.rank))
46 : return false;
47 : // Need to tie-break to get a consistent ordering
48 1932 : if (CMP()(b.h, a.h)) // higher heuristic costs are lower priority
49 : return true;
50 1890 : if (CMP()(a.h, b.h))
51 : return false;
52 1848 : if (a.id < b.id)
53 : return true;
54 : if (b.id < a.id)
55 : return false;
56 : #if PRIORITYQUEUE_DEBUG
57 : debug_warn(L"duplicate tiles in queue");
58 : #endif
59 : return false;
60 : }
61 : };
62 :
63 :
64 : /**
65 : * Priority queue implemented as a binary heap.
66 : * This is quite dreadfully slow in MSVC's debug STL implementation,
67 : * so we shouldn't use it unless we reimplement the heap functions more efficiently.
68 : */
69 : template <typename ID, typename R, typename H, typename CMP = std::less<R> >
70 1907 : class PriorityQueueHeap
71 : {
72 : public:
73 : struct Item
74 : {
75 : ID id;
76 : R rank; // f = g+h (estimated total cost of path through here)
77 : H h; // heuristic cost
78 : };
79 :
80 9515 : void push(const Item& item)
81 : {
82 9515 : m_Heap.push_back(item);
83 9530 : push_heap(m_Heap.begin(), m_Heap.end(), QueueItemPriority<Item, CMP>());
84 9513 : }
85 :
86 0 : void promote(ID id, R /*oldrank*/, R newrank, H newh)
87 : {
88 : // Loop backwards since it seems a little faster in practice
89 0 : for (ssize_t n = m_Heap.size() - 1; n >= 0; --n)
90 : {
91 0 : if (m_Heap[n].id == id)
92 : {
93 : #if PRIORITYQUEUE_DEBUG
94 : ENSURE(m_Heap[n].rank > newrank);
95 : #endif
96 0 : m_Heap[n].rank = newrank;
97 0 : m_Heap[n].h = newh;
98 0 : push_heap(m_Heap.begin(), m_Heap.begin()+n+1, QueueItemPriority<Item, CMP>());
99 0 : return;
100 : }
101 : }
102 : }
103 :
104 2851 : Item pop()
105 : {
106 : #if PRIORITYQUEUE_DEBUG
107 : ENSURE(m_Heap.size());
108 : #endif
109 2851 : Item r = m_Heap[0];
110 2851 : pop_heap(m_Heap.begin(), m_Heap.end(), QueueItemPriority<Item, CMP>());
111 2850 : m_Heap.pop_back();
112 2850 : return r;
113 : }
114 :
115 2856 : bool empty()
116 : {
117 2856 : return m_Heap.empty();
118 : }
119 :
120 : size_t size()
121 : {
122 : return m_Heap.size();
123 : }
124 :
125 948 : void clear()
126 : {
127 1896 : m_Heap.clear();
128 : }
129 :
130 : std::vector<Item> m_Heap;
131 : };
132 :
133 : /**
134 : * Priority queue implemented as an unsorted array.
135 : * This means pop() is O(n), but push and promote are O(1), and n is typically small
136 : * (average around 50-100 in some rough tests).
137 : * It seems fractionally slower than a binary heap in optimised builds, but is
138 : * much simpler and less susceptible to MSVC's painfully slow debug STL.
139 : */
140 : template <typename ID, typename R, typename H, typename CMP = std::less<R> >
141 : class PriorityQueueList
142 : {
143 : public:
144 : struct Item
145 : {
146 : ID id;
147 : R rank; // f = g+h (estimated total cost of path through here)
148 : H h; // heuristic cost
149 : };
150 :
151 : void push(const Item& item)
152 : {
153 : m_List.push_back(item);
154 : }
155 :
156 : Item* find(ID id)
157 : {
158 : for (size_t n = 0; n < m_List.size(); ++n)
159 : {
160 : if (m_List[n].id == id)
161 : return &m_List[n];
162 : }
163 : return NULL;
164 : }
165 :
166 : void promote(ID id, R /*oldrank*/, R newrank, H newh)
167 : {
168 : find(id)->rank = newrank;
169 : find(id)->h = newh;
170 : }
171 :
172 : Item pop()
173 : {
174 : #if PRIORITYQUEUE_DEBUG
175 : ENSURE(m_List.size());
176 : #endif
177 : // Loop backwards looking for the best (it's most likely to be one
178 : // we've recently pushed, so going backwards saves a bit of copying)
179 : Item best = m_List.back();
180 : size_t bestidx = m_List.size()-1;
181 : for (ssize_t i = (ssize_t)bestidx-1; i >= 0; --i)
182 : {
183 : if (QueueItemPriority<Item, CMP>()(best, m_List[i]))
184 : {
185 : bestidx = i;
186 : best = m_List[i];
187 : }
188 : }
189 : // Swap the matched element with the last in the list, then pop the new last
190 : m_List[bestidx] = m_List[m_List.size()-1];
191 : m_List.pop_back();
192 : return best;
193 : }
194 :
195 : bool empty()
196 : {
197 : return m_List.empty();
198 : }
199 :
200 : size_t size()
201 : {
202 : return m_List.size();
203 : }
204 :
205 :
206 : void clear()
207 : {
208 : m_List.clear();
209 : }
210 :
211 : std::vector<Item> m_List;
212 : };
213 :
214 : #endif // INCLUDED_PRIORITYQUEUE
|