473,408 Members | 2,030 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,408 software developers and data experts.

STL container advice

I am implementing a event messaging system. Basically I do:

---update cycle---

processing/check for new events
allocate a new event
put it in a std::queue

Dispatch events
make an event equal to std::queue.front()
pop an event from the std::queue
get its type
send it off to registered handlers

Now problem is that the handler may or may not process it. The handler
will return me a 0 if it was taken care of and a 1 if not. If the
event was not handled I want to leave it to be dispatched again next
update cycle. A std::queue only has a front() function, but no
iterator. So I cannot get the event, decide if I still want it or not,
then go to the next event without getting rid of the first. I cannot
iterate through and choose to erase handled events.

First I considered making another queue and putting unhandled events
into it then copying that queue to the original, but I think that
would be darn expensive, no?

I also considered using a vector instead of a queue, so I can iterate
through and erase as I need to, but isn't a vector more expensive,
especially since I am looking at the front and erasing in random
places?

Any other ideas for a more efficient event queue?

Mar 13 '07 #1
4 1847
brekehan wrote:
I am implementing a event messaging system. Basically I do:

---update cycle---

processing/check for new events
allocate a new event
put it in a std::queue

Dispatch events
make an event equal to std::queue.front()
pop an event from the std::queue
get its type
send it off to registered handlers
Here you indicate that there are multiple handlers. Or have I
misunderstood and there is only one handler per event? That's not
clear.
Now problem is that the handler may or may not process it. The handler
will return me a 0 if it was taken care of and a 1 if not.
So, each handler can return 0 or 1, right? So, do you keep the event
if at least one handler returned 1 or throw it out if at least one
hanlder returned 0? That's not clear as well.
If the
event was not handled I want to leave it to be dispatched again next
update cycle.
Dispatched to whom? What happens to those handlers that have already
indicated that they "took care" of the event?

Now, if we presume that there is a single handler for each event, you
could simply delay popping the event from the queue until the handler
returns 0 ("taken care of"). Of course, it would stall processing of
all events following the one that needs to be delayed.
A std::queue only has a front() function, but no
iterator. So I cannot get the event, decide if I still want it or not,
then go to the next event without getting rid of the first. I cannot
iterate through and choose to erase handled events.
Why do you need a queue then? Just use a heap or a deque.
First I considered making another queue and putting unhandled events
into it then copying that queue to the original, but I think that
would be darn expensive, no?
It's unclear what that would serve. If you want to iterate over the
events in your container and pull out those you want, then how is that
a queue? The whole point of a queue is that the order of arrival is
of the utmost importance.
>
I also considered using a vector instead of a queue, so I can iterate
through and erase as I need to, but isn't a vector more expensive,
especially since I am looking at the front and erasing in random
places?
Yes, it is more expensive. Besides, std::queue is not a contaner.
It's a container _adapter_.
Any other ideas for a more efficient event queue?
Here you go again. Yours is not a queue, apparently. Otherwise you
wouldn't be asking those questions. Determine what you want and
only then try to pick the right container. Perhaps 'priority_queue'
is what you can use? Perhaps you need to write your own. Use
std::list, for example. Iterating is straightforward, removal is
swift, so are insertions. A bit more expensive in terms of memory,
but should still be fine.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #2
brekehan wrote:
I am implementing a event messaging system. Basically I do:

---update cycle---

processing/check for new events
allocate a new event
put it in a std::queue

Dispatch events
make an event equal to std::queue.front()
pop an event from the std::queue
get its type
send it off to registered handlers

Now problem is that the handler may or may not process it. The handler
will return me a 0 if it was taken care of and a 1 if not. If the
event was not handled I want to leave it to be dispatched again next
update cycle. A std::queue only has a front() function, but no
iterator. So I cannot get the event, decide if I still want it or not,
then go to the next event without getting rid of the first. I cannot
iterate through and choose to erase handled events.

First I considered making another queue and putting unhandled events
into it then copying that queue to the original, but I think that
would be darn expensive, no?

I also considered using a vector instead of a queue, so I can iterate
through and erase as I need to, but isn't a vector more expensive,
especially since I am looking at the front and erasing in random
places?

Any other ideas for a more efficient event queue?
Your description is somewhat vague. Why can't you delay popping the
queue until after you've determined that the event was successfully
handled? Are you handling multiple events concurrently? If you really
need to put things back into the queue, have you looked at std::deque?

-Mark
Mar 14 '07 #3
On Mar 14, 9:10 am, Mark P <use...@fall2005REMOVE.fastmailCAPS.fm>
wrote:
brekehan wrote:
I am implementing a event messaging system. Basically I do:
---update cycle---
processing/check for new events
allocate a new event
put it in a std::queue
Dispatch events
make an event equal to std::queue.front()
pop an event from the std::queue
get its type
send it off to registered handlers
Now problem is that the handler may or may not process it. The handler
will return me a 0 if it was taken care of and a 1 if not. If the
event was not handled I want to leave it to be dispatched again next
update cycle. A std::queue only has a front() function, but no
iterator. So I cannot get the event, decide if I still want it or not,
then go to the next event without getting rid of the first. I cannot
iterate through and choose to erase handled events.
First I considered making another queue and putting unhandled events
into it then copying that queue to the original, but I think that
would be darn expensive, no?
I also considered using a vector instead of a queue, so I can iterate
through and erase as I need to, but isn't a vector more expensive,
especially since I am looking at the front and erasing in random
places?
Any other ideas for a more efficient event queue?

Your description is somewhat vague. Why can't you delay popping the
queue until after you've determined that the event was successfully
handled? Are you handling multiple events concurrently? If you really
need to put things back into the queue, have you looked at std::deque?

-Mark
To know more about dequeue
http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp

Mar 14 '07 #4
On Mar 14, 5:28 am, "brekehan" <c...@austin.rr.comwrote:
I am implementing a event messaging system. Basically I do:

---update cycle---

processing/check for new events
allocate a new event
put it in a std::queue

Dispatch events
make an event equal to std::queue.front()
pop an event from the std::queue
get its type
send it off to registered handlers

Now problem is that the handler may or may not process it. The handler
will return me a 0 if it was taken care of and a 1 if not. If the
event was not handled I want to leave it to be dispatched again next
update cycle. A std::queue only has a front() function, but no
iterator. So I cannot get the event, decide if I still want it or not,
then go to the next event without getting rid of the first. I cannot
iterate through and choose to erase handled events.

First I considered making another queue and putting unhandled events
into it then copying that queue to the original, but I think that
would be darn expensive, no?

I also considered using a vector instead of a queue, so I can iterate
through and erase as I need to, but isn't a vector more expensive,
especially since I am looking at the front and erasing in random
places?

Any other ideas for a more efficient event queue?
Well I believe you would be needing operation where deletion could
occur in the middle of your queue; to such I believe a list is more
appropriate; lists supports forward and backward iteration; popping
form the front and the end of the list as well as deleting from the
middle of the list, but it's still confusing how your application is
performing.

Mar 14 '07 #5

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

Similar topics

1
by: Istvan Buki | last post by:
Dear C++ gurus, I'm having some problems inserting some template classes into containers. I'm looking for ideas, suggestions,... on how to achieve this but first let me explain my problem....
8
by: Markus Dehmann | last post by:
I defined a base class in order to put heterogeneous values into a standard container: All values that I store in the container are derived from my base class. Now when I iterate over the...
5
by: deancoo | last post by:
Being fairly new to STL, I'm looking for a little advice on which container to use. Let me set up the scenario for you to consider. Suppose you're trying to model a hotel. The hotel has a fixed...
1
by: anony | last post by:
Hi, I would like to find out if it's possible to check the value of a Container.DataItem inside the Datagrid's <ItemTemplate> tag .... based on it's value, I would to either output it or output...
4
by: hsomob1999 | last post by:
I have a class, lets call it Schmuck. It has 2 public properties Name (string) and active (boolean). When I bind to a Repeater, like so: <%#container.dataitem("Name")%> i get the following error:...
5
by: edward.birch | last post by:
Can anyone see anything wrong with the following code? (CONTAINER can be list, vector, set, ...) template <class T> void Destroy(T * p) { delete p; } void...
7
by: vsgdp | last post by:
I have a container of pointers. It is possible for two pointers to point to the same element. I want to remove duplicates. I am open to which container is best for this. I thought of using...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
1
by: infodbms | last post by:
Hello, I have a big DMS tablespace with large container span across 3 filesystem on DB2 v8.2/AIX5.2. Each container is of different size on each of the filesystem. Looking for some...
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:
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.