Connecting Tech Pros Worldwide Forums | Help | Site Map

Data Structure help required.

Member
 
Join Date: Jul 2008
Posts: 52
#1: Oct 21 '09
Hi there,

I have a situtation, I am on my way to build an alarm application which will run in the background and will intimate the user for an event which he had set before. Exactly what a scheduler or alarm clock does.
I will explain the design in short here. I maintain the tasks set by the user in a list of "Task" class. With every tick of the timer I initiate a background thread that will poll the task list to check if there are any tasks for the present time stamp. If yes it will raise an event which does the work of intimating the user by playing a music and displaying a form.
The, back ground thread is needed so that the main thread does not have to wait for the scanning of the list.
So far so good. The application is working well. But I know at heart that the List is not a good thing to use in this case as the application will terribly fail if the list is too long. may be the user will be intimated 2-3 mins after the time he may had set his alarm to.
I request for suggestions about which data structure to use here, if not list. I know there are some very fast list like structures (Like something used by the word suggesting thread of the MS Word, as it can pick up words from huge ocean of words.). However I dont know what they are and how to use them. If you suggest a design change, I whole heartedly shall agree. I know it is not a very good design.
Sincerely looking for your suggestions.

Regards,
Sid

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,784
#2: Oct 21 '09

re: Data Structure help required.


A couple ideas that might help:
Sort your tasks by time. Now you don't have to search all of them. As soon as you check a task that in the future you know that every task beyond it is also in the future, so stop searching.
Keep a marker variable set to the index of the last used task. Now you don't bother checkng tasks that have already been run today.
At this point your search is only from 'marker' to 'future' instead of the entire list.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 235
#3: Oct 21 '09

re: Data Structure help required.


If you want something truly efficient, you'll likely have to write your own storage and search. Try a google on search algorithms, there are a lot, and you'll need to pick the one that's best for you.

You also have the option of built-in structures to help you. You're using a List... I'm not sure what the code behind your search and retrieve is, but it's likely linear. My suggestion would be to try a HashTable as they have very fast retrieval mechanisms.

However, you'll still have to keep track of the data intelligently. As Thlintoq said, you can safely ignore anything that's past what you're looking for. The hash table might help you group things... like, tasks that are all for a specific day (or half a day) could be stored in the same hash location, so when you search you just pick up the items that are reasonably close to what you're looking for. Then you would do a linear search on that particular group.

A pseudo-code example of what I'm trying to say...

Expand|Select|Wrap|Line Numbers
  1. struct TaskStorageObject
  2. {
  3.   string key;
  4.   List<Task> taskList;
  5. }
  6.  
  7. HashTable taskStorage = new HashTable();
  8.  
  9. main method
  10. {
  11.   ...
  12.   Task myTask = new Task(<DateTime object for occurance>, <whatever>);
  13.   InsertTask(taskStorage, myTask);
  14. }
  15.  
  16. InsertTask(HashTable taskStorage, Task taskToInsert)
  17. {
  18.   string key = taskToInsert.TaskTime.Date.ToString(); // ie, "21-10-2009"
  19.   TaskStorageObject taskSet = taskStorage[key];
  20.   if (taskSet != null)
  21.   {
  22.     taskSet.taskList.Add(taskToInsert);
  23.   }
  24.   else
  25.   {
  26.     // create a task storage object, put your task in it, and put it in the hash table
  27.   }
  28. }
Retrieval would be done in a similar way... you'd have a DateTime for what you're looking for so you'd get the TaskSet for the day and then perform a linear search on that to find the particular task.

You can break it down however you like though, I'm just trying to throw out suggestions. Between this and what Thlintoq wrote, hopefully you've got a few ideas now :)

(Does this seem reasonable or make sense?)
Member
 
Join Date: Jul 2008
Posts: 52
#4: Oct 22 '09

re: Data Structure help required.


Thanks tlhintoq and Thank You Gary for your ideas. I think I have got the idea.
manipulating the List/Hashtable with every alarm event is the need.
Also this will save me from initiating the background thread for every timer tick.

Regards,
Sid
Reply

Tags
.net design, c sharp, data structure, design, list