Skip to content

util/rb_tree: Add augmented trees and interval trees

Connor Abbott requested to merge cwabbott0/mesa:review/interval-tree into main

From the last commit:

    An "augmented tree" is a tree with extra data attached which flows from
    the leaves to the root. An "interval tree" is a datastructure of
    (potentially-overlapping) intervals where, in addition to inserting and
    removing intervals, we can quickly lookup all the intervals which
    overlap a given interval.
    
    After describing red-black trees, CLRS explains how it's possible to
    implement an interval tree using an augmented red-black tree where the
    nodes are ordered by interval start and each node also stores the
    maximum interval end for its entire subtree.
    
    Implement the interval tree extension described by CLRS. Iterating over
    all overlapping intervals is actually an exercise, so we have to solve
    the exercise. The recursive solution has been re-written to use the
    parent pointers to avoid needing a stack, similarly to rb_tree_first()
    and rb_node_next().
    
    For now, we only implement unsigned intervals, but the core algorithms
    are all abstracted to allow other types. There's still some boilerplate,
    but it's the best that can be done in C.

I needed interval trees for a use-case in ir3.

Merge request reports