Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynarrayheap.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
38#include <gtest/gtest.h>
39
40#include <tpl_dynArrayHeap.H>
41
42#include <algorithm>
43#include <cstdint>
44#include <queue>
45#include <random>
46#include <stdexcept>
47#include <vector>
48
49using namespace Aleph;
50using namespace testing;
51
52namespace
53{
54 template <typename Heap>
55 std::vector<typename Heap::Item_Type> drain(Heap & h)
56 {
57 std::vector<typename Heap::Item_Type> out;
58 while (!h.is_empty())
59 out.push_back(h.getMin());
60 return out;
61 }
62
63 struct Greater
64 {
65 bool operator()(int a, int b) const noexcept { return a > b; }
66 };
67
68} // namespace
69
76
78{
80 EXPECT_THROW((void)heap.top(), std::underflow_error);
81 EXPECT_THROW((void)static_cast<const DynArrayHeap<int> &>(heap).top(), std::underflow_error);
82 EXPECT_THROW((void)heap.getMin(), std::underflow_error);
83}
84
86{
88 heap.insert(5);
89 heap.insert(2);
90 heap.insert(4);
91 heap.insert(1);
92
93 EXPECT_EQ(heap.size(), 4U);
94 EXPECT_EQ(heap.top(), 1);
95
96 EXPECT_EQ(heap.getMin(), 1);
97 EXPECT_EQ(heap.get(), 2);
98 EXPECT_EQ(heap.getMax(), 4);
99 EXPECT_EQ(heap.getMin(), 5);
100 EXPECT_TRUE(heap.is_empty());
101}
102
104{
106 heap.insert(3);
107 heap.insert(1);
108 EXPECT_THROW(heap.reserve(1), std::out_of_range);
109}
110
112{
114 heap.reserve(16);
115 heap.insert(10);
116 heap.insert_direct(1);
117 heap.insert_direct(5);
118 EXPECT_EQ(heap.top(), 1);
119
120 auto drained = drain(heap);
121 EXPECT_TRUE(std::is_sorted(drained.begin(), drained.end()));
122}
123
125{
127 heap.put(3);
128 heap.append(2);
129 heap.put(1);
130 EXPECT_EQ(heap.top(), 1);
131}
132
134{
136 heap.insert(1);
137 heap.insert(10);
138 heap.insert(3);
139 EXPECT_EQ(heap.top(), 10);
140 EXPECT_EQ(heap.getMin(), 10);
141 EXPECT_EQ(heap.getMin(), 3);
142 EXPECT_EQ(heap.getMin(), 1);
143}
144
146{
148 for (int i = 1; i <= 10; ++i)
149 heap.insert(i);
150
151 std::vector<int> visited;
152 struct Op
153 {
154 std::vector<int> *v;
155 bool operator()(int x)
156 {
157 v->push_back(x);
158 return true;
159 }
160 };
161
162 Op op{&visited};
163 EXPECT_TRUE(heap.traverse(op));
164 EXPECT_EQ(visited.size(), 10U);
165}
166
168{
170 for (int i = 1; i <= 10; ++i)
171 heap.insert(i);
172
173 int count = 0;
174 struct Op
175 {
176 int *c;
177 bool operator()(int)
178 {
179 ++(*c);
180 return *c < 3;
181 }
182 };
183
184 Op op{&count};
185 EXPECT_FALSE(heap.traverse(op));
186 EXPECT_EQ(count, 3);
187}
188
190{
191 std::mt19937_64 rng(0xD15EA5EULL);
192 std::uniform_int_distribution<int> op_dist(0, 99);
193 std::uniform_int_distribution<int> val_dist(-10000, 10000);
194
196 std::priority_queue<int, std::vector<int>, std::greater<int>> ref;
197
198 constexpr int ops = 30000;
199 for (int i = 0; i < ops; ++i)
200 {
201 const int op = op_dist(rng);
202 if (op < 60)
203 {
204 const int v = val_dist(rng);
205 heap.insert(v);
206 ref.push(v);
207 }
208 else
209 {
210 if (ref.empty())
211 {
212 EXPECT_TRUE(heap.is_empty());
213 EXPECT_THROW((void)heap.getMin(), std::underflow_error);
214 }
215 else
216 {
217 ASSERT_FALSE(heap.is_empty());
218 EXPECT_EQ(heap.top(), ref.top());
219 EXPECT_EQ(heap.getMin(), ref.top());
220 ref.pop();
221 }
222 }
223
224 EXPECT_EQ(heap.size(), ref.size());
225 EXPECT_EQ(heap.is_empty(), ref.empty());
226 if (!ref.empty())
227 EXPECT_EQ(heap.top(), ref.top());
228 }
229
230 while (!ref.empty())
231 {
232 ASSERT_FALSE(heap.is_empty());
233 EXPECT_EQ(heap.top(), ref.top());
234 EXPECT_EQ(heap.getMin(), ref.top());
235 ref.pop();
236 }
237 EXPECT_TRUE(heap.is_empty());
238}
long double h
Definition btreepic.C:154
Dynamic heap (priority queue) backed by DynArray.
void reserve(size_t n)
Ensure the underlying array has capacity for at least n elements.
constexpr bool is_empty() const noexcept
Return true if the heap is empty.
T getMin()
Remove and return the top element.
bool traverse(Operation &operation)
Traverse all elements in the heap.
T & put(const T &key)
Alias for insert().
T & append(const T &key)
Alias for insert().
T & insert(const T &key)
Insert a copy of key into the heap.
T & top()
Return the element with highest priority (the heap top).
T & insert_direct(const T &key)
Insert by directly indexing into the backing array.
constexpr size_t size() const noexcept
Return the number of elements.
T & top() const
Definition htlist.H:1683
T & push(const T &item)
Definition htlist.H:1523
void empty() noexcept
empty the list
Definition htlist.H:1689
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
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
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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
Array-based dynamic binary heap.