Thread View: comp.lang.c++.leda
1 messages
1 total messages
Started by lemahieu
Thu, 03 Aug 1995 22:26
Bug in p_queue?
Author: lemahieu
Date: Thu, 03 Aug 1995 22:26
Date: Thu, 03 Aug 1995 22:26
69 lines
1940 bytes
1940 bytes
It seems I've found a bug in the LEDA priority queue (p_queue).
The problem is with the copy constructor when multiple elements with
the same priority exist. In this case, the replicated version has
elements with that same priority in the reverse order of the original.
The LEDA manual makes no mention that priority queues should only have
one instance of a priority per queue (unlike for sorted sequences), so
I assume this is legal.
Here's some sample code.
// Paul LeMahieu
//
// pqueuebug.cc
#include <LEDA/p_queue.h>
#include <iostream.h>
// print out queue by popping min elements from a copy
void print_val(p_queue<int,int> pq)
{
pq_item it;
while(!pq.empty()) {
it = pq.find_min();
cout << pq.prio(it) << ", " << pq.inf(it) << endl;
pq.del_item(it);
}
}
// _identical_ to print_val but pass by reference
void print_ref(p_queue<int,int>& pq)
{
pq_item it;
while(!pq.empty()) {
it = pq.find_min();
cout << pq.prio(it) << ", " << pq.inf(it) << endl;
pq.del_item(it);
}
}
main(void)
{
p_queue<int,int> pq;
pq.insert(1,3);
pq.insert(1,2);
pq.insert(1,1);
print_val(pq);
cout << endl;
print_ref(pq);
}
This gives the following results:
1, 1
1, 2
1, 3
1, 3
1, 2
1, 1
Note that the copied version gets the elements popped off in
reverse order from the original.
-- Paul LeMahieu
lemahieu@paradise.caltech.edu
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads