Priority Queue not popping out the topmost element. Weird!!!

Hi,

For my Discrete event simulation assignment, I was advised to use priority queues. I was able to access the perfect top element but when I pop out I get the bottom element! I don't understand where am I going wrong is my comparator function that goes wrong?

Comparator function

1
2
3
4
5
6
7
8
9
class CompareEventQueue {
public:
    bool operator()(Event& e1, Event& e2) {	
		if (e1.time == e2.time)  {
		   return (e1.processId > e2.processId);
		}
		return (e1.time > e2.time);
    }
};


Priority queue definition is

priority_queue <Event, vector<Event>, CompareEventQueue> eventQueue;

The object Event is of type
1
2
3
4
5
6
class Event {
public:
	eventType eventStart;
	double time; //time units since the start of simulation
	int processId;
};


So when I insert

EventObject1 ---> (CPUBurstCompletionEvent, 6.59999999, 0)
EventObject2 ---> (arrival, 6.59999999, 1)

So when I access the top element it gives me EventObject1;
Event temp = eventQueue.top();

but when I pop an element out, it pops out EventObject2; weird!!!!! :(.. Some1 plz help me! I am a noob..

This is the link to the image of my eventQueue after I add EventObject1 and EventObject2 http://imageshack.us/photo/my-images/98/screenshot20111009at329.png/
Last edited on
I'm fairly sure that CompareEventQueue::operator()() should behave as an operator<() overload.
Is my declaration wrong is what you want to say? Coz I don't udnerstand what you mean
The declaration is fine. It's the semantics that's wrong.
Can you please edit it for me? I am very new to c++...
Just invert the relational operators. It's supposed to be <, not >.
Ya I tried that, it stil does't work! :(....

If I do the '<' then I get the top object as EventObject2 and not EventObject1 which is what I don't want...
Last edited on
Have you still got that == comparison between two doubles ?

This will be a source of unreliability, there is a good description of the problem of comparing floating point numbers here

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

If you require a wide ranging number combined with exact comparison use a long int or similar


Topic archived. No new replies allowed.