2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
#ifndef NS_TPRIORITY_QUEUE_H_
|
|
|
|
#define NS_TPRIORITY_QUEUE_H_
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A templatized priority queue data structure that uses an nsTArray to serve as
|
|
|
|
* a binary heap. The default comparator causes this to act like a min-heap.
|
|
|
|
* Only the LessThan method of the comparator is used.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T, class Compare = nsDefaultComparator<T, T>>
|
2010-01-12 23:00:49 +03:00
|
|
|
class nsTPriorityQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename nsTArray<T>::size_type size_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor also creates a comparator object using the default
|
|
|
|
* constructor for type Compare.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTPriorityQueue() : mCompare(Compare()) {}
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor to allow a specific instance of a comparator object to be
|
|
|
|
* used.
|
|
|
|
*/
|
2014-08-19 23:11:22 +04:00
|
|
|
explicit nsTPriorityQueue(const Compare& aComp) : mCompare(aComp) {}
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor
|
|
|
|
*/
|
|
|
|
nsTPriorityQueue(const nsTPriorityQueue& aOther)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mElements(aOther.mElements)
|
|
|
|
, mCompare(aOther.mCompare)
|
|
|
|
{
|
|
|
|
}
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if the queue is empty or false otherwise.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
bool IsEmpty() const { return mElements.IsEmpty(); }
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The number of elements in the queue.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
size_type Length() const { return mElements.Length(); }
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The topmost element in the queue without changing the queue. This
|
|
|
|
* is the element 'a' such that there is no other element 'b' in the queue for
|
2011-10-17 18:59:28 +04:00
|
|
|
* which Compare(b, a) returns true. (Since this container does not check
|
2010-01-12 23:00:49 +03:00
|
|
|
* for duplicate entries there may exist 'b' for which Compare(a, b) returns
|
2011-10-17 18:59:28 +04:00
|
|
|
* false.)
|
2010-01-12 23:00:49 +03:00
|
|
|
*/
|
|
|
|
const T& Top() const
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mElements.IsEmpty(), "Empty queue");
|
2010-01-12 23:00:49 +03:00
|
|
|
return mElements[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an element to the queue
|
|
|
|
* @param aElement The element to add
|
2011-10-17 18:59:28 +04:00
|
|
|
* @return true on success, false on out of memory.
|
2010-01-12 23:00:49 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Push(const T& aElement)
|
2010-01-12 23:00:49 +03:00
|
|
|
{
|
|
|
|
T* elem = mElements.AppendElement(aElement);
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!elem) {
|
|
|
|
return false; // Out of memory
|
|
|
|
}
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
// Sift up
|
|
|
|
size_type i = mElements.Length() - 1;
|
|
|
|
while (i) {
|
|
|
|
size_type parent = (size_type)((i - 1) / 2);
|
|
|
|
if (mCompare.LessThan(mElements[parent], mElements[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Swap(i, parent);
|
|
|
|
i = parent;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-01-12 23:00:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and returns the top-most element from the queue.
|
|
|
|
* @return The topmost element, that is, the element 'a' such that there is no
|
2011-10-17 18:59:28 +04:00
|
|
|
* other element 'b' in the queue for which Compare(b, a) returns true.
|
2010-01-12 23:00:49 +03:00
|
|
|
* @see Top()
|
|
|
|
*/
|
|
|
|
T Pop()
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mElements.IsEmpty(), "Empty queue");
|
2010-01-12 23:00:49 +03:00
|
|
|
T pop = mElements[0];
|
|
|
|
|
|
|
|
// Move last to front
|
|
|
|
mElements[0] = mElements[mElements.Length() - 1];
|
|
|
|
mElements.TruncateLength(mElements.Length() - 1);
|
|
|
|
|
|
|
|
// Sift down
|
|
|
|
size_type i = 0;
|
2014-06-27 05:35:39 +04:00
|
|
|
while (2 * i + 1 < mElements.Length()) {
|
2010-01-12 23:00:49 +03:00
|
|
|
size_type swap = i;
|
2014-06-27 05:35:39 +04:00
|
|
|
size_type l_child = 2 * i + 1;
|
2010-01-12 23:00:49 +03:00
|
|
|
if (mCompare.LessThan(mElements[l_child], mElements[i])) {
|
|
|
|
swap = l_child;
|
|
|
|
}
|
|
|
|
size_type r_child = l_child + 1;
|
|
|
|
if (r_child < mElements.Length() &&
|
|
|
|
mCompare.LessThan(mElements[r_child], mElements[swap])) {
|
|
|
|
swap = r_child;
|
|
|
|
}
|
|
|
|
if (swap == i) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Swap(i, swap);
|
|
|
|
i = swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all elements from the queue.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
void Clear() { mElements.Clear(); }
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides readonly access to the queue elements as an array. Generally this
|
|
|
|
* should be avoided but may be needed in some situations such as when the
|
|
|
|
* elements contained in the queue need to be enumerated for cycle-collection.
|
|
|
|
* @return A pointer to the first element of the array. If the array is
|
|
|
|
* empty, then this pointer must not be dereferenced.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
const T* Elements() const { return mElements.Elements(); }
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Swaps the elements at the specified indices.
|
|
|
|
*/
|
|
|
|
void Swap(size_type aIndexA, size_type aIndexB)
|
|
|
|
{
|
|
|
|
T temp = mElements[aIndexA];
|
|
|
|
mElements[aIndexA] = mElements[aIndexB];
|
|
|
|
mElements[aIndexB] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<T> mElements;
|
|
|
|
Compare mCompare; // Comparator object
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NS_TPRIORITY_QUEUE_H_
|