So I have a vector of objects. Each object has a range represented by a high & low value associated with it. For every object in the list, I need to find every other object which has an overlapping range.
I have an algorithm that works, but I suspect it could be faster.
Currently, I form a vector of pointers to the original list and sort it by the low value for the objects. I then walk through this list, and for each object, I do a binary search of the list from the current point forward for the objects high value. This diagram may help. This only captures the overlappers that have a higher low value then the current object, but that's OK. As long as each overlapping pair is captured somewhere, we're good, and these missing overlaps are captured in the other objects.
Any ideas? I was thinking of making a list with both the low & high values of each object, and sorting that. If you could look at each object and can know where it's high & low values resided in the list, you wouldn't have to do all those searches. You could just grab everything in the list between those positions, eliminate the duplicates, and go. But I can't seem to come up with a data structure that will accomplish that? How can I sort a list of objects such that each object knows its position in the final sorted list? I was thinking maybe some kind of tree representation instead of a flat vector, but haven't quite wrapped my mind around that yet.
I know I'm using list & vector interchangebly - I'm not worried about what kind of container I actually use. I currently use STL vectors. I need to do this in C++.
Any ideas?
I have an algorithm that works, but I suspect it could be faster.
Currently, I form a vector of pointers to the original list and sort it by the low value for the objects. I then walk through this list, and for each object, I do a binary search of the list from the current point forward for the objects high value. This diagram may help. This only captures the overlappers that have a higher low value then the current object, but that's OK. As long as each overlapping pair is captured somewhere, we're good, and these missing overlaps are captured in the other objects.
Any ideas? I was thinking of making a list with both the low & high values of each object, and sorting that. If you could look at each object and can know where it's high & low values resided in the list, you wouldn't have to do all those searches. You could just grab everything in the list between those positions, eliminate the duplicates, and go. But I can't seem to come up with a data structure that will accomplish that? How can I sort a list of objects such that each object knows its position in the final sorted list? I was thinking maybe some kind of tree representation instead of a flat vector, but haven't quite wrapped my mind around that yet.
I know I'm using list & vector interchangebly - I'm not worried about what kind of container I actually use. I currently use STL vectors. I need to do this in C++.
Any ideas?