gecko-dev/xpcom/ds/nsTPriorityQueue.h
Benjamin Smedberg 2b3617b084 Bug 1332631 part C - file moves from xpcom/glue to xpcom/ds, r=froydnj
MozReview-Commit-ID: 9ZhC5bROWdO

--HG--
rename : xpcom/glue/Observer.h => xpcom/ds/Observer.h
rename : xpcom/glue/PLDHashTable.cpp => xpcom/ds/PLDHashTable.cpp
rename : xpcom/glue/PLDHashTable.h => xpcom/ds/PLDHashTable.h
rename : xpcom/glue/nsArrayEnumerator.cpp => xpcom/ds/nsArrayEnumerator.cpp
rename : xpcom/glue/nsArrayEnumerator.h => xpcom/ds/nsArrayEnumerator.h
rename : xpcom/glue/nsArrayUtils.cpp => xpcom/ds/nsArrayUtils.cpp
rename : xpcom/glue/nsArrayUtils.h => xpcom/ds/nsArrayUtils.h
rename : xpcom/glue/nsBaseHashtable.h => xpcom/ds/nsBaseHashtable.h
rename : xpcom/glue/nsCOMArray.cpp => xpcom/ds/nsCOMArray.cpp
rename : xpcom/glue/nsCOMArray.h => xpcom/ds/nsCOMArray.h
rename : xpcom/glue/nsClassHashtable.h => xpcom/ds/nsClassHashtable.h
rename : xpcom/glue/nsDataHashtable.h => xpcom/ds/nsDataHashtable.h
rename : xpcom/glue/nsDeque.cpp => xpcom/ds/nsDeque.cpp
rename : xpcom/glue/nsDeque.h => xpcom/ds/nsDeque.h
rename : xpcom/glue/nsEnumeratorUtils.cpp => xpcom/ds/nsEnumeratorUtils.cpp
rename : xpcom/glue/nsEnumeratorUtils.h => xpcom/ds/nsEnumeratorUtils.h
rename : xpcom/glue/nsHashKeys.h => xpcom/ds/nsHashKeys.h
rename : xpcom/glue/nsInterfaceHashtable.h => xpcom/ds/nsInterfaceHashtable.h
rename : xpcom/glue/nsJSThingHashtable.h => xpcom/ds/nsJSThingHashtable.h
rename : xpcom/glue/nsPointerHashKeys.h => xpcom/ds/nsPointerHashKeys.h
rename : xpcom/glue/nsQuickSort.cpp => xpcom/ds/nsQuickSort.cpp
rename : xpcom/glue/nsQuickSort.h => xpcom/ds/nsQuickSort.h
rename : xpcom/glue/nsRefPtrHashtable.h => xpcom/ds/nsRefPtrHashtable.h
rename : xpcom/glue/nsTArray-inl.h => xpcom/ds/nsTArray-inl.h
rename : xpcom/glue/nsTArray.cpp => xpcom/ds/nsTArray.cpp
rename : xpcom/glue/nsTArray.h => xpcom/ds/nsTArray.h
rename : xpcom/glue/nsTArrayForwardDeclare.h => xpcom/ds/nsTArrayForwardDeclare.h
rename : xpcom/glue/nsTHashtable.h => xpcom/ds/nsTHashtable.h
rename : xpcom/glue/nsTObserverArray.cpp => xpcom/ds/nsTObserverArray.cpp
rename : xpcom/glue/nsTObserverArray.h => xpcom/ds/nsTObserverArray.h
rename : xpcom/glue/nsTPriorityQueue.h => xpcom/ds/nsTPriorityQueue.h
extra : rebase_source : 5f0638e3268acb932e1a8d3f499d283bc1922acd
extra : histedit_source : dcea9485885877bb02f95a1ecc627465a507f757
2017-01-20 13:59:21 -05:00

162 lines
4.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#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.
*/
template<class T, class Compare = nsDefaultComparator<T, T>>
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.
*/
nsTPriorityQueue() : mCompare(Compare()) {}
/**
* Constructor to allow a specific instance of a comparator object to be
* used.
*/
explicit nsTPriorityQueue(const Compare& aComp) : mCompare(aComp) {}
/**
* Copy constructor
*/
nsTPriorityQueue(const nsTPriorityQueue& aOther)
: mElements(aOther.mElements)
, mCompare(aOther.mCompare)
{
}
/**
* @return True if the queue is empty or false otherwise.
*/
bool IsEmpty() const { return mElements.IsEmpty(); }
/**
* @return The number of elements in the queue.
*/
size_type Length() const { return mElements.Length(); }
/**
* @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
* which Compare(b, a) returns true. (Since this container does not check
* for duplicate entries there may exist 'b' for which Compare(a, b) returns
* false.)
*/
const T& Top() const
{
MOZ_ASSERT(!mElements.IsEmpty(), "Empty queue");
return mElements[0];
}
/**
* Adds an element to the queue
* @param aElement The element to add
* @return true on success, false on out of memory.
*/
bool Push(const T& aElement)
{
T* elem = mElements.AppendElement(aElement);
if (!elem) {
return false; // Out of memory
}
// 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;
}
return true;
}
/**
* Removes and returns the top-most element from the queue.
* @return The topmost element, that is, the element 'a' such that there is no
* other element 'b' in the queue for which Compare(b, a) returns true.
* @see Top()
*/
T Pop()
{
MOZ_ASSERT(!mElements.IsEmpty(), "Empty queue");
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;
while (2 * i + 1 < mElements.Length()) {
size_type swap = i;
size_type l_child = 2 * i + 1;
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.
*/
void Clear() { mElements.Clear(); }
/**
* 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.
*/
const T* Elements() const { return mElements.Elements(); }
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_