hydra.overlay.python.util.cons_list module
Persistent (immutable) sequence backed by a native tuple.
Mirrors hydra.util.ConsList from Hydra-Java. Implements
collections.abc.Sequence so a ConsList IS a Sequence. Public APIs can
stay typed as Sequence[E] while internals get C-speed indexing,
iteration, and concatenation.
This implementation uses a native tuple as the backing store, with a
_start offset marking the logical beginning of the list within that
tuple. head/tail/drop are O(1): tail shares the same backing
tuple with _start incremented, no copy. cons (prepend) is still O(n)
by design – in codegen-heavy workloads, the dominant cost turned out to be
allocation per cell in a linked-list version, so the tuple copy wins despite
the asymptotic loss for prepend.
- class hydra.overlay.python.util.cons_list.ConsList(_inner: tuple[Any, ...] | None = None, _start: int = 0, *, _internal: bool = False)
Bases:
Sequence[T],Generic[T]A persistent (immutable) sequence backed by a native
tupleplus offset.Construct via
ConsList.empty(),ConsList.cons(x, xs),ConsList.singleton(x),ConsList.of(...), orConsList.from_iterable(...). Direct__init__is gated.- foldl(f: Callable[[R, T], R], initial: R) R
- foldr(f: Callable[[T, R], R], initial: R) R
- property head: T
- is_empty() bool
- last() T