|
| 1 | +#= Abstract Arrays |
| 2 | +https://docs.julialang.org/en/v1/manual/interfaces/ |
| 3 | +
|
| 4 | +Methods to implement Brief description |
| 5 | +size(A) Returns a tuple containing the dimensions of A |
| 6 | +getindex(A, i::Int) (if IndexLinear) Linear scalar indexing |
| 7 | +getindex(A, I::Vararg{Int, N}) (if IndexCartesian, where N = ndims(A)) N-dimensional scalar indexing |
| 8 | +
|
| 9 | +Optional methods Default definition Brief description |
| 10 | +IndexStyle(::Type) IndexCartesian() Returns either IndexLinear() or IndexCartesian(). See the description below. |
| 11 | +setindex!(A, v, i::Int) (if IndexLinear) Scalar indexed assignment |
| 12 | +setindex!(A, v, I::Vararg{Int, N}) (if IndexCartesian, where N = ndims(A)) N-dimensional scalar indexed assignment |
| 13 | +getindex(A, I...) defined in terms of scalar getindex Multidimensional and nonscalar indexing |
| 14 | +setindex!(A, X, I...) defined in terms of scalar setindex! Multidimensional and nonscalar indexed assignment |
| 15 | +iterate defined in terms of scalar getindex Iteration |
| 16 | +length(A) prod(size(A)) Number of elements |
| 17 | +similar(A) similar(A, eltype(A), size(A)) Return a mutable array with the same shape and element type |
| 18 | +similar(A, ::Type{S}) similar(A, S, size(A)) Return a mutable array with the same shape and the specified element type |
| 19 | +similar(A, dims::Dims) similar(A, eltype(A), dims) Return a mutable array with the same element type and size dims |
| 20 | +similar(A, ::Type{S}, dims::Dims) Array{S}(undef, dims) Return a mutable array with the specified element type and size |
| 21 | +
|
| 22 | +Non-traditional indices Default definition Brief description |
| 23 | +axes(A) map(OneTo, size(A)) Return a tuple of AbstractUnitRange{<:Integer} of valid indices |
| 24 | +similar(A, ::Type{S}, inds) similar(A, S, Base.to_shape(inds)) Return a mutable array with the specified indices inds (see below) |
| 25 | +similar(T::Union{Type,Function}, inds) T(Base.to_shape(inds)) Return an array similar to T with the specified indices inds (see below) |
| 26 | +=# |
| 27 | + |
| 28 | +# And arbitrary new type for array values |
| 29 | +struct ArrayTestVal |
| 30 | + a::Int |
| 31 | +end |
| 32 | + |
| 33 | +# In case `eltype` and `ndims` have custom methods |
| 34 | +# We should always be able to use these to mean the same thing |
| 35 | +_eltype(::AbstractArray{T}) where T = T |
| 36 | +_ndims(::AbstractArray{<:Any,N}) where N = N |
| 37 | + |
| 38 | +array_components = (; |
| 39 | + mandatory = (; |
| 40 | + type = A -> A isa AbstractArray, |
| 41 | + eltype = ( |
| 42 | + A -> eltype(A) isa Type, |
| 43 | + A -> eltype(A) == _eltype(A), |
| 44 | + ), |
| 45 | + ndims = ( |
| 46 | + A -> ndims(A) isa Int, |
| 47 | + A -> ndims(A) == _ndims(A), |
| 48 | + ), |
| 49 | + size = ( |
| 50 | + "size(A) returns a tuple of Integer" => A -> size(A) isa NTuple{<:Any,Integer}, |
| 51 | + "length of size(A) matches ndims(A)" => A -> length(size(A)) == ndims(A), |
| 52 | + ), |
| 53 | + getindex = ( |
| 54 | + "Can index with begin/firstinex" => A -> A[begin] isa eltype(A), |
| 55 | + "Can index with end/lastindex" => A -> A[end] isa eltype(A), |
| 56 | + "Can index with all indices in `eachindex(A)`" => A -> all(x -> A[x] isa eltype(A), eachindex(A)), |
| 57 | + "Can index with multiple dimensions" => A -> A[map(first, axes(A))...] isa eltype(A), |
| 58 | + "Can use trailing ones" => A -> A[map(first, axes(A))..., 1, 1, 1] isa eltype(A), |
| 59 | + "Can index with CartesianIndex" => A -> A[CartesianIndex(map(first, axes(A))...)] isa eltype(A), |
| 60 | + "Can use trailing ones in CartesianIndex" => A -> A[CartesianIndex(map(first, axes(A))..., 1, 1, 1)] isa eltype(A), |
| 61 | + ), |
| 62 | + indexstyle = "IndexStyle returns IndexCartesian or IndexLinear" => A -> IndexStyle(A) in (IndexCartesian(), IndexLinear()), |
| 63 | + ), |
| 64 | + # TODO implement all the optional conditions |
| 65 | + optional = (; |
| 66 | + setindex! = ( |
| 67 | + A -> length(A) > 1 || throw(ArgumentError("Test arrays must have more than one element to test setindex!")), |
| 68 | + "setindex! can write the first to the last element" => |
| 69 | + A -> begin |
| 70 | + x1 = A[begin]; x2 = A[end] |
| 71 | + A[begin] = x2 |
| 72 | + A[end] = x1 |
| 73 | + A[begin] == x2 && A[end] == x1 |
| 74 | + end, |
| 75 | + "setindex! can write the first to the last element using multidimensional indices" => |
| 76 | + A -> begin |
| 77 | + fs = map(first, axes(A)) |
| 78 | + ls = map(last, axes(A)) |
| 79 | + x1 = A[fs...]; |
| 80 | + x2 = A[ls...] |
| 81 | + A[fs...] = x2 |
| 82 | + A[ls...] = x1 |
| 83 | + A[fs...] == x2 && A[ls...] == x1 |
| 84 | + end, |
| 85 | + "setindex! can write to all indices in eachindex(A)" => |
| 86 | + A -> begin |
| 87 | + v = first(A) |
| 88 | + all(eachindex(A)) do i |
| 89 | + A[i] = v |
| 90 | + A[i] === v |
| 91 | + end |
| 92 | + end, |
| 93 | + "setindex! can write to all indices in CartesianIndices(A)" => |
| 94 | + A -> begin |
| 95 | + v = first(A) # We have already tested writing to the first index above |
| 96 | + all(CartesianIndices(A)) do i |
| 97 | + A[i] = v |
| 98 | + A[i] === v |
| 99 | + end |
| 100 | + end, |
| 101 | + ), |
| 102 | + similar_type = "`similar(A)` returns an object the same type and size as `A`" => |
| 103 | + A -> begin |
| 104 | + A1 = similar(A) |
| 105 | + A1 isa typeof(A) && size(A1) == size(A) |
| 106 | + end, |
| 107 | + similar_eltype = "similar(A, T::Type) returns an object the same base type as `A` with eltype of `T`" => |
| 108 | + A -> begin |
| 109 | + A1 = similar(A, ArrayTestVal); |
| 110 | + _wrappertype(A) == _wrappertype(A1) && eltype(A1) == ArrayTestVal && size(A) == size(A1) |
| 111 | + end, |
| 112 | + similar_size = "similar(A, s::NTuple{Int}) returns an object the same type as `A` with size `s`" => |
| 113 | + A -> begin |
| 114 | + A1 = similar(A, (2, 3)) |
| 115 | + A2 = similar(A, (4, 5)) |
| 116 | + _wrappertype(A) == _wrappertype(A1) && size(A1) == (2, 3) && size(A2) == (4, 5) |
| 117 | + end, |
| 118 | + similar_eltype_size = "similar(A, T::Type, s::NTuple{Int}) returns an object the same type as `A` with eltype `T` and size `s`" => |
| 119 | + A -> begin |
| 120 | + A1 = similar(A, ArrayTestVal, (2, 3)) |
| 121 | + A2 = similar(A, ArrayTestVal, (4, 5)) |
| 122 | + _wrappertype(A) == _wrappertype(A1) && eltype(A1) == ArrayTestVal && size(A1) == (2, 3) && size(A2) == (4, 5) |
| 123 | + end, |
| 124 | + ) |
| 125 | +) |
| 126 | + |
| 127 | +_wrappertype(A) = Base.typename(typeof(A)).wrapper |
| 128 | + |
| 129 | +@interface ArrayInterface AbstractArray array_components "Base Julia AbstractArray interface" |
0 commit comments