-
Notifications
You must be signed in to change notification settings - Fork 7
/
priority_queue.h
491 lines (382 loc) · 17.3 KB
/
priority_queue.h
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a priority_queue that is just like the C++
// std::priority_queue adapter class, except it has a couple extension functions.
// The primary distinctions between this priority_queue and std::priority_queue are:
// - priority_queue has a couple extension functions that allow you to
// use a priority queue in extra ways. See the code for documentation.
// - priority_queue can contain objects with alignment requirements.
// std::priority_queue cannot do so without a bit of tedious non-portable effort.
// - priority_queue supports debug memory naming natively.
// - priority_queue is easier to read, debug, and visualize.
// - priority_queue is savvy to an environment that doesn't have exception handling,
// as is sometimes the case with console or embedded environments.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_PRIORITY_QUEUE_H
#define EASTL_PRIORITY_QUEUE_H
#include <eastl/internal/config.h>
#include <eastl/vector.h>
#include <eastl/heap.h>
#include <eastl/functional.h>
#include <eastl/initializer_list.h>
#include <stddef.h>
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
EA_DISABLE_VC_WARNING(4530 4571);
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// EASTL_PRIORITY_QUEUE_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_PRIORITY_QUEUE_DEFAULT_NAME
#define EASTL_PRIORITY_QUEUE_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " priority_queue" // Unless the user overrides something, this is "EASTL priority_queue".
#endif
/// EASTL_PRIORITY_QUEUE_DEFAULT_ALLOCATOR
///
#ifndef EASTL_PRIORITY_QUEUE_DEFAULT_ALLOCATOR
#define EASTL_PRIORITY_QUEUE_DEFAULT_ALLOCATOR allocator_type(EASTL_PRIORITY_QUEUE_DEFAULT_NAME)
#endif
/// priority_queue
///
/// The behaviour of this class is just like the std::priority_queue
/// class and you can refer to std documentation on it.
///
/// A priority_queue is an adapter container which implements a
/// queue-like container whereby pop() returns the item of highest
/// priority. The entire queue isn't necessarily sorted; merely the
/// first item in the queue happens to be of higher priority than
/// other items. You can read about priority_queues in many books
/// on algorithms, such as "Algorithms" by Robert Sedgewick.
///
/// The Container type is a container which is random access and
/// supports empty(), size(), clear(), insert(), front(),
/// pushBack(), and popBack(). You would typically use vector
/// or deque.
///
/// Note that we don't provide functions in the priority_queue
/// interface for working with allocators or names. The reason for
/// this is that priority_queue is an adapter class which can work
/// with any standard sequence and not necessarily just a sequence
/// provided by this library. So what we do is provide a member
/// accessor function get_container() which allows the user to
/// manipulate the sequence as needed. The user needs to be careful
/// not to change the container's contents, however.
///
/// Classic heaps allow for the concept of removing arbitrary items
/// and changing the priority of arbitrary items, though the C++
/// std heap (and thus priority_queue) functions don't support
/// these operations. We have extended the heap algorithms and the
/// priority_queue implementation to support these operations.
///
///////////////////////////////////////////////////////////////////
template <typename T, typename Container = eastl::vector<T>, typename Compare = eastl::less<typename Container::value_type> >
class priority_queue
{
public:
typedef priority_queue<T, Container, Compare> this_type;
typedef Container container_type;
typedef Compare compare_type;
//typedef typename Container::allocator_type allocator_type; // We can't currently declare this because the container may be a type that doesn't have an allocator.
typedef typename Container::value_type value_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
typedef typename Container::size_type size_type;
typedef typename Container::difference_type difference_type;
public: // We declare public so that global comparison operators can be implemented without adding an inline level and without tripping up GCC 2.x friend declaration failures. GCC (through at least v4.0) is poor at inlining and performance wins over correctness.
container_type c; // The C++ standard specifies that you declare a protected member variable of type Container called 'c'.
compare_type comp; // The C++ standard specifies that you declare a protected member variable of type Compare called 'comp'.
public:
priority_queue();
// Allocator is templated here because we aren't allowed to infer the allocator_type from the Container, as some containers (e.g. array) don't
// have allocators. For containers that don't have allocator types, you could use void or char as the Allocator template type.
template <class Allocator>
explicit priority_queue(const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(allocator), comp()
{
}
template <class Allocator>
priority_queue(const this_type& x, const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(x.c, allocator), comp(x.comp)
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <class Allocator>
priority_queue(this_type&& x, const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(eastl::move(x.c), allocator), comp(x.comp)
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
explicit priority_queue(const compare_type& compare);
explicit priority_queue(const compare_type& compare, container_type&& x);
priority_queue(const compare_type& compare, const container_type& x);
priority_queue(std::initializer_list<value_type> ilist, const compare_type& compare = compare_type()); // C++11 doesn't specify that std::priority_queue has initializer list support.
template <typename InputIterator>
priority_queue(InputIterator first, InputIterator last);
template <typename InputIterator>
priority_queue(InputIterator first, InputIterator last, const compare_type& compare);
template <typename InputIterator>
priority_queue(InputIterator first, InputIterator last, const compare_type& compare, const container_type& x);
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const compare_type& compare, container_type&& x);
// Additional C++11 support to consider:
//
// template <class Allocator>
// priority_queue(const Compare&, const Allocator&);
//
// template <class Allocator>
// priority_queue(const Compare&, const container_type&, const Allocator&);
//
// template <class Allocator>
// priority_queue(const Compare&, container_type&&, const Allocator&);
bool empty() const;
size_type size() const;
const_reference top() const;
void push(const value_type& value);
void push(value_type&& x);
template <class... Args>
void emplace(Args&&... args);
void pop();
void pop(value_type& value); // Extension to the C++11 Standard that allows popping a move-only type (e.g. unique_ptr).
void change(size_type n); /// Moves the item at the given array index to a new location based on its current priority.
void remove(size_type n); /// Removes the item at the given array index.
container_type& get_container();
const container_type& get_container() const;
void swap(this_type& x) EASTL_NOEXCEPT_IF((eastl::is_nothrow_swappable<this_type::container_type>::value && eastl::is_nothrow_swappable<this_type::compare_type>::value));
bool validate() const;
}; // class priority_queue
///////////////////////////////////////////////////////////////////////
// priority_queue
///////////////////////////////////////////////////////////////////////
template <typename T, typename Container, typename Compare>
inline priority_queue<T, Container, Compare>::priority_queue()
: c(), // To consider: use c(EASTL_PRIORITY_QUEUE_DEFAULT_ALLOCATOR) here, though that would add the requirement that the user supplied container support this.
comp()
{
}
template <typename T, typename Container, typename Compare>
inline priority_queue<T, Container, Compare>::priority_queue(const compare_type& compare)
: c(), // To consider: use c(EASTL_PRIORITY_QUEUE_DEFAULT_ALLOCATOR) here, though that would add the requirement that the user supplied container support this.
comp(compare)
{
}
template <typename T, typename Container, typename Compare>
inline priority_queue<T, Container, Compare>::priority_queue(const compare_type& compare, const container_type& x)
: c(x), comp(compare)
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
inline priority_queue<T, Container, Compare>::priority_queue(const compare_type& compare, container_type&& x)
: c(eastl::move(x)), comp(compare)
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
inline priority_queue<T, Container, Compare>::priority_queue(std::initializer_list<value_type> ilist, const compare_type& compare)
: c(), comp(compare)
{
c.insert(c.end(), ilist.begin(), ilist.end());
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
template <typename InputIterator>
inline priority_queue<T, Container, Compare>::priority_queue(InputIterator first, InputIterator last)
: c(first, last), comp()
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
template <typename InputIterator>
inline priority_queue<T, Container, Compare>::priority_queue(InputIterator first, InputIterator last, const compare_type& compare)
: c(first, last), comp(compare)
{
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
template <typename InputIterator>
inline priority_queue<T, Container, Compare>::priority_queue(InputIterator first, InputIterator last, const compare_type& compare, const container_type& x)
: c(x), comp(compare)
{
c.insert(c.end(), first, last);
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
template <typename InputIterator>
inline priority_queue<T, Container, Compare>::priority_queue(InputIterator first, InputIterator last, const compare_type& compare, container_type&& x)
: c(eastl::move(x)), comp(compare)
{
c.insert(c.end(), first, last);
eastl::makeHeap(c.begin(), c.end(), comp);
}
template <typename T, typename Container, typename Compare>
inline bool priority_queue<T, Container, Compare>::empty() const
{
return c.empty();
}
template <typename T, typename Container, typename Compare>
inline typename priority_queue<T, Container, Compare>::size_type
priority_queue<T, Container, Compare>::size() const
{
return c.size();
}
template <typename T, typename Container, typename Compare>
inline typename priority_queue<T, Container, Compare>::const_reference
priority_queue<T, Container, Compare>::top() const
{
return c.front();
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::push(const value_type& value)
{
#if EASTL_EXCEPTIONS_ENABLED
try
{
c.pushBack(value);
eastl::pushHeap(c.begin(), c.end(), comp);
}
catch(...)
{
c.clear();
throw;
}
#else
c.pushBack(value);
eastl::pushHeap(c.begin(), c.end(), comp);
#endif
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::push(value_type&& value)
{
#if EASTL_EXCEPTIONS_ENABLED
try
{
c.pushBack(eastl::move(value));
eastl::pushHeap(c.begin(), c.end(), comp);
}
catch(...)
{
c.clear();
throw;
}
#else
c.pushBack(eastl::move(value));
eastl::pushHeap(c.begin(), c.end(), comp);
#endif
}
template <typename T, typename Container, typename Compare>
template <class... Args>
inline void priority_queue<T, Container, Compare>::emplace(Args&&... args)
{
push(value_type(eastl::forward<Args>(args)...)); // The C++11 Standard 23.6.4/1 states that c.emplace is used, but also declares that c doesn't need to have an emplace function.
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::pop()
{
#if EASTL_EXCEPTIONS_ENABLED
try
{
eastl::popHeap(c.begin(), c.end(), comp);
c.popBack();
}
catch(...)
{
c.clear();
throw;
}
#else
eastl::popHeap(c.begin(), c.end(), comp);
c.popBack();
#endif
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::pop(value_type& value)
{
value = eastl::move(c.front()); // To consider: value = move_if_noexcept_assignable(c.front());
pop();
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::change(size_type n) // This function is not in the STL std::priority_queue.
{
eastl::changeHeap(c.begin(), c.size(), n, comp);
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::remove(size_type n) // This function is not in the STL std::priority_queue.
{
eastl::removeHeap(c.begin(), c.size(), n, comp);
c.popBack();
}
template <typename T, typename Container, typename Compare>
inline typename priority_queue<T, Container, Compare>::container_type&
priority_queue<T, Container, Compare>::get_container()
{
return c;
}
template <typename T, typename Container, typename Compare>
inline const typename priority_queue<T, Container, Compare>::container_type&
priority_queue<T, Container, Compare>::get_container() const
{
return c;
}
template <typename T, typename Container, typename Compare>
inline void priority_queue<T, Container, Compare>::swap(this_type& x) EASTL_NOEXCEPT_IF((eastl::is_nothrow_swappable<this_type::container_type>::value &&
eastl::is_nothrow_swappable<this_type::compare_type>::value))
{
using eastl::swap;
swap(c, x.c);
swap(comp, x.comp);
}
template <typename T, typename Container, typename Compare>
inline bool
priority_queue<T, Container, Compare>::validate() const
{
return c.validate() && eastl::isHeap(c.begin(), c.end(), comp);
}
///////////////////////////////////////////////////////////////////////
// global operators
///////////////////////////////////////////////////////////////////////
template <typename T, typename Container, typename Compare>
bool operator==(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return (a.c == b.c);
}
template <typename T, typename Container, typename Compare>
bool operator<(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return (a.c < b.c);
}
template <typename T, typename Container, typename Compare>
inline bool operator!=(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return !(a.c == b.c);
}
template <typename T, typename Container, typename Compare>
inline bool operator>(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return (b.c < a.c);
}
template <typename T, typename Container, typename Compare>
inline bool operator<=(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return !(b.c < a.c);
}
template <typename T, typename Container, typename Compare>
inline bool operator>=(const priority_queue<T, Container, Compare>& a, const priority_queue<T, Container, Compare>& b)
{
return !(a.c < b.c);
}
template <class T, class Container, class Compare>
inline void swap(priority_queue<T, Container, Compare>& a, priority_queue<T, Container, Compare>& b) EASTL_NOEXCEPT_IF((eastl::is_nothrow_swappable<typename priority_queue<T, Container, Compare>::container_type>::value &&
eastl::is_nothrow_swappable<typename priority_queue<T, Container, Compare>::compare_type>::value)) // EDG has a bug and won't let us use Container in this noexcept statement
{
a.swap(b);
}
} // namespace eastl
EA_RESTORE_VC_WARNING();
#endif // Header include guard