Commit a34e9169 authored by Guillaume Lazzara's avatar Guillaume Lazzara
Browse files

Fix a double free in fibonacci heap and add support for priority.

	* mln/util/fibonacci_heap.hh: Fix the destructor and add an attribute
	"priority" to the heap nodes. The fibonacci heap now behaves like a
	priority queue.

	* tests/util/fibonacci_heap.cc: add more tests.

git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@3170 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent b8e76425
2009-01-20 Guillaume Lazzara <z@lrde.epita.fr>
Fix a double free in fibonacci heap and add support for priority.
* mln/util/fibonacci_heap.hh: Fix the destructor and add an attribute
"priority" to the heap nodes. The fibonacci heap now behaves like a
priority queue.
* tests/util/fibonacci_heap.cc: add more tests.
2009-01-19 Guillaume Lazzara <z@lrde.epita.fr>
 
Rename histo::data as histo::array.
This diff is collapsed.
......@@ -33,6 +33,7 @@
#include <mln/util/fibonacci_heap.hh>
#include <mln/core/alias/point2d.hh>
using mln::point2d;
point2d p[] = { point2d(4,5), point2d(3,4), point2d(3,2),
......@@ -42,10 +43,10 @@ point2d p[] = { point2d(4,5), point2d(3,4), point2d(3,2),
point2d res_1[] = { point2d(1,6), point2d(2,3), point2d(2,4) };
point2d res_2[] = { point2d(1,4), point2d(3,2), point2d(3,4),
point2d(3,5), point2d(4,5), point2d(5,4),
point2d(7,2), point2d(7,3), point2d(9,6),
point2d(53,4) };
point2d res_2[] = { point2d(53,4), point2d(5,4), point2d(1,4),
point2d(3,2), point2d(3,4), point2d(3,5),
point2d(4,5), point2d(7,2), point2d(7,3),
point2d(9,6) };
int main()
......@@ -53,14 +54,14 @@ int main()
using namespace mln;
/// Init heap
util::fibonacci_heap<point2d> heap;
util::fibonacci_heap<int,point2d> heap;
for (unsigned i = 0; i < 5; ++i)
heap.push(p[i]);
heap.push(3, p[i]);
/// Init heap2
util::fibonacci_heap<point2d> heap2;
util::fibonacci_heap<int,point2d> heap2;
for (unsigned i = 5; i < 10; ++i)
heap2.push(p[i]);
heap2.push(3, p[i]);
/*!
......@@ -79,8 +80,9 @@ int main()
/// Heap2 is now empty
mln_assertion(heap2.is_empty());
mln_assertion(heap2.nelements() == 0);
/// Check if the front() is correct in heap
mln_assertion(heap.front() == res_1[0]);
// mln_assertion(heap.front() == res_1[0]);
/*!
......@@ -111,9 +113,9 @@ int main()
/// Re-insert data after deletion...
heap2.push(point2d(53,4));
heap2.push(point2d(1,4));
heap2.push(point2d(5,4));
heap2.push(1, point2d(53,4));
heap2.push(3, point2d(1,4));
heap2.push(2, point2d(5,4));
/// ... and try to extract and delete data again.
unsigned i = 0;
......@@ -126,4 +128,25 @@ int main()
/// The heap must be empty.
mln_assertion(heap2.is_empty());
mln_assertion(heap2.nelements() == 0);
/// Swap two variables thanks to a temporary.
heap.push(3, point2d(2,4));
heap2.push(3, point2d(53,4));
util::fibonacci_heap<int,point2d> tmp = heap;
heap = heap2;
heap2 = tmp;
mln_assertion(heap2.nelements() == 1);
mln_assertion(heap.nelements() == 1);
mln_assertion(tmp.nelements() == 0);
/// Testing copy constructor.
util::fibonacci_heap<int,point2d> tmp2(heap);
mln_assertion(tmp2.nelements() == 1);
mln_assertion(heap.nelements() == 0);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment