I have a set of items strictly ordered on time. Each item can either be single (no Next, no Previous) or part of a span (with either 1 or 2 further endpoints). For example in the picture A is part of a 3 element span 1 -> 4 -> 5, B is part of a 2 element span 2 -> 3 and C is part of a 2 element span 6 -> 16. As you can see, spans can overlap each other (start2 > start1, end1 > end2).
What I want to do is work out a fast algorithm for finding which spans (or individuals) are intersecting a given span Min -> Max.
My naive approach is to simply test every single item in the set, with something like:
for(auto const & e : items)
{
if(e.Previous)
{
// Middle or last item in a set (already been tested).
continue;
}
auto t1 = e.Time, t2 = e.Time;
if(e.Next)
{
if(e.Next.Next)
{
// Part of a 3 item span (start, middle, end).
t2 = e.Next.Next.Time;
}
else
{
// Part of a 2 item span (start, end).
t2 = e.Next.Time;
}
}
if(t1 > MaxTime || t2 < MinTime)
{
// Does not intersect the span.
continue;
}
... Do something with the item, such as draw it.
}
This doesn't perform too badly with 250,000 items actually but starts to be pretty slow when you get into millions.
I have plenty of time available at start-up to get a std:future up and running to pre-process these things into some more useful structure if that helps. I also need to be able to append to the end of the set, though keeping strictly ascending time order (i.e. now() >= items.last().time).
Does anything occur to anyone?
