chaimleib / intervaltree

A mutable, self-balancing interval tree. Queries may be by point, by range overlap, or by range containment.
Apache License 2.0
636 stars 108 forks source link

Remove overlaps #134

Open mbhall88 opened 9 months ago

mbhall88 commented 9 months ago

I love this library. I use it A LOT!

One function I would love to have on an IntervalTree is remove_overlaps. This would remove anyway interval within the tree that overlaps with another.

My current work around is effectively this

remove = []
for iv in tree:
    overlaps = list(tree.overlap(iv))
    if len(overlaps) > 1:
        remove.extend(overlaps)

for iv in remove:
    tree.remove(iv)
snewell92 commented 7 months ago

I would suggest splitting this functionality in two. I have a use case right now where I need to find all intervals in a given tree that overlap, which the first part of the above code does (which is what i will write), so splitting this up would yield an API like (pls excuse the quick psuedo code):

def get_all_overlaps(self):
    for interval in self:
        # TODO this will likely yield duplicate sets, so further refinement is needed
        yield tree.overlap(interval)

# suggest 'pruning' as the action to the tree, but remove still fine
def prune_all_overlaps(self):
    removed = []

    for overlaps in self.get_all_overlaps():
        for interval in overlaps:
            removed.append(interval)
            self.remove(interval)

    return removed