Efficient Calendar Searching?
Posted: Sat Oct 18, 2008 10:47 am
I think this probably is better situated in PHP - Code, to which I have moved it, leaving a 'shadow' topic in PHP - Theory and Design.
Not sure if this is the right place and apologies to any moderator who feels it should be moved to a different forum.
Anyway, I building a Calendar object to hold an array of Event objects (each object simply has a title, start_date, end_date).
I want to be able to call the following:
Anyway this is trivial to accomplish using a foreach loop and looping through each event and comparing the $start and $end date to the event's $start or $end date.
E.g. To implement the method getEventsBetweenDates($start, $end) you can easily do the following
E.g. Similarly to implement the method getEventsOverDates($start, $end) you can easily do the following
What I don't like here is that it's pretty inefficient because I have to start at the start and loop through all entries so I'm wondering if anyone knows of a good way to organise the array and use a better searching algorithm than just looping through the entries. I did look a bit at using a binary search algorithm but you'd need two (one for the start dates and one for the end dates) and then you have to merge your set of results from the start index with the ones that matched against the end index, and this also seems like overkill.
I should add that I'm using the Zend Framework, but I didn't see anything there that helps here. I checked out Zend_Search_Lucene to see if they had an in-memory implementation, but they didn't and the documentation said it was quite intensive which probably translates into overkill for this class.
Not sure if this is the right place and apologies to any moderator who feels it should be moved to a different forum.
Anyway, I building a Calendar object to hold an array of Event objects (each object simply has a title, start_date, end_date).
I want to be able to call the following:
Code: Select all
// events that start on/after $start and end on/before $end
$events = $calendar->getEventsBetweenDates($start, $end);
// returns events that start or end anywhere within the specified range
// (hmm suggestions for a better method name are welcome)
$events = $calendar->getEventsOverDates($start, $end);E.g. To implement the method getEventsBetweenDates($start, $end) you can easily do the following
Code: Select all
foreach($eventArray as $evt)
{
if( ($start <= $evt->start && $evt->end <= $end)
{
$result[] = $evt;
}
}Code: Select all
foreach($eventArray as $evt)
{
if( ($start <= $evt->start && $evt->end <= $end)
|| ($evt->start <= $start && $evt->end => $end)
|| ($start => $evt->start && $evt->start <= $end)
|| ($evt->end >= $start && $evt->end <= $end))
{
$result[] = $evt;
}
}I should add that I'm using the Zend Framework, but I didn't see anything there that helps here. I checked out Zend_Search_Lucene to see if they had an in-memory implementation, but they didn't and the documentation said it was quite intensive which probably translates into overkill for this class.