Consider a large set of floating-point intervals in 1-dimension, e.g.
[1.0, 2.5], 1.0 |---------------|2.5
[1.5, 3.6], 1.5|---------------------|3.6
.....
It is desired to find all intervals that contain a given point. For example given point = 1.2, algorithm should return the first interval, and if given point = 2.0, it should return the first two interval in the above example.
In the problem I am dealing, this operation needs to be repeated for a large number of times for a large number of intervals. Therefore a brute-force search is not desired and performance is an important factor.
After searching about it, I saw this problem is addressed using interval skip list in the context of computational geometry. I was wondering if there is any simple, efficient C++ implementation available.
EDIT: To be more precise about the problem, there are N intervals and for M points, it should be determined which intervals contain each point. N and M are large numbers where M is larger than N.
Suggest using CGAL range trees:
Wikipedia says interval trees (1-dimensional range trees) can "efficiently find all intervals that overlap with any given interval or point".
If your distribution of intervals allows it, it may be worth to consider a gridding approach: choose some grid size s
and create an array of lists. Every k
-th list enumerates the intervals that overlap with the "cell" [k.s, (k+1).s[
.
Then a query amounts to finding the cell that contains the query point (in O(1)
) and reporting all intervals in the list that effectively contain it (in O(K)
).
Both preprocessing time and storage are O(I.L+G)
where I
is the number of intervals and L
the average interval length in terms of the grid size and G
the total number of grid cells. s
must be chosen carefully.
来源:https://stackoverflow.com/questions/28639476/finding-all-intervals-that-overlap-a-point