2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct HeapNode {
|
|
|
|
bool in_heap() const {
|
|
|
|
return pos_ != -1;
|
|
|
|
}
|
|
|
|
bool is_top() const {
|
|
|
|
return pos_ == 0;
|
|
|
|
}
|
|
|
|
void remove() {
|
|
|
|
pos_ = -1;
|
|
|
|
}
|
2021-08-15 09:59:12 +02:00
|
|
|
int32 pos_ = -1;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class KeyT, int K = 4>
|
|
|
|
class KHeap {
|
|
|
|
public:
|
|
|
|
bool empty() const {
|
|
|
|
return array_.empty();
|
|
|
|
}
|
|
|
|
size_t size() const {
|
|
|
|
return array_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyT top_key() const {
|
|
|
|
return array_[0].key_;
|
|
|
|
}
|
|
|
|
|
2021-08-15 10:15:14 +02:00
|
|
|
KeyT get_key(const HeapNode *node) const {
|
|
|
|
size_t pos = static_cast<size_t>(node->pos_);
|
|
|
|
CHECK(pos < array_.size());
|
|
|
|
return array_[pos].key_;
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
const HeapNode *top() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return array_[0].node_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
HeapNode *pop() {
|
|
|
|
CHECK(!empty());
|
|
|
|
HeapNode *result = array_[0].node_;
|
|
|
|
result->remove();
|
2021-08-15 09:59:12 +02:00
|
|
|
erase(static_cast<size_t>(0));
|
2018-12-31 20:04:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(KeyT key, HeapNode *node) {
|
|
|
|
CHECK(!node->in_heap());
|
|
|
|
array_.push_back({key, node});
|
2021-08-15 09:59:12 +02:00
|
|
|
fix_up(array_.size() - 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void fix(KeyT key, HeapNode *node) {
|
2021-08-15 09:59:12 +02:00
|
|
|
size_t pos = static_cast<size_t>(node->pos_);
|
|
|
|
CHECK(pos < array_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
KeyT old_key = array_[pos].key_;
|
|
|
|
array_[pos].key_ = key;
|
|
|
|
if (key < old_key) {
|
|
|
|
fix_up(pos);
|
|
|
|
} else {
|
|
|
|
fix_down(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void erase(HeapNode *node) {
|
2021-08-15 09:59:12 +02:00
|
|
|
size_t pos = static_cast<size_t>(node->pos_);
|
2018-12-31 20:04:05 +01:00
|
|
|
node->remove();
|
2021-08-15 09:59:12 +02:00
|
|
|
CHECK(pos < array_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
erase(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
2019-07-06 13:29:15 +02:00
|
|
|
void for_each(F &&f) const {
|
|
|
|
for (auto &it : array_) {
|
|
|
|
f(it.key_, it.node_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
void for_each(F &&f) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : array_) {
|
|
|
|
f(it.key_, it.node_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check() const {
|
|
|
|
for (size_t i = 0; i < array_.size(); i++) {
|
|
|
|
for (size_t j = i * K + 1; j < i * K + 1 + K && j < array_.size(); j++) {
|
2019-02-12 21:48:16 +01:00
|
|
|
CHECK(array_[i].key_ <= array_[j].key_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Item {
|
|
|
|
KeyT key_;
|
|
|
|
HeapNode *node_;
|
|
|
|
};
|
|
|
|
vector<Item> array_;
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
void fix_up(size_t pos) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto item = array_[pos];
|
|
|
|
|
|
|
|
while (pos) {
|
2021-08-15 09:59:12 +02:00
|
|
|
auto parent_pos = (pos - 1) / K;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto parent_item = array_[parent_pos];
|
|
|
|
|
|
|
|
if (parent_item.key_ < item.key_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
parent_item.node_->pos_ = static_cast<int32>(pos);
|
2018-12-31 20:04:05 +01:00
|
|
|
array_[pos] = parent_item;
|
|
|
|
pos = parent_pos;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
item.node_->pos_ = static_cast<int32>(pos);
|
2018-12-31 20:04:05 +01:00
|
|
|
array_[pos] = item;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
void fix_down(size_t pos) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto item = array_[pos];
|
|
|
|
while (true) {
|
2021-08-15 09:59:12 +02:00
|
|
|
auto left_pos = pos * K + 1;
|
|
|
|
auto right_pos = min(left_pos + K, array_.size());
|
|
|
|
auto next_pos = pos;
|
2018-12-31 20:04:05 +01:00
|
|
|
KeyT next_key = item.key_;
|
2021-08-15 09:59:12 +02:00
|
|
|
for (auto i = left_pos; i < right_pos; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
KeyT i_key = array_[i].key_;
|
|
|
|
if (i_key < next_key) {
|
|
|
|
next_key = i_key;
|
|
|
|
next_pos = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next_pos == pos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
array_[pos] = array_[next_pos];
|
2021-08-15 09:59:12 +02:00
|
|
|
array_[pos].node_->pos_ = static_cast<int32>(pos);
|
2018-12-31 20:04:05 +01:00
|
|
|
pos = next_pos;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
item.node_->pos_ = static_cast<int32>(pos);
|
2018-12-31 20:04:05 +01:00
|
|
|
array_[pos] = item;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:59:12 +02:00
|
|
|
void erase(size_t pos) {
|
2018-12-31 20:04:05 +01:00
|
|
|
array_[pos] = array_.back();
|
|
|
|
array_.pop_back();
|
2021-08-15 09:59:12 +02:00
|
|
|
if (pos < array_.size()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
fix_down(pos);
|
|
|
|
fix_up(pos);
|
|
|
|
}
|
2021-08-15 09:46:41 +02:00
|
|
|
if (array_.capacity() > 50 && array_.size() < array_.capacity() / 4) {
|
|
|
|
array_.shrink_to_fit();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|