Equations
- mkArray n v = { data := List.replicate n v }
ofFn f
with f : Fin n → α
returns the list whose ith element is f i
.
ofFn f = #[f 0, f 1, ... , f(n - 1)]
Equations
- Array.ofFn f = Array.ofFn.go f 0 (Array.mkEmpty n)
Auxiliary for ofFn
. ofFn.go f i acc = acc ++ #[f i, ..., f(n - 1)]
Equations
- Array.ofFn.go f i acc = if h : i < n then Array.ofFn.go f (i + 1) (acc.push (f ⟨i, h⟩)) else acc
The array #[0, 1, ..., n - 1]
.
Equations
- Array.range n = Nat.fold (flip Array.push) n (Array.mkEmpty n)
Equations
- Array.instEmptyCollection = { emptyCollection := #[] }
Swaps two entries in an array.
This will perform the update destructively provided that a
has a reference
count of 1 when called.
Equations
- a.swap i j = let v₁ := a.get i; let v₂ := a.get j; let a' := a.set i v₂; a'.set (⋯ ▸ j) v₁
Swaps two entries in an array, or panics if either index is out of bounds.
This will perform the update destructively provided that a
has a reference
count of 1 when called.
Equations
- a.shrink n = Array.shrink.loop (a.size - n) a
Equations
- Array.shrink.loop 0 x = x
- Array.shrink.loop n.succ x = Array.shrink.loop n x.pop
We claim this unsafe implementation is correct because an array cannot have more than usizeSz
elements in our runtime.
This kind of low level trick can be removed with a little bit of compiler support. For example, if the compiler simplifies as.size < usizeSz
to true.
Equations
- as.forInUnsafe b f = let sz := USize.ofNat as.size; Array.forInUnsafe.loop as f sz 0 b
See comment at forInUnsafe
Equations
- Array.foldlMUnsafe f init as start stop = if start < stop then if stop ≤ as.size then Array.foldlMUnsafe.fold f as (USize.ofNat start) (USize.ofNat stop) init else pure init else pure init
Equations
- Array.foldlMUnsafe.fold f as i stop b = if (i == stop) = true then pure b else do let __do_lift ← f b (as.uget i ⋯) Array.foldlMUnsafe.fold f as (i + 1) stop __do_lift
Reference implementation for foldlM
Equations
- One or more equations did not get rendered due to their size.
Equations
- Array.foldrMUnsafe.fold f as i stop b = if (i == stop) = true then pure b else do let __do_lift ← f (as.uget (i - 1) ⋯) b Array.foldrMUnsafe.fold f as (i - 1) stop __do_lift
Reference implementation for foldrM
Equations
- One or more equations did not get rendered due to their size.
See comment at forInUnsafe
Equations
- Array.mapMUnsafe f as = let sz := USize.ofNat as.size; unsafeCast (Array.mapMUnsafe.map f sz 0 (unsafeCast as))
Reference implementation for mapM
Equations
- Array.mapM f as = Array.mapM.map f as 0 (Array.mkEmpty as.size)
Equations
- Array.mapM.map f as i r = if hlt : i < as.size then do let __do_lift ← f as[i] Array.mapM.map f as (i + 1) (r.push __do_lift) else pure r
Equations
- as.mapIdxM f = Array.mapIdxM.map as f as.size 0 ⋯ (Array.mkEmpty as.size)
Equations
- One or more equations did not get rendered due to their size.
- Array.mapIdxM.map as f 0 j x bs = pure bs
Equations
- Array.anyM p as start stop = let any := fun (stop : Nat) (h : stop ≤ as.size) => Array.anyM.loop p as stop h start; if h : stop ≤ as.size then any stop h else any as.size ⋯
Equations
- Array.allM p as start stop = do let __do_lift ← Array.anyM (fun (v : α) => do let __do_lift ← p v pure !__do_lift) as start stop pure !__do_lift
Equations
- Array.forM f as start stop = Array.foldlM (fun (x : PUnit) => f) PUnit.unit as start stop
Equations
- Array.forRevM f as start stop = Array.foldrM (fun (a : α) (x : PUnit) => f a) PUnit.unit as start stop
Equations
- Array.foldl f init as start stop = (Array.foldlM f init as start stop).run
Equations
- Array.foldr f init as start stop = (Array.foldrM f init as start stop).run
Equations
- a.findSome! f = match a.findSome? f with | some b => b | none => panicWithPosWithDecl "Init.Data.Array.Basic" "Array.findSome!" 450 14 "failed to find element"
Equations
- as.findIdx? p = Array.findIdx?.loop as p 0
Equations
- Array.elem a as = as.contains a
Equations
- One or more equations did not get rendered due to their size.
Convert a Array α
into an List α
. This is O(n) in the size of the array.
Equations
- as.toList = Array.foldr List.cons [] as as.size
Prepends an Array α
onto the front of a list. Equivalent to as.toList ++ l
.
Equations
- as.toListAppend l = Array.foldr List.cons l as as.size
Equations
- as.append bs = Array.foldl (fun (r : Array α) (v : α) => r.push v) as bs 0
Equations
- as.appendList bs = List.foldl (fun (r : Array α) (v : α) => r.push v) as bs
Equations
- Array.concatMap f as = Array.foldl (fun (bs : Array β) (a : α) => bs ++ f a) #[] as 0
Equations
- One or more equations did not get rendered due to their size.
Equations
- Array.filterM p as start stop = Array.foldlM (fun (r : Array α) (a : α) => do let __do_lift ← p a if __do_lift = true then pure (r.push a) else pure r) #[] as start stop
Equations
- Array.filterMap f as start stop = (Array.filterMapM f as start stop).run
Equations
- as.reverse = if h : as.size ≤ 1 then as else Array.reverse.loop as 0 ⟨as.size - 1, ⋯⟩
Equations
- Array.reverse.loop as i j = if h : i < ↑j then let_fun this := ⋯; let as_1 := as.swap ⟨i, ⋯⟩ j; let_fun this := ⋯; Array.reverse.loop as_1 (i + 1) ⟨↑j - 1, this⟩ else as
Equations
- Array.popWhile p as = if h : as.size > 0 then if p (as.get ⟨as.size - 1, ⋯⟩) = true then Array.popWhile p as.pop else as else as
Equations
- Array.takeWhile p as = Array.takeWhile.go p as 0 #[]
Equations
- Array.takeWhile.go p as i r = if h : i < as.size then let a := as.get ⟨i, h⟩; if p a = true then Array.takeWhile.go p as (i + 1) (r.push a) else r else r
Remove the element at a given index from an array without bounds checks, using a Fin
index.
This function takes worst case O(n) time because
it has to backshift all elements at positions greater than i
.
Remove the element at a given index from an array, or do nothing if the index is out of bounds.
This function takes worst case O(n) time because
it has to backshift all elements at positions greater than i
.
Insert element a
at position i
.
Equations
- as.insertAt i a = let j := as.size; let as_1 := as.push a; Array.insertAt.loop as i as_1 ⟨j, ⋯⟩
Equations
- Array.insertAt.loop as✝ i as j = if ↑i < ↑j then let j' := ⟨↑j - 1, ⋯⟩; let as_1 := as.swap j' j; Array.insertAt.loop as✝ i as_1 ⟨↑j', ⋯⟩ else as
Insert element a
at position i
. Panics if i
is not i ≤ as.size
.
Equations
- as.insertAt! i a = if h : i ≤ as.size then as.insertAt ⟨i, ⋯⟩ a else panicWithPosWithDecl "Init.Data.Array.Basic" "Array.insertAt!" 780 7 "invalid index"
Equations
- a.toArrayLit n hsz = List.toArray (a.toListLitAux n hsz n ⋯ [])
Equations
- as.allDiff = Array.allDiffAux as 0
Equations
- as.zipWith bs f = Array.zipWithAux f as bs 0 #[]