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 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <cstdlib>
34# include <ctime>
35# include <cstring>
36# include <iostream>
37# include <tpl_dynBinHeap.H>
38
39# include <cassert>
40using namespace std;
41
42int keys [] = { 36, 32, 4, 12, 52, 59, 2, 2, 26, 1 };
43
44struct Record
45{
46 char buf[10];
47 int i;
48
49 Record (int n) : i(n)
50 {
51 std::memset(buf, 0, sizeof(buf));
52 }
53
54 virtual ~Record() { }
55
56 virtual void foo() = 0;
57};
58
59
60struct Rec : public Record
61{
62 Rec(int j) : Record(j) {}
63
64 virtual void foo()
65 {
66
67 }
68
69 virtual ~Rec() {}
70};
71
72struct Cmp_Rec
73{
74 bool operator () (const Record & l, const Record & r) const
75 {
76 return l.i < r.i;
77 }
78
79 bool operator () (Record * l, Record * r) const
80 {
81 return l->i < r->i;
82 }
83};
84
85struct Cmp_Ptr
86{
87 bool operator () (unsigned int* p1, unsigned int* p2) const
88 {
89 return *p1 < *p2;
90 }
91};
92
93
94int main(int argc, char* argv[])
95{
96 MESSAGE("main()");
97 srand(time(nullptr));
98 unsigned int n = 10;
99
100 if (argc > 1)
101 n = static_cast<unsigned int>(atoi(argv[1]));
102
103 {
104 cout << "Testing with predefined keys array" << endl;
105 DynBinHeap<int> heap;
106 for (int key : keys)
107 heap.insert(key);
108
109 assert(heap.verify_heap());
110 cout << "Predefined keys heap size: " << heap.size() << endl;
111 }
112
113 {
114 DynBinHeap<int> heap;
115 unsigned int i, value;
116
117 for (i = 0; i < n; i++)
118 {
119 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
120 cout << value << ", ";
121 heap.insert(value);
122 }
123
124 assert(heap.verify_heap());
125
126 cout << "\n\n"
127 << "Sum = " << heap.fold(0, [] (const int acu, const int i)
128 {
129 return acu + i;
130 });
131 cout << endl
132 << endl;
133 }
134
135 {
136 cout << "testing with pointers" << endl;
137
139 unsigned int i, value;
140 unsigned int *ptr;
141
142 for (i = 0; i < n; i++)
143 {
144 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
145 ptr = new unsigned int;
146 *ptr = value;
147 cout << value << ", ";
148 heap.insert(ptr);
149 }
150
151 cout << "\n\n"
152 << "Extracting from heap" << endl;
153
154 for (i = 0; i < n; i++)
155 {
156 ptr = heap.getMin();
157 value = *ptr;
158 cout << value << " ";
159 delete ptr;
160 }
161
162 cout << "\n\n";
163 }
164
165 {
166 cout << "testing with pointers to record" << endl;
167
169 unsigned int i, value;
170 Rec *ptr;
171
172 for (i = 0; i < n; i++)
173 {
174 value = (unsigned int) ((10.0*n*rand())/(RAND_MAX+1.0));
175 ptr = new Rec(value);
176 cout << value << ", ";
177 heap.insert(ptr);
178 }
179
180 cout << "\n\n"
181 << "Extracting from heap" << endl;
182
183 for (i = 0; i < n; i++)
184 {
185 ptr = static_cast<Rec*>(heap.getMin());
186 value = ptr->i;
187 cout << value << " ";
188 delete ptr;
189 }
190
191 cout << "\n\n";
192 }
193
194}
#define MESSAGE(...)
Print an informational message (no-op when MESSAGES is not defined).
Definition ahDefs.H:259
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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:1383
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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