In-place writes on UInt64Array #
Writes through slices, and folds that write each element back in place.
Writes through slices #
Replaces the element at index i of the slice, counted from the start of the slice, in the
underlying array. The array is updated in place when neither it nor the slice is shared, so a slice
taken from an array that is no longer used writes into that array. s.array gives back the whole
array afterwards.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Replaces the element at index i of the slice in the underlying array, like set. If i is out of
bounds, the slice is returned unmodified.
Equations
- s.set! i v = if h : i < Std.Slice.size s then s.set i v h else s
Instances For
Applies f to the element at index i of the slice, in place like set. If i is out of bounds,
the slice is returned unmodified.
Instances For
Swaps the elements at indices i and j of the slice, in place like set.
Instances For
The loop of reverse: swaps the elements at i and j, then moves both towards the middle.
Equations
- ScalarArray.UInt64Array.Slice.reverse.loop s i j hj = if h : i < j then ScalarArray.UInt64Array.Slice.reverse.loop (s.swap i j ⋯ hj) (i + 1) (j - 1) ⋯ else s
Instances For
Reverses the order of the elements of the slice, in place like set. The array is made exclusive
once, before the loop, which then stores without checks.
Equations
- s.reverse = if h : 0 < Std.Slice.size s then ScalarArray.UInt64Array.Slice.reverse.loop s 0 (Std.Slice.size s - 1) ⋯ else s
Instances For
In-place folds over slices #
The loop of foldlInPlace, over Nat indices.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Replaces each element of the slice while threading an accumulator, in place like set: for each
k < s.size, from 0 up, the element x at index k of the slice becomes the first component of
f k hk x b, whose second component is the next accumulator. Returns the new slice and the last
accumulator.
The array is made exclusive once, before the loop, which then stores without checks, where each
set checks at every write. f only sees the elements, never the array, which is what makes
skipping the checks safe. The proof hk : k.toNat < s.size lets f read other arrays with
uget.
Equations
- s.foldlInPlace init f = ScalarArray.UInt64Array.Slice.foldlInPlace.loop (Std.Slice.size s) f s ⋯ 0 init
Instances For
foldlInPlace leaves the elements of the array outside the slice unchanged.
foldlInPlace as a pure left fold over the indices of the slice: the new elements of the slice and
the last accumulator, in one statement. The Then form follows by foldlInPlaceThen_eq.
The loop of foldrInPlace, over Nat indices, from k - 1 down to 0.
Equations
- One or more equations did not get rendered due to their size.
- ScalarArray.UInt64Array.Slice.foldrInPlace.loop n f s hs 0 x_3 x = (s, x)
Instances For
Replaces each element of the slice while threading an accumulator, like foldlInPlace but from
the last element to the first: for each k < s.size, from s.size - 1 down to 0, the element
x at index k of the slice becomes the first component of f k hk x b, whose second component
is the next accumulator. Returns the new slice and the last accumulator.
The array is made exclusive once, before the loop, which then stores without checks.
Equations
- s.foldrInPlace init f = ScalarArray.UInt64Array.Slice.foldrInPlace.loop (Std.Slice.size s) f s ⋯ (Std.Slice.size s) ⋯ init
Instances For
foldrInPlace leaves the elements of the array outside the slice unchanged.
foldrInPlace as a pure right fold over the indices of the slice: the new elements of the slice
and the last accumulator, in one statement. The Then form follows by foldrInPlaceThen_eq.
foldlInPlace followed by g on the new slice and the last accumulator, which the loop calls
itself where foldlInPlace returns them as a pair. The pair is a heap object, allocated once per
call, which shows when the fold runs over short slices many times; when g consumes the
accumulator, for example by writing it into the slice, no pair is allocated at all.
foldlInPlace is the case g := Prod.mk.
Equations
- s.foldlInPlaceThen init f g = g (s.foldlInPlace init f).fst (s.foldlInPlace init f).snd
Instances For
foldrInPlace followed by g on the new slice and the last accumulator, which the loop calls
itself where foldrInPlace returns them as a pair. When g consumes the accumulator, no pair is
allocated. foldrInPlace is the case g := Prod.mk.
Equations
- s.foldrInPlaceThen init f g = g (s.foldrInPlace init f).fst (s.foldrInPlace init f).snd
Instances For
Folding over windows of one array #
The loop of foldlInPlace2, over Nat offsets and indices.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Writes the window of len elements at rs from the windows at xs and ys of the same array,
in place, threading an accumulator: for each j < len, from 0 up, it reads x := a[xs + j] and
y := a[ys + j] from the current array, then stores the first component of f j hj x y b at
rs + j, and the second component is the next accumulator. Returns the new array and the last
accumulator.
Each step reads after the writes of the earlier steps, so overlapping windows are well defined: with
rs = xs + 1, for example, step j reads what step j - 1 wrote. Reading the windows through f
rather than through another reference to a keeps a exclusive, which a second reference would
not, and then the first write would copy the whole array.
The array is made exclusive once, before the loop, which then reads and stores without checks.
Equations
- a.foldlInPlace2 xs ys rs len hx hy hr init f = ScalarArray.UInt64Array.foldlInPlace2.loop xs.toNat ys.toNat rs.toNat len.toNat f a hx hy hr 0 init
Instances For
foldlInPlace2 followed by g on the new array and the last accumulator, which the loop calls
itself where foldlInPlace2 returns them as a pair. When g consumes the accumulator, no pair is
allocated. g also receives a proof that the size is unchanged, so that it can go on with bounded
reads and writes, such as another foldlInPlace2Then.
Equations
- a.foldlInPlace2Then xs ys rs len hx hy hr init f g = g (a.foldlInPlace2 xs ys rs len hx hy hr init f).fst ⋯ (a.foldlInPlace2 xs ys rs len hx hy hr init f).snd
Instances For
foldlInPlace on the slice a[start.toNat...start.toNat + len.toNat], for bounds that are
already USize: returns the new array and the last accumulator. It is specified as that slice
fold, so the slice lemmas apply after foldlInPlaceRange_eq. The implementation runs the loop of
foldlInPlace on the USize bounds directly, without building the slice, whose Nat bounds
cost a conversion on the way in and out of every call.
Equations
- One or more equations did not get rendered due to their size.
Instances For
foldlInPlaceRange followed by g on the new array and the last accumulator, which the loop
calls itself where foldlInPlaceRange returns them as a pair, like Slice.foldlInPlaceThen.
g also receives a proof that the size is unchanged, like foldlInPlace2Then, so that it can go
on with bounded reads and writes.
Equations
- a.foldlInPlaceRangeThen start len h init f g = g (a.foldlInPlaceRange start len h init f).fst ⋯ (a.foldlInPlaceRange start len h init f).snd
Instances For
foldlInPlaceRange with two accumulators: returns the new array and the two last accumulators.
It is specified as foldlInPlaceRange on the pair of accumulators, so the lemmas about that fold
apply after foldlInPlaceRangeWith2Acc_eq. The implementation passes the two accumulators as
separate arguments of the loop, where a pair would be allocated at every element and would box
scalar accumulators such as UInt64 carries.
Equations
- One or more equations did not get rendered due to their size.
Instances For
foldlInPlaceRangeWith2Acc followed by g on the new array, a proof that its size is unchanged
and the two last accumulators, like foldlInPlaceRangeThen.
Equations
- One or more equations did not get rendered due to their size.
Instances For
foldrInPlace on the slice a[start.toNat...start.toNat + len.toNat], for bounds that are
already USize, like foldlInPlaceRange: returns the new array and the last accumulator.
Equations
- One or more equations did not get rendered due to their size.
Instances For
foldrInPlaceRange followed by g on the new array, a proof that its size is unchanged and the
last accumulator, like foldlInPlaceRangeThen.
Equations
- a.foldrInPlaceRangeThen start len h init f g = g (a.foldrInPlaceRange start len h init f).fst ⋯ (a.foldrInPlaceRange start len h init f).snd