yapb-noob-edition/ext/crlib/cr-binheap.h

151 lines
3.1 KiB
C
Raw Normal View History

2019-07-27 17:36:24 +03:00
//
// YaPB - Counter-Strike Bot based on PODBot by Markus Klinge.
// Copyright © 2004-2020 YaPB Project <yapb@jeefo.net>.
2019-07-27 17:36:24 +03:00
//
// SPDX-License-Identifier: MIT
2019-07-27 17:36:24 +03:00
//
#pragma once
#include <crlib/cr-array.h>
CR_NAMESPACE_BEGIN
// simple priority queue
template <typename T> class BinaryHeap final : public DenyCopying {
private:
2020-06-12 18:52:38 +03:00
Array <T> contents_;
2019-07-27 17:36:24 +03:00
public:
explicit BinaryHeap () = default;
2020-06-12 18:52:38 +03:00
BinaryHeap (BinaryHeap &&rhs) noexcept : contents_ (cr::move (rhs.contents_))
2019-07-27 17:36:24 +03:00
{ }
~BinaryHeap () = default;
public:
template <typename U> bool push (U &&item) {
2020-06-12 18:52:38 +03:00
if (!contents_.push (cr::move (item))) {
2019-07-27 17:36:24 +03:00
return false;
}
2020-06-12 18:52:38 +03:00
const size_t length = contents_.length ();
2019-07-27 17:36:24 +03:00
if (length > 1) {
percolateUp (length - 1);
}
return true;
}
template <typename ...Args> bool emplace (Args &&...args) {
2020-06-12 18:52:38 +03:00
if (!contents_.emplace (cr::forward <Args> (args)...)) {
2019-07-27 17:36:24 +03:00
return false;
}
2020-06-12 18:52:38 +03:00
const size_t length = contents_.length ();
2019-07-27 17:36:24 +03:00
if (length > 1) {
percolateUp (length - 1);
}
return true;
}
const T &top () const {
2020-06-12 18:52:38 +03:00
return contents_[0];
2019-07-27 17:36:24 +03:00
}
T pop () {
2020-06-12 18:52:38 +03:00
if (contents_.length () == 1) {
return contents_.pop ();
2019-07-27 17:36:24 +03:00
}
2020-06-12 18:52:38 +03:00
auto key (cr::move (contents_[0]));
2019-07-27 17:36:24 +03:00
2020-06-12 18:52:38 +03:00
contents_[0] = cr::move (contents_.last ());
contents_.discard ();
2019-07-27 17:36:24 +03:00
percolateDown (0);
return key;
}
public:
size_t length () const {
2020-06-12 18:52:38 +03:00
return contents_.length ();
2019-07-27 17:36:24 +03:00
}
bool empty () const {
2020-06-12 18:52:38 +03:00
return !contents_.length ();
2019-07-27 17:36:24 +03:00
}
void clear () {
2020-06-12 18:52:38 +03:00
contents_.clear ();
2019-07-27 17:36:24 +03:00
}
private:
void percolateUp (size_t index) {
while (index != 0) {
2020-06-12 18:52:38 +03:00
const size_t parentIndex = parent (index);
2019-07-27 17:36:24 +03:00
2020-06-12 18:52:38 +03:00
if (contents_[parentIndex] > contents_[index]) {
cr::swap (contents_[index], contents_[parentIndex]);
2019-07-29 23:11:49 +03:00
index = parentIndex;
}
else {
2019-07-27 17:36:24 +03:00
break;
}
}
}
void percolateDown (size_t index) {
2019-07-29 23:11:49 +03:00
while (hasLeft (index)) {
size_t bestIndex = left (index);
2019-07-27 17:36:24 +03:00
2019-07-29 23:11:49 +03:00
if (hasRight (index)) {
2020-06-12 18:52:38 +03:00
const size_t rightIndex = right (index);
2019-07-27 17:36:24 +03:00
2020-06-12 18:52:38 +03:00
if (contents_[rightIndex] < contents_[bestIndex]) {
2019-07-29 23:11:49 +03:00
bestIndex = rightIndex;
2019-07-27 17:36:24 +03:00
}
}
2020-06-12 18:52:38 +03:00
if (contents_[index] > contents_[bestIndex]) {
cr::swap (contents_[index], contents_[bestIndex]);
2019-07-29 23:11:49 +03:00
index = bestIndex;
bestIndex = left (index);
}
else {
2019-07-27 17:36:24 +03:00
break;
}
}
}
private:
BinaryHeap &operator = (BinaryHeap &&rhs) noexcept {
if (this != &rhs) {
2020-06-12 18:52:38 +03:00
contents_ = cr::move (rhs.contents_);
2019-07-27 17:36:24 +03:00
}
return *this;
}
private:
2019-07-29 23:11:49 +03:00
static constexpr size_t parent (size_t index) {
return (index - 1) / 2;
}
2019-07-27 17:36:24 +03:00
static constexpr size_t left (size_t index) {
2019-07-29 23:11:49 +03:00
return (index * 2) + 1;
2019-07-27 17:36:24 +03:00
}
static constexpr size_t right (size_t index) {
2019-07-29 23:11:49 +03:00
return (index * 2) + 2;
2019-07-27 17:36:24 +03:00
}
2019-07-29 23:11:49 +03:00
bool hasLeft (size_t index) const {
2020-06-12 18:52:38 +03:00
return left (index) < contents_.length ();
2019-07-29 23:11:49 +03:00
}
bool hasRight (size_t index) const {
2020-06-12 18:52:38 +03:00
return right (index) < contents_.length ();
2019-07-27 17:36:24 +03:00
}
};
CR_NAMESPACE_END