473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

priority_queues or not?

Hello

I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the "interface1 *",
providing it with a comparison function that will sort according to
priority, so that later I can iterate in a sorted manner.

I would have used priority_queue, however I want to be able to register and
unregister arbitrary interfaces.
std::set allows one to remove and insert anywhere.

Please advise.

--
Elias
Mar 28 '06 #1
6 1402
Hello,

lallous wrote:
I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the
"interface1 *", providing it with a comparison function that will sort
according to priority, so that later I can iterate in a sorted manner.

I would have used priority_queue, however I want to be able to
register and unregister arbitrary interfaces.
std::set allows one to remove and insert anywhere.

Please advise.


priority_queue is meant for a situation, where a set of elements is
moved through the queue, i.e. they are queued and some time later they
are unqueued. priority_queue maintains a certain order only when
removing the elements one by one. I think you intend to use your set of
elements repeatedly, this means you would have to recreate the priority
queue each time you need it. So priority queue seems not to be the
right tool.

My suggestion is map, i.e. removing the priority from interface1, and
using a map from priority to interface1 pointers as sorted structure.

class interface1
{
public:
virtual ~interface1() { };
virtual void on_event() { }
};

std::map<int,interface1*> on_event_listeners;

map supports inserting and erasing everywhere.

Assigning priorities to certain implementations of interface1 has been
left open in the code above, but in the end the OP has left it open as
well.

You have several tasks, the on_event implementations, assigning
priorities to them, and maintaining lists of implementations, which
should be separated from each other.

Bernd Strieder

Mar 28 '06 #2
In article <48************@individual.net>,
"lallous" <la*****@lgwm.org> wrote:
Hello

I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the "interface1 *",
providing it with a comparison function that will sort according to
priority, so that later I can iterate in a sorted manner.
Are you saying you don't want to remove the items as you are processing
them? If that's the case, then I agree with you.

I would have used priority_queue, however I want to be able to register and
unregister arbitrary interfaces.
What? I don't understand what you mean here...
std::set allows one to remove and insert anywhere.
No it doesn't. It requires items to be sorted. If you provide a pred
that sorts by _priority, then they must be in _priority order. Also, set
doesn't allow two items with the same _priority to exist. Also, set
doesn't allow modification of the contained elements. (However, since
the elements contained are pointers, that's probably not an issue.)

Please advise.


I have to agree with Bernd, although I think you would be better served
by a multimap<int, interface*> so that several things can have the same
priority.

Now, one might ask, "why rip the priority out of the class?" I suspect
that 'priority' is not an intrinsic property of objects of the class,
but instead a relation between two objects of the same class in the
particular environment they are currently being used in. When dealing
with a single object, the priority becomes nonsensical.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 28 '06 #3
Hello Bernd,

"Bernd Strieder" <st******@informatik.uni-kl.de> wrote in message
news:e0**********@news.uni-kl.de...
Hello,

lallous wrote:
I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the
"interface1 *", providing it with a comparison function that will sort
according to priority, so that later I can iterate in a sorted manner.

I would have used priority_queue, however I want to be able to
register and unregister arbitrary interfaces.
std::set allows one to remove and insert anywhere.

Please advise.


priority_queue is meant for a situation, where a set of elements is
moved through the queue, i.e. they are queued and some time later they
are unqueued. priority_queue maintains a certain order only when
removing the elements one by one. I think you intend to use your set of
elements repeatedly, this means you would have to recreate the priority
queue each time you need it. So priority queue seems not to be the
right tool.

My suggestion is map, i.e. removing the priority from interface1, and
using a map from priority to interface1 pointers as sorted structure.

class interface1
{
public:
virtual ~interface1() { };
virtual void on_event() { }
};

std::map<int,interface1*> on_event_listeners;

map supports inserting and erasing everywhere.

Assigning priorities to certain implementations of interface1 has been
left open in the code above, but in the end the OP has left it open as
well.

You have several tasks, the on_event implementations, assigning
priorities to them, and maintaining lists of implementations, which
should be separated from each other.


How do you suggest I managed priorites with an std::map ?
It is true that map can add/remove items on demand, however, the keys are
not sorted, unlike an std::set.

So the best thing is to use an std::set<interface1_ptr> with custom compare
functor?

--
Elias
Apr 3 '06 #4
lallous wrote:
....
It is true that map can add/remove items on demand, however, the keys
are not sorted, unlike an std::set.


Are too! :-)

Jeff
Apr 3 '06 #5
Hello

I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the "interface1
*",
providing it with a comparison function that will sort according to
priority, so that later I can iterate in a sorted manner.
Are you saying you don't want to remove the items as you are processing
them? If that's the case, then I agree with you.


Yes, processing is done when a given event is triggered.
Removing is done when a certain client decides to unregister from the list
(by providing the "interface1 *").
I would have used priority_queue, however I want to be able to register
and
unregister arbitrary interfaces.
What? I don't understand what you mean here...


Doesn't priority_queue require you to extract only the top item?
But I may want to remove a given handler by its pointer value, and don't
think priority_queue would allow me to remove any item but the top one.
std::set allows one to remove and insert anywhere.
No it doesn't. It requires items to be sorted. If you provide a pred
that sorts by _priority, then they must be in _priority order. Also, set
doesn't allow two items with the same _priority to exist. Also, set
doesn't allow modification of the contained elements. (However, since
the elements contained are pointers, that's probably not an issue.)


You mean that std::set will allow you to remove any item from anywhere, and
it will re-sort again?

I have to agree with Bernd, although I think you would be better served
by a multimap<int, interface*> so that several things can have the same
priority.

I didn't understand how I can implement my requirement using a map or
multimap.

Can you elaborate more on this idea please?

Currently, I am relying on std::set<interface1 *> which are sorted based on
their _priority member.
So when I process the 'set', the elements with highest priority will be
processed first.

But do maps maintain a certain sorting that is reliable and when iterated it
will yield items in my desired order? (highest priority elements first and
least at last)
Now, one might ask, "why rip the priority out of the class?" I suspect
that 'priority' is not an intrinsic property of objects of the class,
but instead a relation between two objects of the same class in the
particular environment they are currently being used in. When dealing
with a single object, the priority becomes nonsensical.


You are correct, priority is used only to define a certain order
relationship and is nonesensical when an object is used alone.
But I wanted a self-contained structure that will allow one to insert items
and sort them by order.

--
Elias
Apr 3 '06 #6

"lallous" <la*****@lgwm.org> wrote in message
news:49************@individual.net...
Hello

I have a design / C++ question, hope you can help me.

class interface1
{
protected:
int _priority;
public:
virtual ~interface1() { };
interface1(int priority) { _priority = priority; }
virtual void on_event() { }
};

Now users can subclass this interface and override the on_event().

Now I have an std::list<interface *>

However, the std::list does not sort the elements.
Thus I want to use an std::set<> instead that will sort the "interface1
*",
providing it with a comparison function that will sort according to
priority, so that later I can iterate in a sorted manner.
Are you saying you don't want to remove the items as you are processing
them? If that's the case, then I agree with you.


Yes, processing is done when a given event is triggered.
Removing is done when a certain client decides to unregister from the list
(by providing the "interface1 *").
I would have used priority_queue, however I want to be able to register
and
unregister arbitrary interfaces.


What? I don't understand what you mean here...


Doesn't priority_queue require you to extract only the top item?
But I may want to remove a given handler by its pointer value, and don't
think priority_queue would allow me to remove any item but the top one.
std::set allows one to remove and insert anywhere.


No it doesn't. It requires items to be sorted. If you provide a pred
that sorts by _priority, then they must be in _priority order. Also, set
doesn't allow two items with the same _priority to exist. Also, set
doesn't allow modification of the contained elements. (However, since
the elements contained are pointers, that's probably not an issue.)


You mean that std::set will allow you to remove any item from anywhere,

and it will re-sort again?


You probably should read some STL document first before you make any design
decision. It seems you are not very familiar with the STL classes.
Apr 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim Strathmeyer | last post by:
So, I've been looking for a wrapper class for STL priority queues, and haven't been able to find one. (Websearching for info about the STL sure shows how much it's changed over the past few years.)...
3
by: seung | last post by:
How do I convert, for example, from vector to queue? Say, vector<int> a; queue<int> b = queue(a); This gives type error because there's no queue constructor that takes vector. Is there any...
6
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without...
10
by: mvjohn100 | last post by:
Being in the development of a project. our group is confused about what shall be the better one c++ string or simple c string allocation. Thanks in advance. john
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.