I have two sets of ranges. Each range is a pair of integers (beginning and end) representing a certain subrange of one larger range. Two sets of ranges are in a structure similar to this (of course ... s will be replaced by actual numbers).
$a_ranges = { a_1 => { start => ..., end => ..., }, a_2 => { start => ..., end => ..., }, a_3 => { start => ..., end => ..., },
I need to determine which ranges from set A overlap, with which they vary from set B. Given the two ranges, it is easy to determine whether they overlap. I just used a double loop to do this: swipe all the elements in set A in the outer loop, swipe all the elements in set B in the inner loop, and keep track of which ones overlap.
I have two problems with this approach. Firstly, overlap space is extremely rare - even if there are thousands of ranges in each set, I expect each range from set A to overlap, possibly with 1 or 2 ranges from set B. My approach lists every single possibility that is overkill . This leads to my second problem - that it is very weak. The code ends very quickly (will be crushed) when there are hundreds of ranges in each set, but it takes a very long time (+/- 30 minutes) when there are thousands of ranges in each set.
Is there a better way to index these ranges so that I don't do so many extra checks for overlapping?
Update . The result I'm looking for is two hashes (one for each set of ranges), where the keys are the identifiers of the range and the values ββare the identifiers of the ranges from another set that overlap with the given range in that set.
source share