473,386 Members | 1,652 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,386 software developers and data experts.

Event Queues

10
im trying to write a program that store a binary tree of possible events in an array. i need to be able to sort the the Events in the array based on the previous event that caused it by the time which they will occur. After the specific time has passed the event will be removed and all other events will be bumped up, all the while new events will be added to the end and sorted by their time.

Please Help

heres wot i've got so far, i've got some test that will test whe the program is doin wot it should. i just dont know how to implement the add(event) and remove(event) methods
Expand|Select|Wrap|Line Numbers
  1. // Store a collection of events.  The operations are to add an event to the
  2. // collection, or to remove the earliest event from the collection.
  3. // Internally, a priority heap is used to provide an efficient implementation.
  4.  
  5. class EventQueue
  6.     // The fields of an EventQueue are...
  7.     Event[] eventqueue = new Event[100];
  8.     // The capacity() method returns the maximum size that the queue can grow
  9.     // to without resizing.
  10.     public int capicity()
  11.     {
  12.         return eventqueue.length;
  13.     }
  14.     // The size() method returns the number of events currently in the queue.
  15.     public int size(int count)
  16.     {
  17.     for (int i=0; i<eventqueue.length;i++)
  18.         {
  19.             if (eventqueue != null); 
  20.             count = count+1;
  21.         }
  22.         return count;
  23.     }
  24.     // The add(event) method adds an event to the queue.
  25.     public void add(Event event)
  26.     {
  27.  
  28.  
  29.     }
  30.  
  31.     // The take() method returns the earliest event in the queue.
  32.  
  33.     // Test the queue.
  34.  
  35.     public static void main(String[] args)
  36.     {
  37.         EventQueue q = new EventQueue();
  38.         Event e1 = new Event("One", 1);
  39.         Event e2 = new Event("Two", 2);
  40.         Event e3 = new Event("Three", 3);
  41.         q.add(e2);
  42.         q.add(e3);
  43.         q.add(e1);
  44.         test(q.size(), 3, "The queue should have size 3.");
  45.         test(q.capacity() >= 3, true, "The capacity should be >= size.");
  46.         test(q.take(), e1, "The earliest event was expected.");
  47.         test(q.take(), e2, "The second event was expected.");
  48.         test(q.take(), e3, "The third event was expected.");
  49.     }
  50.  
  51.     // Test that an actual value matches an expected value, either of which can
  52.     // be null.  Print the given message and throw an error if not.
  53.  
  54.     private static void test(Object actual, Object expected, String message)
  55.     {
  56.         if (actual == null && expected == null) return;
  57.         if (actual != null && actual.equals(expected)) return;
  58.         System.err.print(message);
  59.         System.err.print(": " + actual);
  60.         System.err.println(", not " + expected);
  61.         throw new Error("Testing failed.");
  62.     }
  63. }
Feb 15 '08 #1
5 2284
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Feb 15 '08 #2
BigDaddyLH
1,216 Expert 1GB
I'm confused. Isn't one of the features of a sorted tree data structure that it can efficiently maintain data in a sorted order yet allow you to insert and remove elements? Why the array, then? Are you sure you read your assignment correctly?
Feb 15 '08 #3
nt5515
10
hey sorry bout not using th [code] tags. im new to this and didnt know how. I had considered using a tree structure but the question does state the use of an array. I think its more an exercise in manipulating the array resizing and sorting etc.

could you tell me how to add a new event to the array? please
Feb 17 '08 #4
nt5515
10
heres the info i recieved about the question:

The purpose of the EventQueue class is to store all the events that "haven't happened yet" (according to the simulated current time). The events are taken out of the queue in order of increasing time. Here's a starting point for your class:

EventQueue.java

The EventQueue class stores events in an array which is bigger than is needed. For now, you can create the array with length 100 and assume there will never be more than 100 events. There is also a separate variable size which records how many events are currently stored in the array (from index 0 to index size-1).

The array represents a tree in which each node has up to two children. No pointers or anything like that are needed, just the array itself, because the tree has a very regular structure. The item at position n in the array has children which are at positions 2*n+1 and 2*n+2 in the tree, and a parent which is at position (n-1)/2. The item at position 0 is the top one in the tree and has no parent, and an item at position n has one child or no children if one or both of the child positions are greater than or equal to size. So, suppose the array contains 10 items in positions 0 to 9 like this:

a b c d e f g h i j

Then the tree which is implicitly represented looks like this. (It is a binary tree which is completely filled in, up to some point in the lowest row of nodes.)

a
/ \
b c
/ \ / \
d e f g
/ \ /
h i j

The trick is then to make sure that each event has an earlier time than either of its children. So, in the tree above, event a is earlier than b or c, but we don't care whether b is earlier than c or not. Then b is earlier than d or e and so on. This is effectively a compromise "partially sorted" array, making both of the necessary operations reasonably quick.

Then there are the two operations on the array to implement: adding another event, and extracting the earliest event.

To add another event, first put it in the next available slot in the array (slot size, after which size is incremented). The new event is then "bubbled up". That means while it has a parent, and it is earlier than its parent, swap it with its parent.

The queue allows events to have equal times, but doesn't guarantee any particular ordering between them (i.e. they will be extracted from the queue one after the other, nut not necessarily in the same order as they were added to the queue). We will discuss the efficiency of this priority queue implementation later in the course.
Feb 17 '08 #5
BigDaddyLH
1,216 Expert 1GB
Your instructor is describing a classic data structure called a "priority queue": http://en.wikipedia.org/wiki/Priority_queue .
Certain operations can be implemented efficiently in a PQ, including adding an element to the queue and remove the least element from the queue. The queue itself is not totally sorted, and shouldn't be used in applications that require the data to be totally sorted.
Feb 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marcin Kielar | last post by:
hello i'm searching for algorithm able to detect and resolve conflicts during recursive event propagation. I'd like to implement small message passing framework without using message queue -...
0
by: Aki Niimura | last post by:
Hello everyone. I'm trying to control a program from a Python program using IPC. Although using a socket is a common choice for such applications, I would like to use SysV message queues...
0
by: badtemper | last post by:
Is there any way to use perl or any other programming language to perform a "qchk -A" from a web page and see the listing? My problem is that I manage the network for a medical facility that is...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
1
by: saleem | last post by:
Dear friends, I am working on the problem which deals with the data management of requests and how should we match the responses for the requests to compare the correct Response. my problem...
6
by: Iain | last post by:
I've got a (VC++ 6.0) com object which does something asynchronously (writes a DVD image, actually). It rasies COM events to indicate progress, including completion. I've got this running in a...
7
by: copx | last post by:
How do you implement an event queue in C? The problem I had is that events needed pointers to the objects they affect and I do not know any way to check if pointers are actually valid in C. The...
4
by: Anis | last post by:
hi, i want to know what is event that windows log in "Event Log". i have read that there r some services that log the event, but at the base level what exactly event is ? and can we make our...
2
by: Chuck B | last post by:
I couldn't find the info I was looking for anwhere else so I'll try here. I have a custom control that calls a delegate method when a SelectHandler event is fired. The SelectHandler method adds a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.