Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynBinHeap.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <cstdlib>
29# include <ctime>
30# include <cstring>
31# include <iostream>
32# include <tpl_dynBinHeap.H>
33
34# include <cassert>
35using namespace std;
36
37int keys [] = { 36, 32, 4, 12, 52, 59, 2, 2, 26, 1 };
38
39struct Record
40{
41 char buf[10];
42 int i;
43
44 Record (int n) : i(n)
45 {
46 std::memset(buf, 0, sizeof(buf));
47 }
48
49 virtual ~Record() { }
50
51 virtual void foo() = 0;
52};
53
54
55struct Rec : public Record
56{
57 Rec(int j) : Record(j) {}
58
59 virtual void foo()
60 {
61
62 }
63
64 virtual ~Rec() {}
65};
66
67struct Cmp_Rec
68{
69 bool operator () (const Record & l, const Record & r) const
70 {
71 return l.i < r.i;
72 }
73
74 bool operator () (Record * l, Record * r) const
75 {
76 return l->i < r->i;
77 }
78};
79
80struct Cmp_Ptr
81{
82 bool operator () (unsigned int* p1, unsigned int* p2) const
83 {
84 return *p1 < *p2;
85 }
86};
87
88
89int main(int argc, char* argv[])
90{
91 MESSAGE("main()");
92 srand(time(nullptr));
93 unsigned int n = 10;
94
95 if (argc > 1)
96 n = static_cast<unsigned int>(atoi(argv[1]));
97
98 {
99 cout << "Testing with predefined keys array" << endl;
100 DynBinHeap<int> heap;
101 for (int key : keys)
102 heap.insert(key);
103
104 assert(heap.verify_heap());
105 cout << "Predefined keys heap size: " << heap.size() << endl;
106 }
107
108 {
109 DynBinHeap<int> heap;
110 unsigned int i, value;
111
112 for (i = 0; i < n; i++)
113 {
114 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
115 cout << value << ", ";
116 heap.insert(value);
117 }
118
119 assert(heap.verify_heap());
120
121 cout << "\n\n"
122 << "Sum = " << heap.fold(0, [] (const int acu, const int i)
123 {
124 return acu + i;
125 });
126 cout << endl
127 << endl;
128 }
129
130 {
131 cout << "testing with pointers" << endl;
132
134 unsigned int i, value;
135 unsigned int *ptr;
136
137 for (i = 0; i < n; i++)
138 {
139 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
140 ptr = new unsigned int;
141 *ptr = value;
142 cout << value << ", ";
143 heap.insert(ptr);
144 }
145
146 cout << "\n\n"
147 << "Extracting from heap" << endl;
148
149 for (i = 0; i < n; i++)
150 {
151 ptr = heap.getMin();
152 value = *ptr;
153 cout << value << " ";
154 delete ptr;
155 }
156
157 cout << "\n\n";
158 }
159
160 {
161 cout << "testing with pointers to record" << endl;
162
164 unsigned int i, value;
165 Rec *ptr;
166
167 for (i = 0; i < n; i++)
168 {
169 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
170 ptr = new Rec(value);
171 cout << value << ", ";
172 heap.insert(ptr);
173 }
174
175 cout << "\n\n"
176 << "Extracting from heap" << endl;
177
178 for (i = 0; i < n; i++)
179 {
180 ptr = static_cast<Rec*>(heap.getMin());
181 value = ptr->i;
182 cout << value << " ";
183 delete ptr;
184 }
185
186 cout << "\n\n";
187 }
188
189}
#define MESSAGE(format, args...)
Print an informational message (no-op when MESSAGES is not defined).
Definition ahDefs.H:259
Dynamic heap of elements of type T ordered by a comparison functor.
T getMin()
Remove the minimum element (according to Compare) and return it.
T & insert(const T &item)
Insert a copy of item into the heap.
virtual bool verify_heap(Node *p) const
const size_t & size() const noexcept
T fold(const T &init, Operation &operation) const
Simplified version of foldl() where the folded type is the same type of elements stored in the contai...
Definition ah-dry.H:1267
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
bool operator()(unsigned int *p1, unsigned int *p2) const
bool operator()(const Record &l, const Record &r) const
virtual ~Rec()
virtual void foo()
Rec(int j)
virtual void foo()=0
Record(int n)
virtual ~Record()
char buf[10]
int keys[]
gsl_rng * r
Dynamic binary heap with node-based storage.
DynList< int > l