diff --git a/.github/workflows/Documenter.yml b/.github/workflows/Documenter.yml index a482cfaf2..160132647 100644 --- a/.github/workflows/Documenter.yml +++ b/.github/workflows/Documenter.yml @@ -32,4 +32,4 @@ jobs: DATADEPS_ALWAYS_ACCEPT: true run: | julia --code-coverage=user --project=docs/ --color=yes docs/genfiles.jl - julia --code-coverage=user --project=docs/ --color=yes docs/make.jl \ No newline at end of file + julia --code-coverage=user --project=docs/ --color=yes docs/make.jl diff --git a/src/utils.jl b/src/utils.jl index c0016aa77..af16858dd 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,17 +1,34 @@ """ - reorder(A::AbstractDimArray, order::Pair) => AbstractDimArray - reorder(A::Dimension, order::Order) => AbstractDimArray + reorder(A::Union{AbstractDimArray,AbstractDimStack}, order::Pair...) + reorder(A::Union{AbstractDimArray,AbstractDimStack}, order) + reorder(A::Dimension, order::Order) Reorder every dims index/array to `order`, or reorder index for -the the given dimension(s) to the `Order` they wrap. +the the given dimension(s) in `order`. -`order` can be an [`Order`](@ref), or `Dimeension => Order` pairs. +`order` can be an [`Order`](@ref), `Dimension => Order` pairs. +A Tuple of Dimensions or any object that defines `dims` can be used +in which case dimensions are If no axis reversal is required the same objects will be returned, without allocation. + +## Example + +```jldoctest +# Create a DimArray +da = DimArray([1 2 3; 4 5 6], (X(10:10:20), Y(300:-100:100))) +# Reverse it +rev = reverse(da, dims=Y) +# using `da` in reorder will return it to the original order +reorder(rev, da) == da +# output +true """ function reorder end +reorder(x, A::Union{AbstractDimArray,AbstractDimStack,AbstractDimIndices}) = reorder(x, dims(A)) +reorder(x, ::Nothing) = throw(ArgumentError("object has no dimensions")) reorder(x, p::Pair, ps::Vararg{Pair}) = reorder(x, (p, ps...)) reorder(x, ps::Tuple{Vararg{Pair}}) = reorder(x, Dimensions.pairdims(ps...)) # Reorder specific dims. @@ -27,6 +44,7 @@ _reorder(x, orderdims::DimTuple) = _reorder(reorder(x, orderdims[1]), tail(order _reorder(x, orderdims::Tuple{}) = x reorder(x, orderdim::Dimension) = _reorder(val(orderdim), x, dims(x, orderdim)) +reorder(x, orderdim::Dimension{<:LookupArray}) = _reorder(order(orderdim), x, dims(x, orderdim)) _reorder(neworder::Order, x, dim::Dimension) = _reorder(basetypeof(neworder), x, dim) # Reverse the dimension index diff --git a/test/utils.jl b/test/utils.jl index f713a9937..33d2b75ac 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -75,6 +75,19 @@ end d = reorder(dims(da, Y), ForwardOrdered()) @test order(d) isa ForwardOrdered @test index(d) == 100:100:300 + + # reorder with dimension lookups + rev = reverse(da, dims=Y) + reo = reorder(rev, da) + @test rev != da + @test reo == da + @test dims(reo) == dims(da) + @test_throws MethodError reorder(rev, :test) + rev_s = reverse(s, dims=Y) + reo_s = reorder(rev_s, da) + @test rev_s != s + @test reo_s == s + @test dims(reo_s) == dims(s) end @testset "modify" begin