Skip to content

Commit

Permalink
tweak rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Feb 14, 2020
1 parent 49f7f5c commit 3e2053a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
19 changes: 10 additions & 9 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ######################################################
Expand Down Expand Up @@ -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...)
4 changes: 2 additions & 2 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/dimension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...))...)
Expand Down
3 changes: 2 additions & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
8 changes: 4 additions & 4 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) =
Expand Down
2 changes: 1 addition & 1 deletion src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e2053a

Please sign in to comment.