Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
arrayheap_algos.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 <algorithm>
39#include <ranges>
40#include <vector>
41
42#include <gtest/gtest.h>
43
44#include <tpl_arrayHeap.H>
45
46using namespace Aleph;
47using namespace testing;
48
49static bool is_min_heap(const std::vector<int> &v)
50{
51 if (v.empty())
52 return true;
53
54 std::vector<int> tmp(v.size() + 1);
55 for (size_t i = 0; i < v.size(); ++i)
56 tmp[i + 1] = v[i];
57
59 return valid_heap(tmp.data(), 1, v.size(), cmp);
60}
61
63{
64 {
65 std::vector<int> heap_like{1, 3, 2, 7, 5, 4};
67 }
68
69 {
70 std::vector<int> not_heap{3, 1, 2};
72 }
73}
74
76{
77 // Start from a valid min-heap of size 3: [1, 3, 2]
78 int arr[5] = {0, 1, 3, 2, 0};
80
81 // Insert a smaller item at the end and sift it up
82 arr[4] = 0;
83 int &ref = sift_up(arr, 1, 4, cmp);
84
85 EXPECT_EQ(ref, 0);
86 EXPECT_TRUE(valid_heap(arr, 1, 4, cmp));
87 EXPECT_EQ(arr[1], 0);
88}
89
91{
92 // A min-heap of 5 elements would have 1 at root, but we break it.
93 int arr[6] = {0, 9, 1, 2, 3, 4};
95
96 EXPECT_FALSE(valid_heap(arr, 1, 5, cmp));
97 sift_down(arr, 1, 5, cmp);
98 EXPECT_TRUE(valid_heap(arr, 1, 5, cmp));
99 EXPECT_EQ(arr[1], 1);
100}
101
103{
104 // Start with a valid heap
105 int arr[8] = {0, 1, 3, 2, 7, 5, 4, 8};
107 ASSERT_TRUE(valid_heap(arr, 1, 7, cmp));
108
109 // Make an internal node very small; it should bubble up.
110 arr[6] = 0;
111 sift_down_up(arr, 1, 6, 7, cmp);
112 EXPECT_TRUE(valid_heap(arr, 1, 7, cmp));
113 EXPECT_EQ(arr[1], 0);
114
115 // Make an internal node very large; it should sink.
116 arr[2] = 100;
117 sift_down_up(arr, 1, 2, 7, cmp);
118 EXPECT_TRUE(valid_heap(arr, 1, 7, cmp));
119}
120
122{
123 std::vector<int> v{5, 1, 4, 2, 8, 0, 3, 7, 6, 9};
124 std::vector<int> expected = v;
125 std::sort(expected.begin(), expected.end());
126
127 heapsort(v.data(), v.size());
129}
130
132{
133 std::vector<int> v{12, -1, 5, 5, 3, 99, 0, -10, 7, 2, 4};
134 std::vector<int> expected = v;
135 std::sort(expected.begin(), expected.end());
136
137 faster_heapsort(v.data(), v.size());
139}
static bool is_min_heap(const std::vector< int > &v)
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)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r, Compare &cmp)
Restore the heap property by sifting down and then sifting up.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
bool valid_heap(T *array, const size_t l, const size_t r, const Compare &cmp=Compare())
Check whether a range satisfies the heap property.
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Fixed-capacity binary heap and heapsort algorithms.