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 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# include <ctime>
28# include <cstdlib>
29# include <cassert>
30
31# include <iostream>
32# include <string>
33# include <aleph.H>
34# include <tpl_binHeap.H>
35# include <tpl_binNodeUtils.H>
36
37using namespace std;
38using namespace Aleph;
39
40struct Foo
41{
42 int number;
43
44 Foo(int i) : number(i) { /* empty */ }
45
46 bool operator < (const Foo& r)
47 {
48 return number < r.number;
49 }
50
51 bool operator <= (const Foo& r)
52 {
53 return number <= r.number;
54 }
55};
56
57
58int main(int argc, char *argv[])
59{
60 int n = 1000;
61 unsigned int t = std::time(0);
62
63 try
64 {
65 if (argc > 1)
66 n = std::stoi(argv[1]);
67
68 if (argc > 2)
69 t = std::stoi(argv[2]);
70 }
71 catch (...)
72 {
73 // ignore
74 }
75
76 if (n <= 0)
77 {
78 cout << "n must be positive" << endl;
79 return 1;
80 }
81
82 srand(t);
83
84 cout << argv[0] << " " << n << " " << t << endl;
85
86 BinHeap<Foo*> heap;
88 Foo *ptr;
89 int i;
90
91 for (i = n - 1; i >= 0; i--)
92 {
93 ptr = new Foo (i);
94 node = new BinHeap<Foo*>::Node;
95 node->get_key() = ptr;
96 heap.insert(node);
97 }
98
99 // assert(heap.verify_heap());
100
101 for (i = 0; i < n; i++)
102 {
103 node = heap.getMin();
104 delete node->get_key();
105 delete node;
106 }
107
108 // assert(heap.verify_heap());
109
110 int value;
111
112 for (i = n - 1; i >= 0; i--)
113 {
114 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
115 ptr = new Foo (value);
116 node = new BinHeap<Foo*>::Node ;
117 node->get_key() = ptr;
118 heap.insert(node);
119 }
120
121 // assert(heap.verify_heap());
122
123 for (i = 0; i < n; i++)
124 {
125 node = heap.getMin();
126 delete node->get_key();
127 delete node;
128 }
129
130 // assert(heap.verify_heap());
131
132 for (i = n - 1; i >= 0; i--)
133 {
134 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
135 ptr = new Foo (value);
136 node = new BinHeap<Foo*>::Node;
137 node->get_key() = ptr;
138 heap.insert(node);
139 }
140
141 // assert(heap.verify_heap());
142
143 for (i = 0; i < n/2; i++)
144 {
145 node = heap.getMin();
146 delete node->get_key();
147 delete node;
148 }
149 // assert(heap.verify_heap());
150
151 for (i = n - 1; i >= 0; i--)
152 {
153 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
154 ptr = new Foo (value);
155 node = new BinHeap<Foo*>::Node;
156 node->get_key() = ptr;
157 heap.insert(node);
158 }
159 // assert(heap.verify_heap());
160
161 for (i = 0; i < n + n/2; i++)
162 {
163 node = heap.getMin();
164 delete node->get_key();
165 delete node;
166 }
167 // assert(heap.verify_heap());
168
169}
Core header for the Aleph-w library.
Key & get_key() noexcept
Node * getMin()
Elimina del heap el nodo de menor prioridad.
Node * insert(Node *p) noexcept
Inserta un nodo en un heap.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
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.