1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (C) 2010-2025 by the Widelands Development Team
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef WL_LOGIC_COOKIE_PRIORITY_QUEUE_H
#define WL_LOGIC_COOKIE_PRIORITY_QUEUE_H
#include <cassert>
#include <functional>
#include <limits>
#include <vector>
#include "base/macros.h"
#include "logic/map_objects/tribes/wareworker.h"
template <typename Type> struct DefaultCookieAccessor;
template <typename CT> struct CookiePriorityQueueBase {
using CookieType = CT;
using CookieTypeVector = std::vector<CookieType*>;
using CookieSizeType = typename CookieTypeVector::size_type;
struct Cookie {
Cookie() : pos(bad_pos()) {
}
~Cookie() = default;
/** Returns \c true if the cookie is currently managed by a queue */
[[nodiscard]] bool is_active() const {
return pos != bad_pos();
}
private:
friend struct CookiePriorityQueueBase<CookieType>;
CookieSizeType pos;
DISALLOW_COPY_AND_ASSIGN(Cookie);
};
protected:
static CookieSizeType& cookie_pos(Cookie& cookie) {
return cookie.pos;
}
static CookieSizeType bad_pos() {
return std::numeric_limits<CookieSizeType>::max();
}
};
/**
* A priority queue (heap) with back-links called cookies to allow
* key modification in logarithmic time.
*
* Since elements are treated as objects with an identity, we store pointers
* unlike the usual STL container behaviour.
*
* The comparison type must act like a "stricly less than" comparison.
* The "strictly" part is important.
*
* \ref top returns an element such that no other element in the container
* is strictly less than the element returned by \ref top
*
* If an element's position with respect to the ordering is changed,
* you have to call \ref decrease_key or \ref increase_key after the change,
* depending on whether the change caused a decrease or increase, respectively.
*/
template <typename CPQType,
typename Cmp = std::less<CPQType>,
typename Cas = DefaultCookieAccessor<CPQType>>
struct CookiePriorityQueue : CookiePriorityQueueBase<CPQType> {
using Compare = Cmp;
using CookieAccessor = Cas;
using CookieType = typename CookiePriorityQueueBase<CPQType>::CookieType;
using CookieTypeVector = typename CookiePriorityQueueBase<CPQType>::CookieTypeVector;
using CookieSizeType = typename CookiePriorityQueueBase<CPQType>::CookieSizeType;
using Cookie = typename CookiePriorityQueueBase<CPQType>::Cookie;
using BaseType = CookiePriorityQueueBase<CPQType>;
explicit CookiePriorityQueue(Widelands::WareWorker type,
const Compare& comparator = Compare(),
const CookieAccessor& accessor = CookieAccessor());
~CookiePriorityQueue();
[[nodiscard]] CookieSizeType size() const;
[[nodiscard]] bool empty() const;
[[nodiscard]] CookieType* top() const;
void push(CookieType* elt);
void pop(CookieType* elt);
void decrease_key(CookieType* elt);
void increase_key(CookieType* elt);
[[nodiscard]] Widelands::WareWorker type() const {
return type_;
}
private:
Widelands::WareWorker type_;
CookieTypeVector d;
Compare c;
CookieAccessor ca;
void swap(Cookie& a, Cookie& b);
void selftest();
static CookieSizeType& cookie_pos(Cookie& c) {
return BaseType::cookie_pos(c);
}
static CookieSizeType bad_pos() {
return BaseType::bad_pos();
}
static CookieSizeType parent_pos(CookieSizeType pos) {
return (pos - 1) / 2;
}
static CookieSizeType left_child_pos(CookieSizeType pos) {
return (2 * pos) + 1;
}
static CookieSizeType right_child_pos(CookieSizeType pos) {
return (2 * pos) + 2;
}
};
template <typename DCAType> struct DefaultCookieAccessor {
using Cookie = typename CookiePriorityQueueBase<DCAType>::Cookie;
Cookie& operator()(DCAType* t, Widelands::WareWorker type) {
return t->cookie(type);
}
};
template <typename t_T, typename t_Cw, typename t_CA>
CookiePriorityQueue<t_T, t_Cw, t_CA>::CookiePriorityQueue(
Widelands::WareWorker wwtype,
const typename CookiePriorityQueue<t_T, t_Cw, t_CA>::Compare& comparator,
const typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieAccessor& accessor)
: type_(wwtype), c(comparator), ca(accessor) {
}
template <typename t_T, typename t_Cw, typename t_CA>
CookiePriorityQueue<t_T, t_Cw, t_CA>::~CookiePriorityQueue() {
for (typename CookieTypeVector::iterator it = d.begin(); it != d.end(); ++it) {
cookie_pos(ca(*it, type_)) = bad_pos();
}
}
template <typename t_T, typename t_Cw, typename t_CA>
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieSizeType
CookiePriorityQueue<t_T, t_Cw, t_CA>::size() const {
return d.size();
}
template <typename t_T, typename t_Cw, typename t_CA>
bool CookiePriorityQueue<t_T, t_Cw, t_CA>::empty() const {
return d.empty();
}
template <typename t_T, typename t_Cw, typename t_CA>
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieType*
CookiePriorityQueue<t_T, t_Cw, t_CA>::top() const {
return *d.begin();
}
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::push(
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieType* elt) {
Cookie& elt_cookie(ca(elt, type_));
assert(cookie_pos(elt_cookie) == BaseType::bad_pos());
d.push_back(elt);
cookie_pos(elt_cookie) = d.size() - 1;
decrease_key(elt);
}
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::pop(
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieType* elt) {
Cookie& elt_cookie(ca(elt, type_));
assert(cookie_pos(elt_cookie) < d.size());
while (cookie_pos(elt_cookie) > 0) {
Cookie& parent_cookie = ca(*(d.begin() + parent_pos(cookie_pos(elt_cookie))), type_);
assert(cookie_pos(parent_cookie) == parent_pos(cookie_pos(elt_cookie)));
swap(elt_cookie, parent_cookie);
}
swap(elt_cookie, ca(d.back(), type_));
d.pop_back();
cookie_pos(elt_cookie) = BaseType::bad_pos();
if (!d.empty()) {
increase_key(*d.begin());
}
}
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::decrease_key(
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieType* elt) {
Cookie& elt_cookie(ca(elt, type_));
assert(cookie_pos(elt_cookie) < d.size());
while (cookie_pos(elt_cookie) != 0) {
CookieSizeType parent = parent_pos(cookie_pos(elt_cookie));
if (!c(*elt, *d[parent], type_)) {
break;
}
Cookie& parent_cookie(ca(d[parent], type_));
assert(cookie_pos(parent_cookie) == parent);
swap(elt_cookie, parent_cookie);
}
#ifndef NDEBUG
selftest();
#endif
}
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::increase_key(
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::CookieType* elt) {
Cookie& elt_cookie(ca(elt, type_));
assert(cookie_pos(elt_cookie) < d.size());
for (;;) {
CookieSizeType left_child = left_child_pos(cookie_pos(elt_cookie));
CookieSizeType right_child = right_child_pos(cookie_pos(elt_cookie));
if (left_child >= d.size()) {
break;
}
Cookie& left_cookie(ca(d[left_child], type_));
assert(cookie_pos(left_cookie) == left_child);
if (c(**(d.begin() + left_child), *elt, type_)) {
if (right_child >= d.size() || c(*d[left_child], *d[right_child], type_)) {
swap(elt_cookie, left_cookie);
continue;
}
}
if (right_child >= d.size()) {
break;
}
Cookie& right_cookie(ca(d[right_child], type_));
assert(cookie_pos(right_cookie) == right_child);
if (c(*d[right_child], *elt, type_)) {
swap(elt_cookie, right_cookie);
continue;
}
break;
}
#ifndef NDEBUG
selftest();
#endif
}
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::swap(
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::Cookie& a,
typename CookiePriorityQueue<t_T, t_Cw, t_CA>::Cookie& b) {
std::swap(d[cookie_pos(a)], d[cookie_pos(b)]);
std::swap(cookie_pos(a), cookie_pos(b));
}
// Disable in release builds.
template <typename t_T, typename t_Cw, typename t_CA>
void CookiePriorityQueue<t_T, t_Cw, t_CA>::selftest() {
for (CookieSizeType pos = 0; pos < d.size(); ++pos) {
Cookie& elt_cookie(ca(d[pos], type_));
assert(cookie_pos(elt_cookie) == pos);
CookieSizeType left_child = left_child_pos(pos);
CookieSizeType right_child = right_child_pos(pos);
if (left_child < d.size()) {
assert(!c(*d[left_child], *d[pos], type_));
}
if (right_child < d.size()) {
assert(!c(*d[right_child], *d[pos], type_));
}
}
}
#endif // end of include guard: WL_LOGIC_COOKIE_PRIORITY_QUEUE_H
|