@@ -10,21 +10,44 @@ public import Init.Data.Slice.Basic
1010public import Init.Data.Slice.Notation
1111public import Init.Data.Range.Polymorphic.Nat
1212
13+ set_option linter.missingDocs true
14+
1315/-!
1416This module provides slice notation for list slices (a.k.a. `Sublist`) and implements an iterator
1517for those slices.
1618-/
1719
1820open Std Std.Slice Std.PRange
1921
22+ /--
23+ Internal representation of `ListSlice`, which is an abbreviation for `Slice ListSliceData`.
24+ -/
2025public class Std.Slice.Internal.ListSliceData (α : Type u) where
26+ /-- The relevant suffix of the underlying list. -/
2127 list : List α
28+ /-- The maximum length of the slice, starting at the beginning of `list`. -/
2229 stop : Nat
2330
31+ /--
32+ A region of some underlying list. List slices can be used to avoid copying or allocating space,
33+ while being more convenient than tracking the bounds by hand.
34+
35+ A list slice only stores the suffix of the underlying list, starting from the range's lower bound
36+ so that the cost of operations on the slice does not depend on the start position. However,
37+ the cost of creating a list slice is linear in the start position.
38+ -/
2439public abbrev ListSlice (α : Type u) := Slice (Internal.ListSliceData α)
2540
2641variable {α : Type u}
2742
43+ /--
44+ Returns a slice of a list with the given bounds.
45+
46+ If `start` or `stop` are not valid bounds for a subarray, then they are clamped to the list's length.
47+ Additionally, the starting index is clamped to the ending index.
48+
49+ This function is linear in `start` because it stores `as.drop start` in the slice.
50+ -/
2851public def List.toSlice (as : List α) (start : Nat) (stop : Nat) : ListSlice α :=
2952 if start ≤ stop then
3053 ⟨{ list := as.drop start, stop := stop - start }⟩
0 commit comments