gtirb.lazyintervaltree module

Implements a simple wrapper that lazily initializes and updates an IntervalTree.

GTIRB uses IntervalTrees to accelerate certain operations. However, these operations are not always needed for a given GTIRB object or by a given GTIRB analysis. To prevent scripts that do not need the IntervalTrees from wasting time updating the data structures, the LazyIntervalTree in this module delays instantiating or updating the tree. Instead, it queues the updates so they can be rapidly applied when the script invokes an operation that requires an up-to-date tree.

class gtirb.lazyintervaltree.IntervalBuilder(*args, **kwargs)[source]

Bases: Protocol[_Kco, _V]

Gets an interval for certain values.

If no interval is available for a particular value, returns None instead.

__call__(node)[source]

Call self as a function.

__init__(*args, **kwargs)
__protocol_attrs__ = {'__call__'}
class gtirb.lazyintervaltree.LazyIntervalTree(values, make_interval)[source]

Bases: Generic[_K, _V]

Simple wrapper to lazily initialize and update an IntervalTree.

The underlying IntervalTree can be retrieved by calling get(). This will ensure that the tree is up-to-date with all intermediate modifications before returning it.

In many algorithms, the tree may receive large numbers of modifications, adding and removing the same intervals several times before querying. In these cases, it may be faster to rebuild the tree from scratch rather than perform all of the intermediate modifications. For this reason, get() is not guaranteed to always return the same tree object. That is, the tree returned by get() should not be cached; calling get() may return a new tree rather than updating the tree it returned previously.

__init__(values, make_interval)[source]

Create a new lazy tree.

Parameters:
  • values (typing.Collection[typing.TypeVar(_V)]) – collection of values from which the tree can be rebuilt

  • make_interval (gtirb.lazyintervaltree.IntervalBuilder[typing.TypeVar(_K), typing.TypeVar(_V)]) – callable to get an interval for a value

add(value)[source]

Add a value to the tree.

Return type:

None

discard(value)[source]

Remove a value from the tree.

Does nothing if the interval with that value is not present.

Return type:

None

get()[source]

Get the most up-to-date tree reflecting all pending updates.