Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
binheap.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38#include <algorithm>
39#include <functional>
40#include <numeric>
41#include <random>
42#include <vector>
43
44#include <gtest/gtest.h>
45
46#include <tpl_dynBinHeap.H>
47
48using namespace Aleph;
49
51{
52 DynBinHeap<int> heap;
53 const std::vector<int> values = {7, 3, 11, 0, 5, 9, 1};
54
55 for (const int v : values)
56 heap.insert(v);
57
58 std::vector<int> expected = values;
59 std::sort(expected.begin(), expected.end());
60
61 for (const int v : expected)
62 {
63 ASSERT_EQ(heap.top(), v);
64 EXPECT_EQ(heap.getMin(), v);
65 }
66
67 EXPECT_TRUE(heap.is_empty());
68 EXPECT_EQ(heap.size(), 0u);
69}
70
72{
73 DynBinHeap<int> heap;
74 const std::vector<int> values = {40, 10, 30, 20, 50, 60, 70, 15};
75 std::vector<std::reference_wrapper<int>> refs;
76 refs.reserve(values.size());
77
78 for (const int v : values)
79 refs.emplace_back(heap.insert(v));
80
81 ASSERT_EQ(heap.size(), values.size());
82
83 heap.remove(refs[3]); // remove an interior node
84 heap.remove(refs.back()); // remove what used to be the last inserted node
85
86 std::vector<int> remaining;
87 while (!heap.is_empty())
88 remaining.push_back(heap.getMin());
89
90 std::vector<int> expected = values;
91 const auto erase_value = [&expected](int needle) {
92 const auto it = std::find(expected.begin(), expected.end(), needle);
93 ASSERT_NE(it, expected.end());
94 expected.erase(it);
95 };
96 erase_value(20);
97 erase_value(15);
98 std::sort(expected.begin(), expected.end());
99
100 EXPECT_EQ(remaining, expected);
101}
102
104{
105 DynBinHeap<int> heap;
106 auto &high = heap.insert(50);
107 auto &low = heap.insert(5);
108 heap.insert(20);
109
110 high = 1; // becomes the best priority
111 heap.update(high);
112 EXPECT_EQ(heap.top(), 1);
113
114 low = 60; // now should sink
115 heap.update(low);
116
117 std::vector<int> drained;
118 while (!heap.is_empty())
119 drained.push_back(heap.getMin());
120
121 EXPECT_EQ(drained, std::vector<int>({1, 20, 60}));
122}
123
125{
126 DynBinHeap<int> heap;
127 std::vector<int> values(200);
128 std::iota(values.begin(), values.end(), -100);
129 std::mt19937 rng(42);
130 std::shuffle(values.begin(), values.end(), rng);
131
132 for (int v : values)
133 heap.insert(v);
134
135 std::vector<int> extracted;
136 while (!heap.is_empty())
137 extracted.push_back(heap.getMin());
138
139 std::vector<int> expected = values;
140 std::sort(expected.begin(), expected.end());
142}
143
145{
146 DynBinHeap<int> heap;
147 constexpr size_t N = 64;
148 for (size_t i = 0; i < N; ++i)
149 heap.insert(static_cast<int>(i));
150
151 size_t count = 0;
152 for (auto it = heap.get_it(); it.has_curr(); it.next())
153 ++count;
154
155 EXPECT_EQ(count, N);
156}
157
159{
160 DynBinHeap<int> heap;
161 for (int i = 0; i < 50; ++i)
162 heap.insert(i);
163
164 heap.empty();
165 EXPECT_TRUE(heap.is_empty());
166 EXPECT_EQ(heap.size(), 0u);
167
168 // Verify heap can be reused after empty
169 heap.insert(10);
170 EXPECT_EQ(heap.getMin(), 10);
171 EXPECT_TRUE(heap.is_empty());
172}
173
175{
176 DynBinHeap<int> heap;
177 std::vector<std::reference_wrapper<int>> refs;
178 for (int i = 0; i < 5; ++i)
179 refs.emplace_back(heap.insert(10)); // duplicates
180
181 // Make one element the new minimum
182 refs.front().get() = -1;
183 heap.update(refs.front());
184 EXPECT_EQ(heap.top(), -1);
185
186 // Make another element very large to ensure sift-down works
187 refs.back().get() = 100;
188 heap.update(refs.back());
189
190 std::vector<int> drained;
191 while (!heap.is_empty())
192 drained.push_back(heap.getMin());
193
194 EXPECT_TRUE(std::is_sorted(drained.begin(), drained.end()));
195 EXPECT_EQ(drained.front(), -1);
196 EXPECT_EQ(drained.back(), 100);
197}
Dynamic heap of elements of type T ordered by a comparison functor.
T & top() const
Return a reference to the smallest element.
T getMin()
Remove the minimum element (according to Compare) and return it.
void update(T &data) noexcept
Adjust the position of an element after mutating its priority.
void empty() noexcept
Remove every element.
bool remove(T &data) noexcept
Remove an arbitrary element belonging to the heap.
T & insert(const T &item)
Insert a copy of item into the heap.
bool is_empty() const noexcept
const size_t & size() const noexcept
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Dynamic binary heap with node-based storage.