A binary lookup tree of intervals. The intervals contained in the tree are represented using ``Interval(a, b, data)`` objects. Each such object represents a half-open interval ``[a, b)`` with optional data. Examples: --------- Initialize a blank tree:: >>> tree =
| 42 | |
| 43 | # noinspection PyBroadException |
| 44 | class IntervalTree(MutableSet): |
| 45 | """ |
| 46 | A binary lookup tree of intervals. |
| 47 | The intervals contained in the tree are represented using ``Interval(a, b, data)`` objects. |
| 48 | Each such object represents a half-open interval ``[a, b)`` with optional data. |
| 49 | |
| 50 | Examples: |
| 51 | --------- |
| 52 | |
| 53 | Initialize a blank tree:: |
| 54 | |
| 55 | >>> tree = IntervalTree() |
| 56 | >>> tree |
| 57 | IntervalTree() |
| 58 | |
| 59 | Initialize a tree from an iterable set of Intervals in O(n * log n):: |
| 60 | |
| 61 | >>> tree = IntervalTree([Interval(-10, 10), Interval(-20.0, -10.0)]) |
| 62 | >>> tree |
| 63 | IntervalTree([Interval(-20.0, -10.0), Interval(-10, 10)]) |
| 64 | >>> len(tree) |
| 65 | 2 |
| 66 | |
| 67 | Note that this is a set, i.e. repeated intervals are ignored. However, |
| 68 | Intervals with different data fields are regarded as different:: |
| 69 | |
| 70 | >>> tree = IntervalTree([Interval(-10, 10), Interval(-10, 10), Interval(-10, 10, "x")]) |
| 71 | >>> tree |
| 72 | IntervalTree([Interval(-10, 10), Interval(-10, 10, 'x')]) |
| 73 | >>> len(tree) |
| 74 | 2 |
| 75 | |
| 76 | Insertions:: |
| 77 | >>> tree = IntervalTree() |
| 78 | >>> tree[0:1] = "data" |
| 79 | >>> tree.add(Interval(10, 20)) |
| 80 | >>> tree.addi(19.9, 20) |
| 81 | >>> tree |
| 82 | IntervalTree([Interval(0, 1, 'data'), Interval(10, 20), Interval(19.9, 20)]) |
| 83 | >>> tree.update([Interval(19.9, 20.1), Interval(20.1, 30)]) |
| 84 | >>> len(tree) |
| 85 | 5 |
| 86 | |
| 87 | Inserting the same Interval twice does nothing:: |
| 88 | >>> tree = IntervalTree() |
| 89 | >>> tree[-10:20] = "arbitrary data" |
| 90 | >>> tree[-10:20] = None # Note that this is also an insertion |
| 91 | >>> tree |
| 92 | IntervalTree([Interval(-10, 20), Interval(-10, 20, 'arbitrary data')]) |
| 93 | >>> tree[-10:20] = None # This won't change anything |
| 94 | >>> tree[-10:20] = "arbitrary data" # Neither will this |
| 95 | >>> len(tree) |
| 96 | 2 |
| 97 | |
| 98 | Deletions:: |
| 99 | >>> tree = IntervalTree(Interval(b, e) for b, e in [(-10, 10), (-20, -10), (10, 20)]) |
| 100 | >>> tree |
| 101 | IntervalTree([Interval(-20, -10), Interval(-10, 10), Interval(10, 20)]) |
no outgoing calls
searching dependent graphs…