/* * Copyright 2026 Flix Authors * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */pubmod Range {////// Represents a range (b, e) of discrete values from `b` (inclusive) to `e` (exclusive).///pubenumRange[t] withEq, ToString {case Range(t, t) }instanceForEach[Range[t]] withDiscrete[t] {typeElm = tpubdefforEach(f: t -> Unit \ ef, t: Range[t]): Unit \ ef = Range.forEach(f, t) }instanceIterable[Range[t]] withDiscrete[t] {typeElm = tpubdefiterator(rc: Region[r], rng: Range[t]): Iterator[t, r, r] \ r = Range.iterator(rc, rng) }////// Returns `true` if and only if the range `r` is empty, i.e. it contains no elements.///pubdefisEmpty(r: Range[t]): BoolwithOrder[t] =let Range(b, e) = r;not (b<e)////// Returns `true` if and only if `x` is a member of the range `r`.///pubdefmemberOf(x: t, r: Range[t]): BoolwithOrder[t] =let Range(b, e) = r;b<=xandx<e////// Returns `true` if and only if the ranges `r1` and `r2` overlap, i.e. they share at least one element.///pubdefoverlaps(r1: Range[t], r2: Range[t]): BoolwithOrder[t] =notisEmpty(intersection(r1, r2))////// Returns the intersection of the ranges `r1` and `r2`.////// The result is empty if `r1` and `r2` do not overlap.///pubdefintersection(r1: Range[t], r2: Range[t]): Range[t] withOrder[t] =let Range(b1, e1) = r1;let Range(b2, e2) = r2; Range(Order.max(b1, b2), Order.min(e1, e2))////// Applies `f` to every element of `r`.///pubdefforEach(f: t -> Unit \ ef, r: Range[t]): Unit \ efwithDiscrete[t] =let Range(b, e) = r;defloop(i) =if (i<e) {f(i); loop(Discrete.succ(i)) };loop(b)////// Returns an iterator over the elements of `r`.///pubdefiterator(rc: Region[r], rng: Range[t]): Iterator[t, r, r] \ rwithDiscrete[t] =let Range(b, e) = rng;Iterator.unfold(rc, i -> if (i<e) Some((i, Discrete.succ(i))) else None, b)////// Returns the elements of `r` as a list.///pubdeftoList(r: Range[t]): List[t] withDiscrete[t] = regionrc {Range.iterator(rc, r) |> Iterator.toList }////// Returns the elements of `r` as a set.///pubdeftoSet(r: Range[t]): Set[t] withDiscrete[t] = regionrc {Range.iterator(rc, r) |> Iterator.toSet }////// Returns the elements of `r` as a vector.///pubdeftoVector(r: Range[t]): Vector[t] withDiscrete[t] = regionrc {Range.iterator(rc, r) |> Iterator.toVector }}