2021-03-04 00:05:34 +03:00
|
|
|
/*
|
2021-04-22 23:40:43 +03:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2021-03-04 00:05:34 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-04 00:05:34 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/Vector.h>
|
2021-09-16 01:00:33 +03:00
|
|
|
|
2021-03-04 00:05:34 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
template<typename Node, typename Comparator, typename IndexSetter, size_t inline_capacity = 0>
|
|
|
|
class IntrusiveBinaryHeap {
|
|
|
|
AK_MAKE_DEFAULT_COPYABLE(IntrusiveBinaryHeap);
|
|
|
|
AK_MAKE_DEFAULT_MOVABLE(IntrusiveBinaryHeap);
|
|
|
|
|
2021-03-04 00:05:34 +03:00
|
|
|
public:
|
2024-02-25 02:31:15 +03:00
|
|
|
IntrusiveBinaryHeap() = default;
|
2021-03-04 00:05:34 +03:00
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
IntrusiveBinaryHeap(Vector<Node, inline_capacity>&& nodes)
|
|
|
|
: m_nodes(move(nodes))
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
for (ssize_t i = m_nodes.size() / 2; i--;)
|
2021-03-04 00:05:34 +03:00
|
|
|
heapify_down(i);
|
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
[[nodiscard]] size_t size() const { return m_nodes.size(); }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_nodes.is_empty(); }
|
2021-03-04 00:05:34 +03:00
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
void insert(Node const& node)
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
m_nodes.append(node);
|
|
|
|
IndexSetter {}(m_nodes.last(), m_nodes.size() - 1);
|
|
|
|
heapify_up(m_nodes.size() - 1);
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
void insert(Node&& node)
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
m_nodes.append(move(node));
|
|
|
|
IndexSetter {}(m_nodes.last(), m_nodes.size() - 1);
|
|
|
|
heapify_up(m_nodes.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node pop(size_t i)
|
|
|
|
{
|
|
|
|
while (i != 0) {
|
|
|
|
swap_indices(i, (i - 1) / 2);
|
|
|
|
i = (i - 1) / 2;
|
|
|
|
}
|
|
|
|
swap_indices(0, m_nodes.size() - 1);
|
|
|
|
Node node = m_nodes.take_last();
|
2021-03-04 00:05:34 +03:00
|
|
|
heapify_down(0);
|
2024-02-25 02:31:15 +03:00
|
|
|
return node;
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
Node pop_min()
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
return pop(0);
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
Node const& peek_min() const
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
return m_nodes[0];
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
m_nodes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadonlySpan<Node> nodes_in_arbitrary_order() const
|
|
|
|
{
|
|
|
|
return m_nodes;
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-02-25 02:31:15 +03:00
|
|
|
void swap_indices(size_t i, size_t j)
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
swap(m_nodes[i], m_nodes[j]);
|
|
|
|
IndexSetter {}(m_nodes[i], i);
|
|
|
|
IndexSetter {}(m_nodes[j], j);
|
|
|
|
}
|
2021-03-04 00:05:34 +03:00
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
bool compare_indices(size_t i, size_t j)
|
|
|
|
{
|
|
|
|
return Comparator {}(m_nodes[i], m_nodes[j]);
|
|
|
|
}
|
2021-03-04 00:05:34 +03:00
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
void heapify_up(size_t i)
|
|
|
|
{
|
|
|
|
while (i != 0) {
|
|
|
|
auto parent = (i - 1) / 2;
|
|
|
|
if (compare_indices(parent, i))
|
2021-03-04 00:05:34 +03:00
|
|
|
break;
|
2024-02-25 02:31:15 +03:00
|
|
|
swap_indices(i, parent);
|
|
|
|
i = parent;
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
void heapify_down(size_t i)
|
2021-03-04 00:05:34 +03:00
|
|
|
{
|
2024-02-25 02:31:15 +03:00
|
|
|
while (i * 2 + 1 < size()) {
|
|
|
|
size_t min_child = i * 2 + 1;
|
|
|
|
size_t other_child = i * 2 + 2;
|
|
|
|
if (other_child < size() && compare_indices(other_child, min_child))
|
|
|
|
min_child = other_child;
|
|
|
|
if (compare_indices(i, min_child))
|
2021-03-04 00:05:34 +03:00
|
|
|
break;
|
2024-02-25 02:31:15 +03:00
|
|
|
swap_indices(i, min_child);
|
|
|
|
i = min_child;
|
2021-03-04 00:05:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 02:31:15 +03:00
|
|
|
Vector<Node, inline_capacity> m_nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename K, typename V, size_t inline_capacity>
|
|
|
|
class BinaryHeap {
|
|
|
|
public:
|
|
|
|
BinaryHeap() = default;
|
|
|
|
~BinaryHeap() = default;
|
|
|
|
|
|
|
|
// This constructor allows for O(n) construction of the heap (instead of O(nlogn) for repeated insertions)
|
|
|
|
BinaryHeap(K keys[], V values[], size_t size)
|
|
|
|
{
|
|
|
|
Vector<Node, inline_capacity> nodes;
|
|
|
|
nodes.ensure_capacity(size);
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
nodes.unchecked_append({ keys[i], values[i] });
|
|
|
|
m_heap = decltype(m_heap) { move(nodes) };
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t size() const { return m_heap.size(); }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_heap.is_empty(); }
|
|
|
|
|
|
|
|
void insert(K key, V value)
|
|
|
|
{
|
|
|
|
m_heap.insert({ key, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
V pop_min()
|
|
|
|
{
|
|
|
|
return m_heap.pop_min().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] V const& peek_min() const
|
|
|
|
{
|
|
|
|
return m_heap.peek_min().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] K const& peek_min_key() const
|
|
|
|
{
|
|
|
|
return m_heap.peek_min().key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_heap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node {
|
2021-03-14 00:28:30 +03:00
|
|
|
K key;
|
|
|
|
V value;
|
2024-02-25 02:31:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
IntrusiveBinaryHeap<
|
|
|
|
Node,
|
|
|
|
decltype([](Node const& a, Node const& b) { return a.key < b.key; }),
|
|
|
|
decltype([](Node&, size_t) {})>
|
|
|
|
m_heap;
|
2021-03-04 00:05:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-03-04 00:05:34 +03:00
|
|
|
using AK::BinaryHeap;
|
2024-02-25 02:31:15 +03:00
|
|
|
using AK::IntrusiveBinaryHeap;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|