EDIT: New question. Modular comparison function? Quick C++ priority queue question

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
EDIT: New Question!


Here's the new question:


Is there any way of making the priority function of a PQ modular? That is, can I specify at run-time which comparison function I'd like to use? The first response hinted at that, but it'd be more complicated than simply reversing the list. All the comparisons are simple (they're just integral proprieties).

For example: I might want to sort the node list by shortest latency. But then I might want to sort it by max distance or something. Is there any way of maintaining the same priority queue object but having a switch to pick one?

The main reason is I have a function that returns this priority queue. And obviously queues with different comparisons are completely different object types. So if I have a function return priority queue type A, when I change the comparison function, the return type is now invalid.

Is there any way of getting around this?

Basically, I have a bunch of nodes in a list, and I want to write a function that'll return them sorted in whatever way I'd like, preferably without having to do the sorting myself.

Thanks!

Derp. Wouldn't the following work?
Code:
class myCompare {
     int flag;
public:
      myCompare (int flagParm = 0) {flag = flagParm;}
      
      bool operator() (node node1, node node2) {
           switch (flag) {
               case 1:
                  if (node1->valueA > node2->valueA)
                     return TRUE;
                  else
                      return FALSE;
                  break;
               case 2:
                  if (node1->valueX > node2->valueY)
                     return TRUE;
                  else
                      return FALSE;
                  break;

  
           } // switch
      }
};



****

Here's the old question


****



So I want to use a priority queue to order up some "node" class objects that I have. These nodes have a couple of properties -- all integral -- that I'd like to be able to order by. Do I have do define different "compare" functions for each of these? i.e.

bool operator() (const node & leftNode, const node & rightNode) const {
if (leftNode->getMyValue() <= rightnode->getMyValue() )
return TRUE
else
return FALSE;
}

For each of the differing myValue properties I might want to use? Or is there an easier way where I can define the integer value in the [priority queue] object definition?

(And if I have to define the operator as above, where do I do that? In the node class?)

Thanks!
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
Looks like you ought to have a different class for every sorting method, as it's specified at the constructor for the priority queue.

I guess as long as you're consistent throughout a queue's lifetime, you could do complex if statements, plus multiplication by -1 or 1 to reverse order. :rolleyes: You could also build your own heap; they're not that hard to make.

Have you considered something like SQLite instead?
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
So I want to use a priority queue to order up some "node" class objects that I have. These nodes have a couple of properties -- all integral -- that I'd like to be able to order by. Do I have do define different "compare" functions for each of these? i.e.

bool operator() (const node & leftNode, const node & rightNode) const {
if (leftNode->getMyValue() <= rightnode->getMyValue() )
return TRUE
else
return FALSE;
}

For each of the differing myValue properties I might want to use? Or is there an easier way where I can define the integer value in the [priority queue] object definition?

(And if I have to define the operator as above, where do I do that? In the node class?)

Thanks!

The only reason why I would see you use getMyValue() is if you need to calculate or do additional processing on the node to return a value.

Otherwise, make the comparison property of the node visible to the public and do:
Code:
if(node1->property <= node2->property)
return true;
else
return false;


Better yet, if you maintain a list of nodes that is always sorted, then you would just pop off the first node on the list.
 

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
The only reason why I would see you use getMyValue() is if you need to calculate or do additional processing on the node to return a value.

Otherwise, make the comparison property of the node visible to the public and do:
Code:
if(node1->property <= node2->property)
return true;
else
return false;


Better yet, if you maintain a list of nodes that is always sorted, then you would just pop off the first node on the list.

A couple of things...:

1). Information hiding (or not) in this case isn't really the question. How the members are accessed is the least of my worries.

2). The whole point of the queue is that it does the sorting for me.

I've figured out how to do what I want, but I've updated the OP with a more pressing question:

(included here)

Is there any way of making the priority function of a PQ modular? That is, can I specify at run-time which comparison function I'd like to use? The first response hinted at that, but it'd be more complicated than simply reversing the list. All the comparisons are simple (they're just integral proprieties).

For example: I might want to sort the node list by shortest latency. But then I might want to sort it by max distance or something. Is there any way of maintaining the same priority queue object but having a switch to pick one?

The main reason is I have a function that returns this priority queue. And obviously queues with different comparisons are completely different object types. So if I have a function return priority queue type A, when I change the comparison function, the return type is now invalid.

Is there any way of getting around this?

Basically, I have a bunch of nodes in a list, and I want to write a function that'll return them sorted in whatever way I'd like, preferably without having to do the sorting myself.

Thanks!