From eee4e9ad308c804abb74cd11aedf18c0b8b25c1d Mon Sep 17 00:00:00 2001 From: Stanislav Kazmin Date: Mon, 30 Sep 2019 10:58:14 +0200 Subject: [PATCH 1/4] Adding an example of an array type wrapper without loose of perfomrance. --- doc/src/manual/interfaces.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index d5a53e93d6843..5c853ab018e4d 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -401,6 +401,29 @@ so that the `dims` argument (ordinarily a `Dims` size-tuple) can accept `Abstrac perhaps range-types `Ind` of your own design. For more information, see [Arrays with custom indices](@ref man-custom-indices). +Sometimes you need to wrap a standard array into an extra type to be able to dispatch on this type but without +the need of any real changes to the standard array behavior. +The following example introduces such a wrapper type and ensures that the new type will work at the same +speed as a standard array type. +The key point is the `Base.@propagate_inbounds` which allows predefined functions like `sum` +to skip bounds checking as they do for a standard array. + +```jldoctest wrapperarray +julia> mutable struct MyArray{T,N} <: AbstractArray{T,N} + a::Array{T,N} + end + +julia> Base.size(A::MyArray) = size(A.a) + +julia> Base.@propagate_inbounds Base.getindex(A::MyArray, i...) = getindex(A. +a, i...) + +julia> Base.@propagate_inbounds Base.setindex!(A::MyArray, v, i...) = setinde +x!(A.a, v, i...) + +julia> Base.IndexStyle(::Type{<:MyArray}) = IndexLinear() +``` + ## [Strided Arrays](@id man-interface-strided-arrays) | Methods to implement |   | Brief description | From 42e9afe54f4d6e8f04bc687fb50136aefeb7af87 Mon Sep 17 00:00:00 2001 From: Stanislav Kazmin Date: Mon, 30 Sep 2019 11:26:12 +0200 Subject: [PATCH 2/4] corrected the example --- doc/src/manual/interfaces.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index 5c853ab018e4d..0e2fcf31db2c9 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -415,11 +415,9 @@ julia> mutable struct MyArray{T,N} <: AbstractArray{T,N} julia> Base.size(A::MyArray) = size(A.a) -julia> Base.@propagate_inbounds Base.getindex(A::MyArray, i...) = getindex(A. -a, i...) +julia> Base.@propagate_inbounds Base.getindex(A::MyArray, i...) = getindex(A.a, i...) -julia> Base.@propagate_inbounds Base.setindex!(A::MyArray, v, i...) = setinde -x!(A.a, v, i...) +julia> Base.@propagate_inbounds Base.setindex!(A::MyArray, v, i...) = setindex!(A.a, v, i...) julia> Base.IndexStyle(::Type{<:MyArray}) = IndexLinear() ``` From 86434bb098553f5ec2ffa2c05a938959bc273063 Mon Sep 17 00:00:00 2001 From: Stanislav Kazmin Date: Mon, 30 Sep 2019 11:32:02 +0200 Subject: [PATCH 3/4] removed trailing whitespaces --- doc/src/manual/interfaces.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index 0e2fcf31db2c9..6530305d4f74a 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -401,11 +401,11 @@ so that the `dims` argument (ordinarily a `Dims` size-tuple) can accept `Abstrac perhaps range-types `Ind` of your own design. For more information, see [Arrays with custom indices](@ref man-custom-indices). -Sometimes you need to wrap a standard array into an extra type to be able to dispatch on this type but without +Sometimes you need to wrap a standard array into an extra type to be able to dispatch on this type but without the need of any real changes to the standard array behavior. -The following example introduces such a wrapper type and ensures that the new type will work at the same +The following example introduces such a wrapper type and ensures that the new type will work at the same speed as a standard array type. -The key point is the `Base.@propagate_inbounds` which allows predefined functions like `sum` +The key point is the `Base.@propagate_inbounds` which allows predefined functions like `sum` to skip bounds checking as they do for a standard array. ```jldoctest wrapperarray From 6e6b1c0c929f20844c846942706d6ff907d8bc94 Mon Sep 17 00:00:00 2001 From: Stanislav Kazmin Date: Mon, 16 Dec 2019 11:15:51 +0100 Subject: [PATCH 4/4] correctd the example text --- doc/src/manual/interfaces.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index 6530305d4f74a..48bae68514189 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -405,8 +405,8 @@ Sometimes you need to wrap a standard array into an extra type to be able to dis the need of any real changes to the standard array behavior. The following example introduces such a wrapper type and ensures that the new type will work at the same speed as a standard array type. -The key point is the `Base.@propagate_inbounds` which allows predefined functions like `sum` -to skip bounds checking as they do for a standard array. +The key point is the usage of `Base.@propagate_inbounds` which allows predefined functions like `sum` +to skip bounds checking as they would do for a standard array. ```jldoctest wrapperarray julia> mutable struct MyArray{T,N} <: AbstractArray{T,N}