in reply to Optimizing a double loop
Yes, I'm serious. Depending on your actual problem, there might be a way around it.
If you read abut inversion lists, you find that this idea can be generalized to overlapping lists.
Assume you have the ranges 10-50 and 20-100; You mark each start of a range with a positive number, and each end of range by a negative number. So 1-50 and 20-100 would translate to
[1, 20, -50, -100]
That way you only store two integers for a large range.
If you want to know how many ranges overlap at a given point, say at 30, you just traverse the array from left to right up to abs($_) <= 30, and count how many positive and how many negative numbers you have encountered; the difference is the number of overlapping ranges.
You can also cache such numbers as long as the array doesn't change, to avoid having it to iterate it from the beginning every time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Optimizing a double loop
by roibrodo (Sexton) on Jun 03, 2010 at 12:40 UTC | |
by moritz (Cardinal) on Jun 03, 2010 at 12:44 UTC | |
by roibrodo (Sexton) on Jun 03, 2010 at 12:51 UTC | |
by moritz (Cardinal) on Jun 03, 2010 at 14:02 UTC | |
by roibrodo (Sexton) on Jun 03, 2010 at 14:17 UTC | |
| |
by ww (Archbishop) on Jun 03, 2010 at 12:44 UTC |