Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
PriorityQueue.h
Go to the documentation of this file.
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
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
38template <typename Item, typename CMP>
40{
41 bool operator()(const Item& a, const Item& b) const
42 {
43 if (CMP()(b.rank, a.rank)) // higher costs are lower priority
44 return true;
45 if (CMP()(a.rank, b.rank))
46 return false;
47 // Need to tie-break to get a consistent ordering
48 if (CMP()(b.h, a.h)) // higher heuristic costs are lower priority
49 return true;
50 if (CMP()(a.h, b.h))
51 return false;
52 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 */
69template <typename ID, typename R, typename H, typename CMP = std::less<R> >
71{
72public:
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 void push(const Item& item)
81 {
82 m_Heap.push_back(item);
83 push_heap(m_Heap.begin(), m_Heap.end(), QueueItemPriority<Item, CMP>());
84 }
85
86 void promote(ID id, R /*oldrank*/, R newrank, H newh)
87 {
88 // Loop backwards since it seems a little faster in practice
89 for (ssize_t n = m_Heap.size() - 1; n >= 0; --n)
90 {
91 if (m_Heap[n].id == id)
92 {
93#if PRIORITYQUEUE_DEBUG
94 ENSURE(m_Heap[n].rank > newrank);
95#endif
96 m_Heap[n].rank = newrank;
97 m_Heap[n].h = newh;
98 push_heap(m_Heap.begin(), m_Heap.begin()+n+1, QueueItemPriority<Item, CMP>());
99 return;
100 }
101 }
102 }
103
105 {
106#if PRIORITYQUEUE_DEBUG
107 ENSURE(m_Heap.size());
108#endif
109 Item r = m_Heap[0];
110 pop_heap(m_Heap.begin(), m_Heap.end(), QueueItemPriority<Item, CMP>());
111 m_Heap.pop_back();
112 return r;
113 }
114
115 bool empty()
116 {
117 return m_Heap.empty();
118 }
119
120 size_t size()
121 {
122 return m_Heap.size();
123 }
124
125 void clear()
126 {
127 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 */
140template <typename ID, typename R, typename H, typename CMP = std::less<R> >
142{
143public:
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
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
#define CMP(f)
Priority queue implemented as a binary heap.
Definition: PriorityQueue.h:71
Item pop()
Definition: PriorityQueue.h:104
std::vector< Item > m_Heap
Definition: PriorityQueue.h:130
bool empty()
Definition: PriorityQueue.h:115
size_t size()
Definition: PriorityQueue.h:120
void promote(ID id, R, R newrank, H newh)
Definition: PriorityQueue.h:86
void clear()
Definition: PriorityQueue.h:125
void push(const Item &item)
Definition: PriorityQueue.h:80
Priority queue implemented as an unsorted array.
Definition: PriorityQueue.h:142
void promote(ID id, R, R newrank, H newh)
Definition: PriorityQueue.h:166
bool empty()
Definition: PriorityQueue.h:195
size_t size()
Definition: PriorityQueue.h:200
Item pop()
Definition: PriorityQueue.h:172
Item * find(ID id)
Definition: PriorityQueue.h:156
std::vector< Item > m_List
Definition: PriorityQueue.h:211
void push(const Item &item)
Definition: PriorityQueue.h:151
void clear()
Definition: PriorityQueue.h:206
#define debug_warn(expr)
display the error dialog with the given text.
Definition: debug.h:264
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Definition: PriorityQueue.h:74
R rank
Definition: PriorityQueue.h:76
ID id
Definition: PriorityQueue.h:75
H h
Definition: PriorityQueue.h:77
Definition: PriorityQueue.h:145
R rank
Definition: PriorityQueue.h:147
H h
Definition: PriorityQueue.h:148
ID id
Definition: PriorityQueue.h:146
Definition: PriorityQueue.h:40
bool operator()(const Item &a, const Item &b) const
Definition: PriorityQueue.h:41
intptr_t ssize_t
Definition: wposix_types.h:36