Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinHeapPtr.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# include <ctime>
33# include <cstdlib>
34# include <cassert>
35
36# include <iostream>
37# include <string>
38# include <aleph.H>
39# include <tpl_binHeap.H>
40# include <tpl_binNodeUtils.H>
41
42using namespace std;
43using namespace Aleph;
44
45struct Foo
46{
47 int number;
48
49 Foo(int i) : number(i) { /* empty */ }
50
51 bool operator < (const Foo& r)
52 {
53 return number < r.number;
54 }
55
56 bool operator <= (const Foo& r)
57 {
58 return number <= r.number;
59 }
60};
61
62
63int main(int argc, char *argv[])
64{
65 int n = 1000;
66 unsigned int t = std::time(0);
67
68 try
69 {
70 if (argc > 1)
71 n = std::stoi(argv[1]);
72
73 if (argc > 2)
74 t = std::stoi(argv[2]);
75 }
76 catch (...)
77 {
78 // ignore
79 }
80
81 if (n <= 0)
82 {
83 cout << "n must be positive" << endl;
84 return 1;
85 }
86
87 srand(t);
88
89 cout << argv[0] << " " << n << " " << t << endl;
90
91 BinHeap<Foo*> heap;
93 Foo *ptr;
94 int i;
95
96 for (i = n - 1; i >= 0; i--)
97 {
98 ptr = new Foo (i);
99 node = new BinHeap<Foo*>::Node;
100 node->get_key() = ptr;
101 heap.insert(node);
102 }
103
104 // assert(heap.verify_heap());
105
106 for (i = 0; i < n; i++)
107 {
108 node = heap.getMin();
109 delete node->get_key();
110 delete node;
111 }
112
113 // assert(heap.verify_heap());
114
115 int value;
116
117 for (i = n - 1; i >= 0; i--)
118 {
119 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
120 ptr = new Foo (value);
121 node = new BinHeap<Foo*>::Node ;
122 node->get_key() = ptr;
123 heap.insert(node);
124 }
125
126 // assert(heap.verify_heap());
127
128 for (i = 0; i < n; i++)
129 {
130 node = heap.getMin();
131 delete node->get_key();
132 delete node;
133 }
134
135 // assert(heap.verify_heap());
136
137 for (i = n - 1; i >= 0; i--)
138 {
139 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
140 ptr = new Foo (value);
141 node = new BinHeap<Foo*>::Node;
142 node->get_key() = ptr;
143 heap.insert(node);
144 }
145
146 // assert(heap.verify_heap());
147
148 for (i = 0; i < n/2; i++)
149 {
150 node = heap.getMin();
151 delete node->get_key();
152 delete node;
153 }
154 // assert(heap.verify_heap());
155
156 for (i = n - 1; i >= 0; i--)
157 {
158 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
159 ptr = new Foo (value);
160 node = new BinHeap<Foo*>::Node;
161 node->get_key() = ptr;
162 heap.insert(node);
163 }
164 // assert(heap.verify_heap());
165
166 for (i = 0; i < n + n/2; i++)
167 {
168 node = heap.getMin();
169 delete node->get_key();
170 delete node;
171 }
172 // assert(heap.verify_heap());
173
174}
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Key & get_key() noexcept
Node * getMin()
Removes the node with the lowest priority from the heap.
Node * insert(Node *p) noexcept
Inserts a node into a heap.
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
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
STL namespace.
Node heap without virtual destructor.
int i
int number
bool operator<(const Foo &r)
bool operator<=(const Foo &r)
Foo(int i)
gsl_rng * r
Binary heap implementation using tree structure.
Utility functions for binary tree operations.