"""Utilities for iteration over collections of values."""# mypy: disable-error-code="empty-body, misc, override, valid-type, no-untyped-def"from__future__importannotationsfromtypingimportTYPE_CHECKING,Any,no_type_checkfromguppylang_internals.decoratorimportcustom_function,extend_typefromguppylang_internals.definition.customimportNoopCompilerfromguppylang_internals.tys.builtinimportsized_iter_type_deffromguppylangimportguppyfromguppylang.std.optionimportOption,nothing,someifTYPE_CHECKING:fromguppylang.std.langimportcomptime,ownedfromguppylang.std.numimportnatL=guppy.type_var("L",copyable=False,droppable=False)n=guppy.nat_var("n")
[docs]@extend_type(sized_iter_type_def)classSizedIter:"""A wrapper around an iterator type `L` promising that the iterator will yield exactly `n` values. Annotating an iterator with an incorrect size is undefined behaviour. """
[docs]def__class_getitem__(cls,item:Any)->type:# Dummy implementation to allow subscripting of the `SizedIter` type in# positions that are evaluated by the Python interpreterreturncls
[docs]@custom_function(NoopCompiler())def__new__(iterator:L@owned)->SizedIter[L,n]:# type: ignore[type-arg]"""Casts an iterator into a `SizedIter`."""
[docs]@custom_function(NoopCompiler())defunwrap_iter(self:SizedIter[L,n]@owned)->L:"""Extracts the actual iterator."""
[docs]@custom_function(NoopCompiler())def__iter__(self:SizedIter[L,n]@owned)->SizedIter[L,n]:# type: ignore[type-arg]"""Dummy implementation making sized iterators iterable themselves."""
[docs]@guppy.overload(_range_comptime,_range1,_range2,_range3)defrange(start:int,stop:int=0,step:int=1)->Range:"""An iterator that yields a sequence of integers. Behaves like the builtin Python `range` function. Concretely, the ``i``th yielded number is ``start + i * step``. Numbers are yielded as long as they are * ``< stop`` in the case where ``step >= 0``, or * ``> stop`` otherwise. ``start`` defaults to ``0`` and ``step`` defaults to ``1``. If the provided ``stop`` value is comptime known, then the returned iterator will have a static size annotation and may for example be used inside array comprehensions. """