API
Array types
MutableShiftedArrays.MutableShiftedArray
— TypeMutableShiftedArray(parent::AbstractArray, shifts = (), viewsize=size(v); default = zero(eltype(parent)))
Custom AbstractArray
object to store an AbstractArray
parent
shifted by shifts
steps (where shifts
is a Tuple
with one shift
value per dimension of parent
). As opposed to ShiftedArray
of the ShiftedArrays.jl
toolbox, this object is mutable and mutation operations in the padded ranges are ignored. Furthermore it also supports size changes in the view.
For s::MutableShiftedArray
, s[i...] == s.parent[map(-, i, s.shifts)...]
if map(-, i, s.shifts)
is a valid index for s.parent
, and s.v[i, ...] == default
otherwise. Use copy
to collect the values of a MutableShiftedArray
into a normal Array
. The recommended constructor is MutableShiftedArray(parent, shifts; default = missing)
.
If parent
is itself a MutableShiftedArray
with a compatible default value, the constructor does not nest MutableShiftedArray
objects but rather combines the shifts additively.
Arguments
parent::AbstractArray
: the array to be shiftedshifts::Tuple{Int}
: the amount by whichparent
is shifted in each dimension. The default, an empty Tuple will result in no shifts.viewsize::Tuple{Int}
: the size of the view. By default the size of the parent array is used.default::M
: the default value to return when out of bounds in the original array. By default zero of the correspondingeltype
is used. Note that usingmissing
as default value will cause single index accesses in CUDA due to the Union type.
Examples
julia> v = [1, 3, 5, 4];
julia> s = MutableShiftedArray(v, (1,))
4-element MutableShiftedVector{Int64, Int64, Vector{Int64}}:
0
1
3
5
julia> copy(s)
4-element Vector{Int64}:
0
1
3
5
julia> v = reshape(1:16, 4, 4);
julia> s = MutableShiftedArray(v, (0, 2), default=missing)
4×4 MutableShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
missing missing 1 5
missing missing 2 6
missing missing 3 7
missing missing 4 8
julia> shifts(s)
(0, 2)
MutableShiftedArrays.MutableShiftedVector
— TypeMutableShiftedVector{T, S<:AbstractArray}
Shorthand for MutableShiftedArray{T, 1, S}
.
Shifting operations
MutableShiftedArrays.lag
— Functionlag(v::AbstractArray, n = 1, viewsize=size(v); default=zero(eltype(v)))
Return a MutableShiftedArray
object which lazily represents the array v
shifted by n
(an Integer
or a Tuple
of Integer
s). If the number of dimensions of v
exceeds the length of n
, the shift in the remaining dimensions is assumed to be 0
. default
specifies a default value to return when out of bounds in the original array.
Examples
julia> v = [1, 3, 5, 4];
julia> MutableShiftedArrays.lag(v, default=missing)
4-element MutableShiftedVector{Int64, Missing, Vector{Int64}}:
missing
1
3
5
julia> w = 1:2:9
1:2:9
julia> s = MutableShiftedArrays.lag(w, 2, default=missing)
5-element MutableShiftedVector{Int64, Missing, StepRange{Int64, Int64}}:
missing
missing
1
3
5
julia> copy(s)
5-element Vector{Union{Missing, Int64}}:
missing
missing
1
3
5
julia> v = reshape(1:16, 4, 4);
julia> s = MutableShiftedArrays.lag(v, (0, 2), default=missing)
4×4 MutableShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
missing missing 1 5
missing missing 2 6
missing missing 3 7
missing missing 4 8
MutableShiftedArrays.lead
— Functionlead(v::AbstractArray, n = 1, viewsize=size(v); default=zero(eltype(v)))
Return a MutableShiftedArray
object which lazily represents the array v
shifted negatively by n
(an Integer
or a Tuple
of Integer
s). If the number of dimensions of v
exceeds the length of n
, the shift in the remaining dimensions is assumed to be 0
. default
specifies a default value to return when out of bounds in the original array.
Examples
julia> v = [1, 3, 5, 4];
julia> MutableShiftedArrays.lead(v, default=missing)
4-element MutableShiftedVector{Int64, Missing, Vector{Int64}}:
3
5
4
missing
julia> w = 1:2:9
1:2:9
julia> s = MutableShiftedArrays.lead(w, 2, default=missing)
5-element MutableShiftedVector{Int64, Missing, StepRange{Int64, Int64}}:
5
7
9
missing
missing
julia> copy(s)
5-element Vector{Union{Missing, Int64}}:
5
7
9
missing
missing
julia> v = reshape(1:16, 4, 4);
julia> s = MutableShiftedArrays.lead(v, (0, 2), default=missing)
4×4 MutableShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
9 13 missing missing
10 14 missing missing
11 15 missing missing
12 16 missing missing
Accessor functions
MutableShiftedArrays.shifts
— Functionshifts(s::ShiftedArray)
Return amount by which s
is shifted compared to parent(s)
.
MutableShiftedArrays.default
— Functiondefault(s::MutableShiftedArray)
Return default value.
Internals
MutableShiftedArrays.padded_tuple
— Functionpadded_tuple(v::AbstractVector, s)
Internal function used to compute shifts. Return a Tuple
with as many element as the dimensions of v
. The first length(s)
entries are filled with values from s
, the remaining entries are 0
. s
should be an integer, in which case length(s) == 1
, or a container of integers with keys 1:length(s)
.
Examples
julia> MutableShiftedArrays.padded_tuple(rand(10, 10), 3)
(3, 0)
julia> MutableShiftedArrays.padded_tuple(rand(10, 10), (4,))
(4, 0)
julia> MutableShiftedArrays.padded_tuple(rand(10, 10), (1, 5))
(1, 5)