From 46355351aa351a52a4033132b4e0946721c5db43 Mon Sep 17 00:00:00 2001 From: Michael Abbott Date: Mon, 6 Apr 2020 13:55:50 +0200 Subject: [PATCH 1/3] similar(PermutedDimsArray) --- Project.toml | 2 +- README.md | 3 +++ src/Compat.jl | 5 +++++ test/runtests.jl | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d581e7a4b..555134539 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Compat" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.8.0" +version = "3.9.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/README.md b/README.md index 25abf4760..711132e85 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need. ## Supported features +* `similar(::PermutedDimsArray)` now uses the parent ([#35304]). (since Compat 3.9.0) + * `@NamedTuple` macro for convenient `struct`-like syntax for declaring `NamedTuple` types via `key::Type` declarations ([#34548]). (since Compat 3.8.0) @@ -137,3 +139,4 @@ Note that you should specify the correct minimum version for `Compat` in the [#34548]: https://github.com/JuliaLang/julia/pull/34548 [#34652]: https://github.com/JuliaLang/julia/issues/34652 [#34773]: https://github.com/JuliaLang/julia/issues/34773 +[#35304]: https://github.com/JuliaLang/julia/pull/35304 diff --git a/src/Compat.jl b/src/Compat.jl index 58f020de8..f75116bb8 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -329,6 +329,11 @@ if VERSION < v"1.4.0-DEV.513" export evalpoly end +# https://github.com/JuliaLang/julia/pull/35304 +if VERSION < v"1.5.0-DEV.574" + Base.similar(A::PermutedDimsArray, T::Type, dims::Base.Dims) = similar(parent(A), T, dims) +end + # https://github.com/JuliaLang/julia/pull/34548 if VERSION < v"1.5.0-DEV.314" macro NamedTuple(ex) diff --git a/test/runtests.jl b/test/runtests.jl index b4ce1082b..4e763a481 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -298,6 +298,41 @@ end @test evalpoly(1+im, [2,]) == 2 end +@testset "similar(PermutedDimsArray)" begin + # https://github.com/JuliaLang/julia/pull/35298 + # A custom linear slow sparse-like array that relies upon Dict for its storage + struct TSlow{T,N} <: AbstractArray{T,N} + data::Dict{NTuple{N,Int}, T} + dims::NTuple{N,Int} + end + TSlow(::Type{T}, dims::Int...) where {T} = TSlow(T, dims) + TSlow(::Type{T}, dims::NTuple{N,Int}) where {T,N} = TSlow{T,N}(Dict{NTuple{N,Int}, T}(), dims) + + TSlow{T,N}(X::TSlow{T,N}) where {T,N } = X + TSlow( X::AbstractArray{T,N}) where {T,N } = TSlow{T,N}(X) + TSlow{T }(X::AbstractArray{_,N}) where {T,N,_} = TSlow{T,N}(X) + TSlow{T,N}(X::AbstractArray ) where {T,N } = begin + A = TSlow(T, size(X)) + for I in CartesianIndices(X) + A[Tuple(I)...] = X[Tuple(I)...] + end + A + end + + Base.size(A::TSlow) = A.dims + Base.similar(A::TSlow, ::Type{T}, dims::Dims) where {T} = TSlow(T, dims) + Base.IndexStyle(::Type{A}) where {A<:TSlow} = IndexCartesian() + Base.getindex(A::TSlow{T,N}, i::Vararg{Int,N}) where {T,N} = get(A.data, i, zero(T)) + Base.setindex!(A::TSlow{T,N}, v, i::Vararg{Int,N}) where {T,N} = (A.data[i] = v) + + # https://github.com/JuliaLang/julia/pull/35304 + x = PermutedDimsArray([1 2; 3 4], (2, 1)) + @test similar(x, 3,3) isa Array + z = TSlow([1 2; 3 4]) + x_slow = PermutedDimsArray(z, (2, 1)) + @test similar(x_slow, 3,3) isa TSlow +end + # https://github.com/JuliaLang/julia/pull/34548 @testset "@NamedTuple" begin @test (@NamedTuple {a::Int, b::String}) === NamedTuple{(:a, :b),Tuple{Int,String}} === From 113d8a33a42bb497cebfb4ad6101760bb40e0015 Mon Sep 17 00:00:00 2001 From: Michael Abbott Date: Mon, 6 Apr 2020 14:38:24 +0200 Subject: [PATCH 2/3] struct outside testset --- test/runtests.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4e763a481..93f387714 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -298,9 +298,9 @@ end @test evalpoly(1+im, [2,]) == 2 end -@testset "similar(PermutedDimsArray)" begin - # https://github.com/JuliaLang/julia/pull/35298 - # A custom linear slow sparse-like array that relies upon Dict for its storage +# https://github.com/JuliaLang/julia/pull/35298 +begin + # A custom linear slow sparse-like array that relies upon Dict for its storage struct TSlow{T,N} <: AbstractArray{T,N} data::Dict{NTuple{N,Int}, T} dims::NTuple{N,Int} @@ -318,14 +318,15 @@ end end A end - Base.size(A::TSlow) = A.dims Base.similar(A::TSlow, ::Type{T}, dims::Dims) where {T} = TSlow(T, dims) Base.IndexStyle(::Type{A}) where {A<:TSlow} = IndexCartesian() Base.getindex(A::TSlow{T,N}, i::Vararg{Int,N}) where {T,N} = get(A.data, i, zero(T)) Base.setindex!(A::TSlow{T,N}, v, i::Vararg{Int,N}) where {T,N} = (A.data[i] = v) +end - # https://github.com/JuliaLang/julia/pull/35304 +# https://github.com/JuliaLang/julia/pull/35304 +@testset "similar(PermutedDimsArray)" begin x = PermutedDimsArray([1 2; 3 4], (2, 1)) @test similar(x, 3,3) isa Array z = TSlow([1 2; 3 4]) From 57e8cbcac0a771a6cf58567b07d8fa203d662992 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Thu, 30 Apr 2020 07:38:56 +0200 Subject: [PATCH 3/3] Remove spurious blank line README.md introduced during conflict resolution --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 8223b9f99..6b4bd4206 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ Please check the list below for the specific syntax you need. ## Supported features - * `similar(::PermutedDimsArray)` now uses the parent ([#35304]). (since Compat 3.9.0) * `isdisjoint(l, r)` indicates whether two collections are disjoint ([#34427]). (since Compat 3.9.0)