Skip to content

Truly independent web browser

AK: Remove BinaryHeap

Shannon Booth00f0bb044c62
diff --git a/AK/BinaryHeap.h b/AK/BinaryHeap.h
index 9df9c2e4212..dc4ebfb837f 100644
--- a/AK/BinaryHeap.h
+++ b/AK/BinaryHeap.h
@@ -19,13 +19,6 @@ class IntrusiveBinaryHeap {
public:
IntrusiveBinaryHeap() = default;
- IntrusiveBinaryHeap(Vector<Node, inline_capacity>&& nodes)
- : m_nodes(move(nodes))
- {
- for (ssize_t i = m_nodes.size() / 2; i--;)
- heapify_down(i);
- }
-
[[nodiscard]] size_t size() const { return m_nodes.size(); }
[[nodiscard]] bool is_empty() const { return m_nodes.is_empty(); }
@@ -116,66 +109,8 @@ private:
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 {
- K key;
- V value;
- };
-
- IntrusiveBinaryHeap<
- Node,
- decltype([](Node const& a, Node const& b) { return a.key < b.key; }),
- decltype([](Node&, size_t) { })>
- m_heap;
-};
-
}
#if USING_AK_GLOBALLY
-using AK::BinaryHeap;
using AK::IntrusiveBinaryHeap;
#endif
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index 38b2d5966a3..0bcbccf7cff 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -7,7 +7,6 @@ set(AK_TEST_SOURCES
TestAtomic.cpp
TestBadge.cpp
TestBase64.cpp
- TestBinaryHeap.cpp
TestBinarySearch.cpp
TestBitCast.cpp
TestBitmap.cpp
diff --git a/Tests/AK/TestBinaryHeap.cpp b/Tests/AK/TestBinaryHeap.cpp
deleted file mode 100644
index 89127e7bb9f..00000000000
--- a/Tests/AK/TestBinaryHeap.cpp
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2021, Idan Horowitz <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibTest/TestCase.h>
-
-#include <AK/BinaryHeap.h>
-#include <AK/ByteString.h>
-
-using namespace Test::Randomized;
-
-TEST_CASE(construct)
-{
- BinaryHeap<int, int, 5> empty;
- EXPECT(empty.is_empty());
- EXPECT(empty.size() == 0);
-}
-
-TEST_CASE(construct_from_existing)
-{
- int keys[] = { 3, 2, 1 };
- char values[] = { 'c', 'b', 'a' };
- BinaryHeap<int, char, 5> from_existing(keys, values, 3);
- EXPECT(from_existing.size() == 3);
- EXPECT_EQ(from_existing.pop_min(), 'a');
- EXPECT_EQ(from_existing.pop_min(), 'b');
- EXPECT_EQ(from_existing.pop_min(), 'c');
-}
-
-TEST_CASE(populate_int)
-{
- BinaryHeap<int, int, 5> ints;
- ints.insert(1, 10);
- ints.insert(3, 20);
- ints.insert(2, 30);
- EXPECT_EQ(ints.size(), 3u);
- EXPECT_EQ(ints.pop_min(), 10);
- EXPECT_EQ(ints.size(), 2u);
- EXPECT_EQ(ints.pop_min(), 30);
- EXPECT_EQ(ints.size(), 1u);
- EXPECT_EQ(ints.pop_min(), 20);
- EXPECT_EQ(ints.size(), 0u);
-}
-
-TEST_CASE(populate_string)
-{
- BinaryHeap<int, ByteString, 5> strings;
- strings.insert(1, "ABC");
- strings.insert(2, "DEF");
- EXPECT_EQ(strings.size(), 2u);
- EXPECT_EQ(strings.pop_min(), "ABC");
- EXPECT_EQ(strings.pop_min(), "DEF");
- EXPECT(strings.is_empty());
-}
-
-TEST_CASE(large_populate_reverse)
-{
- BinaryHeap<int, int, 1024> ints;
- for (int i = 1023; i >= 0; i--) {
- ints.insert(i, i);
- }
- EXPECT_EQ(ints.size(), 1024u);
- for (int i = 0; i < 1024; i++) {
- EXPECT_EQ(ints.peek_min(), i);
- EXPECT_EQ(ints.pop_min(), i);
- }
-}
-
-RANDOMIZED_TEST_CASE(pop_min_is_min)
-{
- GEN(vec, Gen::vector(1, 10, []() { return Gen::number_u64(); }));
-
- auto sorted { vec };
- AK::quick_sort(sorted);
-
- BinaryHeap<u64, u64, 10> heap;
-
- // insert in a non-sorted order
- for (u64 n : vec) {
- heap.insert(n, n);
- }
-
- // check in a sorted order
- for (u64 sorted_n : sorted) {
- EXPECT_EQ(heap.pop_min(), sorted_n);
- }
-}
-
-RANDOMIZED_TEST_CASE(peek_min_same_as_pop_min)
-{
- GEN(vec, Gen::vector(1, 10, []() { return Gen::number_u64(); }));
- BinaryHeap<u64, u64, 10> heap;
- for (u64 n : vec) {
- heap.insert(n, n);
- }
-
- while (!heap.is_empty()) {
- u64 peeked = heap.peek_min();
- u64 popped = heap.pop_min();
- EXPECT_EQ(peeked, popped);
- }
-}