From 3e2053accaccf4035fa1ec93c29c629cb6140b3e Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Fri, 14 Feb 2020 23:34:13 +1100 Subject: [PATCH] tweak rebuild --- src/array.jl | 19 ++++++++++--------- src/broadcast.jl | 4 ++-- src/dimension.jl | 4 ++-- src/interface.jl | 3 ++- src/methods.jl | 8 ++++---- src/primitives.jl | 2 +- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/array.jl b/src/array.jl index 52fa18b53..c5e18d9f0 100644 --- a/src/array.jl +++ b/src/array.jl @@ -8,9 +8,11 @@ const StandardIndices = Union{AbstractArray,Colon,Integer} # Interface methods ############################################################ dims(A::AbDimArray) = A.dims -@inline rebuild(x, data, dims=dims(x)) = rebuild(x, data, dims, refdims(x)) -@inline rebuild(x::AbDimArray, data, dims=dims(x)) = - rebuild(x, data, dims, refdims(x)) +@inline rebuild(A::AbstractArray, data, dims::Tuple=dims(A), refdims=refdims(A)) = + rebuild(A, data, dims, refdims, name(A)) +# Rebuild for name-updating methods, to avoid having to add dims and refdims +@inline rebuild(A::AbstractArray, data, name::String) = + rebuild(A, data, dims(A), refdims(A), name) # Array interface methods ###################################################### @@ -105,15 +107,14 @@ DimensionalArray(A::AbstractArray, dims, name::String = ""; refdims=()) = # Getters refdims(A::DimensionalArray) = A.refdims data(A::DimensionalArray) = A.data -label(A::DimensionalArray) = A.name +name(A::DimensionalArray) = A.name +label(A::DimensionalArray) = name(A) # DimensionalArray interface -@inline rebuild(A::DimensionalArray, data, dims::Tuple, refdims::Tuple, name::String = A.name) = +@inline rebuild(A::DimensionalArray, data::AbstractArray, dims::Tuple, + refdims::Tuple, name::String) = DimensionalArray(data, dims, refdims, name) -@inline rebuild(A::DimensionalArray, data, dims::Tuple, name::String = A.name; refdims = refdims(A)) = - DimensionalArray(data, dims, refdims, name) -@inline rebuild(A::DimensionalArray, data::AbstractArray, name::String) = - DimensionalArray(data, dims(A), refdims(A), name) + # Array interface (AbstractDimensionalArray takes care of everything else) Base.@propagate_inbounds Base.setindex!(A::DimensionalArray, x, I::Vararg{StandardIndices}) = setindex!(data(A), x, I...) diff --git a/src/broadcast.jl b/src/broadcast.jl index 75164b951..055f185cc 100644 --- a/src/broadcast.jl +++ b/src/broadcast.jl @@ -42,7 +42,7 @@ function Broadcast.copy(bc::Broadcasted{DimensionalStyle{S}}) where S return if A isa Nothing || _dims isa Nothing data else - rebuild(A, data, _dims, "") + rebuild(A, data, _dims, refdims(A), "") end end @@ -53,7 +53,7 @@ function Base.copyto!(dest::AbstractArray, bc::Broadcasted{DimensionalStyle{S}}) return if A isa Nothing || _dims isa Nothing dest else - rebuild(A, data(dest), _dims, "") + rebuild(A, data(dest), _dims, refdims(A), "") end end diff --git a/src/dimension.jl b/src/dimension.jl index a517a5acf..56eebe68f 100644 --- a/src/dimension.jl +++ b/src/dimension.jl @@ -74,8 +74,8 @@ Base.:(==)(dim1::AbDim, dim2::AbDim) = # AbstractArray methods where dims are the dispatch argument -@inline rebuildsliced(A, data, I, name::String = A.name) = - rebuild(A, data, slicedims(A, I)...,name) +@inline rebuildsliced(A, data, I, name::String=name(A)) = + rebuild(A, data, slicedims(A, I)..., name) Base.@propagate_inbounds Base.getindex(A::AbstractArray, dim::AbDim, dims::Vararg{<:AbDim}) = getindex(A, dims2indices(A, (dim, dims...))...) diff --git a/src/interface.jl b/src/interface.jl index 40f0146c0..5760990cd 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -41,7 +41,8 @@ as it is mostly to give context to plots. Ignoring refdims will simply leave som function refdims end refdims(x) = () """ - rebuild(x::AbstractDimensionalArray, data, [dims], [refdims]) + rebuild(x::AbstractDimensionalArray, data, [dims], [refdims], [name]) + rebuild(x::AbstractDimensionalArray, data, [name]) rebuild(x::AbstractDimension, val, [grid], [metadata]) rebuild(x; kwargs...) diff --git a/src/methods.jl b/src/methods.jl index 359ba4c1d..aa163569b 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -73,7 +73,7 @@ Base._dropdims(A::AbstractArray, dims::AbDimTuple) = # Function application -@inline Base.map(f, A::AbDimArray) = rebuild(A, map(f, data(A)), dims(A)) +@inline Base.map(f, A::AbDimArray) = rebuild(A, map(f, data(A))) Base.mapslices(f, A::AbDimArray; dims=1, kwargs...) = begin dimnums = dimnum(A, dims) @@ -141,7 +141,7 @@ _checkmatch(a, b) = newdims = reversearray(DimensionalData.dims(A), dnum) # Reverse the data newdata = reverse(data(A); dims=dnum) - rebuild(A, newdata, newdims, refdims(A)) + rebuild(A, newdata, newdims) end @inline reversearray(dimstorev::Tuple, dnum) = begin @@ -160,7 +160,7 @@ for (pkg, fname) in [(:Base, :permutedims), (:Base, :adjoint), (:Base, :transpose), (:LinearAlgebra, :Transpose)] @eval begin @inline $pkg.$fname(A::AbDimArray{T,2}) where T = - rebuild(A, $pkg.$fname(data(A)), reverse(dims(A)), refdims(A)) + rebuild(A, $pkg.$fname(data(A)), reverse(dims(A))) @inline $pkg.$fname(A::AbDimArray{T,1}) where T = rebuild(A, $pkg.$fname(data(A)), (EmptyDim(), dims(A)...)) end @@ -197,7 +197,7 @@ Base._cat(catdims::AllDimensions, As::AbstractArray...) = begin newdims = (dims(A1)..., add_dims...) end newA = Base._cat(dnum, map(data, As)...) - rebuild(A1; data=newA, dims=formatdims(newA, newdims)) + rebuild(A1, newA, formatdims(newA, newdims)) end _catifcatdim(catdims::Tuple, ds) = diff --git a/src/primitives.jl b/src/primitives.jl index e68ca2d4f..f9c6b6ec3 100644 --- a/src/primitives.jl +++ b/src/primitives.jl @@ -187,7 +187,7 @@ or a tuple of dimensions. Replaces the first dim matching newdim, with newdim, and returns a new object or tuple with the dimension updated. """ -setdim(A, newdim::AbDim) = rebuild(A; dims=setdim(dims(A), newdim)) +setdim(A, newdim::AbDim) = rebuild(A, data(A), setdim(dims(A), newdim)) setdim(dims::AbDimTuple, newdim::AbDim) = map(d -> setdim(d, newdim), dims) setdim(dim::AbDim, newdim::AbDim) = basetypeof(dim) <: basetypeof(newdim) ? newdim : dim