Skip to content

Commit

Permalink
similar(::PermutedDimsArray) (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored Apr 30, 2020
1 parent f27d92d commit 7209cc9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.9.0-DEV"
version = "3.9.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

* `isdisjoint(l, r)` indicates whether two collections are disjoint ([#34427]). (since Compat 3.9.0)

* `mergewith(combine, dicts...)` and `mergewith!(combine, dicts...)` are like
Expand Down Expand Up @@ -145,3 +147,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#34548]: https://github.com/JuliaLang/julia/issues/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
5 changes: 5 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,42 @@ end
@test evalpoly(1+im, [2,]) == 2
end

# 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}
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)
end

# 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])
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}} ===
Expand Down

2 comments on commit 7209cc9

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/13930

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.9.0 -m "<description of version>" 7209cc9c0f9108a2c05b0d3ff2e60aeb8f40f20e
git push origin v3.9.0

Please sign in to comment.