/** Inserts a new item in the queue based on its priority. */ void insert(T entry) {
this->validate(); int index = fArray.size();
*fArray.append() = entry;
this->setIndex(fArray.size() - 1);
this->percolateUpIfNecessary(index);
this->validate();
}
/** Random access removal. This requires that the INDEX function is non-nullptr. */ void remove(T entry) {
SkASSERT(nullptr != INDEX); int index = *INDEX(entry);
SkASSERT(index >= 0 && index < fArray.size());
this->validate();
SkDEBUGCODE(*INDEX(fArray[index]) = -1;) if (index == fArray.size() - 1) {
fArray.pop_back(); return;
}
fArray[index] = fArray[fArray.size() - 1];
fArray.pop_back();
this->setIndex(index);
this->percolateUpOrDown(index);
this->validate();
}
/** Notification that the priority of an entry has changed. This must be called after an item'spriorityischangedtomaintaincorrectordering.Changingthepriorityisonly
allowed if an INDEX function is provided. */ void priorityDidChange(T entry) {
SkASSERT(nullptr != INDEX); int index = *INDEX(entry);
SkASSERT(index >= 0 && index < fArray.size());
this->validate(index);
this->percolateUpOrDown(index);
this->validate();
}
/** Gets the item at index i in the priority queue (for i < this->count()). at(0) is equivalent
to peek(). Otherwise, there is no guarantee about ordering of elements in the queue. */
T at(int i) const { return fArray[i]; }
/** Sorts the queue into priority order. The queue is only guarenteed to remain in sorted order *untilanyotheroperation,otherthanat(),isperformed.
*/ void sort() { if (fArray.size() > 1) {
SkTQSort<T>(fArray.begin(), fArray.end(), LESS); for (int i = 0; i < fArray.size(); i++) {
this->setIndex(i);
}
this->validate();
}
}
bool percolateUpIfNecessary(int index) {
SkASSERT(index >= 0); bool percolated = false; do { if (0 == index) {
this->setIndex(index); return percolated;
} int p = ParentOf(index); if (LESS(fArray[index], fArray[p])) {
using std::swap;
swap(fArray[index], fArray[p]);
this->setIndex(index);
index = p;
percolated = true;
} else {
this->setIndex(index); return percolated;
}
this->validate(index);
} while (true);
}
void percolateDownIfNecessary(int index) {
SkASSERT(index >= 0); do { int child = LeftOf(index);
if (child >= fArray.size()) { // We're a leaf.
this->setIndex(index); return;
}
if (child + 1 >= fArray.size()) { // We only have a left child. if (LESS(fArray[child], fArray[index])) {
using std::swap;
swap(fArray[child], fArray[index]);
this->setIndex(child);
this->setIndex(index); return;
}
} elseif (LESS(fArray[child + 1], fArray[child])) { // The right child is the one we should swap with, if we swap.
child++;
}
// Check if we need to swap. if (LESS(fArray[child], fArray[index])) {
using std::swap;
swap(fArray[child], fArray[index]);
this->setIndex(index);
index = child;
} else { // We're less than both our children.
this->setIndex(index); return;
}
this->validate(index);
} while (true);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.