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/. */
7 #ifndef NS_TPRIORITY_QUEUE_H_
8 #define NS_TPRIORITY_QUEUE_H_
10 #include "mozilla/Assertions.h"
14 * A templatized priority queue data structure that uses an nsTArray to serve as
15 * a binary heap. The default comparator causes this to act like a min-heap.
16 * Only the LessThan method of the comparator is used.
18 template <class T
, class Compare
= nsDefaultComparator
<T
, T
>>
19 class nsTPriorityQueue
{
21 typedef typename nsTArray
<T
>::size_type size_type
;
24 * Default constructor also creates a comparator object using the default
25 * constructor for type Compare.
27 nsTPriorityQueue() : mCompare(Compare()) {}
30 * Constructor to allow a specific instance of a comparator object to be
33 explicit nsTPriorityQueue(const Compare
& aComp
) : mCompare(aComp
) {}
35 nsTPriorityQueue(nsTPriorityQueue
&&) = default;
36 nsTPriorityQueue
& operator=(nsTPriorityQueue
&&) = default;
39 * @return True if the queue is empty or false otherwise.
41 bool IsEmpty() const { return mElements
.IsEmpty(); }
44 * @return The number of elements in the queue.
46 size_type
Length() const { return mElements
.Length(); }
49 * @return The topmost element in the queue without changing the queue. This
50 * is the element 'a' such that there is no other element 'b' in the queue for
51 * which Compare(b, a) returns true. (Since this container does not check
52 * for duplicate entries there may exist 'b' for which Compare(a, b) returns
55 const T
& Top() const {
56 MOZ_ASSERT(!mElements
.IsEmpty(), "Empty queue");
61 * Adds an element to the queue
62 * @param aElement The element to add
63 * @return true on success, false on out of memory.
65 void Push(T
&& aElement
) {
66 mElements
.AppendElement(std::move(aElement
));
69 size_type i
= mElements
.Length() - 1;
71 size_type parent
= (size_type
)((i
- 1) / 2);
72 if (mCompare
.LessThan(mElements
[parent
], mElements
[i
])) {
75 std::swap(mElements
[i
], mElements
[parent
]);
81 * Removes and returns the top-most element from the queue.
82 * @return The topmost element, that is, the element 'a' such that there is no
83 * other element 'b' in the queue for which Compare(b, a) returns true.
87 MOZ_ASSERT(!mElements
.IsEmpty(), "Empty queue");
88 T pop
= std::move(mElements
[0]);
90 const size_type newLength
= mElements
.Length() - 1;
97 mElements
[0] = mElements
.PopLastElement();
101 while (2 * i
+ 1 < newLength
) {
103 size_type l_child
= 2 * i
+ 1;
104 if (mCompare
.LessThan(mElements
[l_child
], mElements
[i
])) {
107 size_type r_child
= l_child
+ 1;
108 if (r_child
< newLength
&&
109 mCompare
.LessThan(mElements
[r_child
], mElements
[swap
])) {
115 std::swap(mElements
[i
], mElements
[swap
]);
123 * Removes all elements from the queue.
125 void Clear() { mElements
.Clear(); }
128 * Provides readonly access to the queue elements as an array. Generally this
129 * should be avoided but may be needed in some situations such as when the
130 * elements contained in the queue need to be enumerated for cycle-collection.
131 * @return A pointer to the first element of the array. If the array is
132 * empty, then this pointer must not be dereferenced.
134 const T
* Elements() const { return mElements
.Elements(); }
136 nsTPriorityQueue
Clone() const {
137 auto res
= nsTPriorityQueue
{mCompare
};
138 res
.mElements
= mElements
.Clone();
143 nsTArray
<T
> mElements
;
144 Compare mCompare
; // Comparator object
147 #endif // NS_TPRIORITY_QUEUE_H_