Debian Codegraph @

js/src/ds/PriorityQueue.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef ds_PriorityQueue_h
8 #define ds_PriorityQueue_h
9
10 #include "js/Vector.h"
11
12 namespace js {
13
14 /*
15 * Class which represents a heap based priority queue using a vector.
16 * Inserting elements and removing the highest priority one are both O(log n).
17 *
18 * Template parameters are the same as for Vector, with the addition that P
19 * must have a static higherPriority(const T& a, const T& b) method which
20 * returns true if |a| has a higher priority than |b|.
21 */
22 template <class T, class P, size_t MinInlineCapacity = 0,
23 class AllocPolicy = TempAllocPolicy>
24 class PriorityQueue {
25 Vector<T, MinInlineCapacity, AllocPolicy> heap;
26
27 PriorityQueue(const PriorityQueue&) = delete;
28 PriorityQueue& operator=(const PriorityQueue&) = delete;
29
30 public:
31 explicit PriorityQueue(AllocPolicy ap = AllocPolicy())
32 : heap(std::move(ap)) {}
33
34 [[nodiscard]] bool reserve(size_t capacity) { return heap.reserve(capacity); }
35
36 size_t length() const { return heap.length(); }
37
38 bool empty() const { return heap.empty(); }
39
40 // highest and popHighest are used to enforce necessary move semantics for
41 // working with UniquePtrs in a queue, and should be used together Example:
42 // UniquePtr<...> x = std::move(queue.highest());
43 // queue.popHighest();
44 T& highest() {
45 MOZ_ASSERT(!empty());
46 return heap[0];
47 }
48
49 void popHighest() {
50 if (heap.length() == 1) {
51 heap.popBack();
52 return;
53 }
54 std::swap(heap[0], heap.back());
55 heap.popBack();
56 siftDown(0);
57 }
58
59 // removeHighest cannot be used with UniquePtrs, and should only be used for
60 // other datatypes.
61 T removeHighest() {
62 T highest = heap[0];
63 T last = heap.popCopy();
64 if (!heap.empty()) {
65 heap[0] = last;
66 siftDown(0);
67 }
68 return highest;
69 }
70
71 [[nodiscard]] bool insert(T&& v) {
72 if (!heap.append(std::move(v))) {
73 return false;
74 }
75 siftUp(heap.length() - 1);
76 return true;
77 }
78
79 void infallibleInsert(T&& v) {
80 heap.infallibleAppend(std::move(v));
81 siftUp(heap.length() - 1);
82 }
83
84 private:
85 /*
86 * Elements of the vector encode a binary tree:
87 *
88 * 0
89 * 1 2
90 * 3 4 5 6
91 *
92 * The children of element N are (2N + 1) and (2N + 2).
93 * The parent of element N is (N - 1) / 2.
94 *
95 * Each element has higher priority than its children.
96 */
97
98 void siftDown(size_t n) {
99 while (true) {
100 size_t left = n * 2 + 1;
101 size_t right = n * 2 + 2;
102
103 if (left < heap.length()) {
104 if (right < heap.length()) {
105 if (P::higherPriority(heap[right], heap[n]) &&
106 P::higherPriority(heap[right], heap[left])) {
107 swap(n, right);
108 n = right;
109 continue;
110 }
111 }
112
113 if (P::higherPriority(heap[left], heap[n])) {
114 swap(n, left);
115 n = left;
116 continue;
117 }
118 }
119
120 break;
121 }
122 }
123
124 void siftUp(size_t n) {
125 while (n > 0) {
126 size_t parent = (n - 1) / 2;
127
128 if (P::higherPriority(heap[parent], heap[n])) {
129 break;
130 }
131
132 swap(n, parent);
133 n = parent;
134 }
135 }
136
137 void swap(size_t a, size_t b) { std::swap(heap[a], heap[b]); }
138 };
139
140 } /* namespace js */
141
142 #endif /* ds_PriorityQueue_h */