Skip to content

Commit 7209cc9

Browse files
authored
similar(::PermutedDimsArray) (#693)
1 parent f27d92d commit 7209cc9

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.9.0-DEV"
3+
version = "3.9.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need.
4444

4545
## Supported features
4646

47+
* `similar(::PermutedDimsArray)` now uses the parent ([#35304]). (since Compat 3.9.0)
48+
4749
* `isdisjoint(l, r)` indicates whether two collections are disjoint ([#34427]). (since Compat 3.9.0)
4850

4951
* `mergewith(combine, dicts...)` and `mergewith!(combine, dicts...)` are like
@@ -145,3 +147,4 @@ Note that you should specify the correct minimum version for `Compat` in the
145147
[#34548]: https://github.com/JuliaLang/julia/issues/34548
146148
[#34652]: https://github.com/JuliaLang/julia/issues/34652
147149
[#34773]: https://github.com/JuliaLang/julia/issues/34773
150+
[#35304]: https://github.com/JuliaLang/julia/pull/35304

src/Compat.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ if VERSION < v"1.4.0-DEV.513"
329329
export evalpoly
330330
end
331331

332+
# https://github.com/JuliaLang/julia/pull/35304
333+
if VERSION < v"1.5.0-DEV.574"
334+
Base.similar(A::PermutedDimsArray, T::Type, dims::Base.Dims) = similar(parent(A), T, dims)
335+
end
336+
332337
# https://github.com/JuliaLang/julia/pull/34548
333338
if VERSION < v"1.5.0-DEV.314"
334339
macro NamedTuple(ex)

test/runtests.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,42 @@ end
298298
@test evalpoly(1+im, [2,]) == 2
299299
end
300300

301+
# https://github.com/JuliaLang/julia/pull/35298
302+
begin
303+
# A custom linear slow sparse-like array that relies upon Dict for its storage
304+
struct TSlow{T,N} <: AbstractArray{T,N}
305+
data::Dict{NTuple{N,Int}, T}
306+
dims::NTuple{N,Int}
307+
end
308+
TSlow(::Type{T}, dims::Int...) where {T} = TSlow(T, dims)
309+
TSlow(::Type{T}, dims::NTuple{N,Int}) where {T,N} = TSlow{T,N}(Dict{NTuple{N,Int}, T}(), dims)
310+
311+
TSlow{T,N}(X::TSlow{T,N}) where {T,N } = X
312+
TSlow( X::AbstractArray{T,N}) where {T,N } = TSlow{T,N}(X)
313+
TSlow{T }(X::AbstractArray{_,N}) where {T,N,_} = TSlow{T,N}(X)
314+
TSlow{T,N}(X::AbstractArray ) where {T,N } = begin
315+
A = TSlow(T, size(X))
316+
for I in CartesianIndices(X)
317+
A[Tuple(I)...] = X[Tuple(I)...]
318+
end
319+
A
320+
end
321+
Base.size(A::TSlow) = A.dims
322+
Base.similar(A::TSlow, ::Type{T}, dims::Dims) where {T} = TSlow(T, dims)
323+
Base.IndexStyle(::Type{A}) where {A<:TSlow} = IndexCartesian()
324+
Base.getindex(A::TSlow{T,N}, i::Vararg{Int,N}) where {T,N} = get(A.data, i, zero(T))
325+
Base.setindex!(A::TSlow{T,N}, v, i::Vararg{Int,N}) where {T,N} = (A.data[i] = v)
326+
end
327+
328+
# https://github.com/JuliaLang/julia/pull/35304
329+
@testset "similar(PermutedDimsArray)" begin
330+
x = PermutedDimsArray([1 2; 3 4], (2, 1))
331+
@test similar(x, 3,3) isa Array
332+
z = TSlow([1 2; 3 4])
333+
x_slow = PermutedDimsArray(z, (2, 1))
334+
@test similar(x_slow, 3,3) isa TSlow
335+
end
336+
301337
# https://github.com/JuliaLang/julia/pull/34548
302338
@testset "@NamedTuple" begin
303339
@test (@NamedTuple {a::Int, b::String}) === NamedTuple{(:a, :b),Tuple{Int,String}} ===

0 commit comments

Comments
 (0)