Skip to content

Commit

Permalink
fix reverse arguements
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Feb 18, 2024
1 parent 2b09950 commit 48ba993
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ macro implement_array_methods(t)
return $_copyto!(dest, src)
end

Base.reverse(a::$t, dims=:) = $_reverse(a, dims)
# For ambiguity
Base.reverse(a::$t{<:Any,1}, dims::Integer) = $_reverse(a, dims)
Base.reverse(a::$t; dims=:) = $_reverse(a, dims)
Base.reverse(a::$t{<:Any,1}) = $_reverse1(a)
Base.reverse(a::$t{<:Any,1}, start::Integer, stop::Integer=lastindex(a)) = $_reverse1(a, start, stop)

# Here we extend the unexported `_replace` method, but we replicate
# much less Base functionality by extending it rather than `replace`.
Expand Down Expand Up @@ -64,7 +64,7 @@ end
_copyto!(dest, Rdest, src, Rsrc) = view(dest, Rdest) .= view(src, Rsrc)

# Use a view for lazy reverse
_reverse(a, dims::Colon) = _reverse(a, ntuple(identity, ndims(a)))
_reverse(a, ::Colon) = _reverse(a, ntuple(identity, ndims(a)))
_reverse(a, dims::Int) = _reverse(a, (dims,))
function _reverse(A, dims::Tuple)
rev_axes = map(ntuple(identity, ndims(A)), axes(A)) do d, a
Expand All @@ -73,6 +73,12 @@ function _reverse(A, dims::Tuple)
end
return view(A, rev_axes...)
end
_reverse1(a) = _reverse(a, 1)
function _reverse1(a, start::Int, stop::Int)
inds = [firstindex(a):start-1; stop:-1:start; stop+1:lastindex(a)]
@show inds
return view(a, inds)
end

# Use broadcast instead of a loop.
# The `count` argument is disallowed as broadcast is not sequential.
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,11 @@ end
@test collect(reverse(a_disk)) == reverse(a)
@test reverse(view(a_disk, :, 1)) == reverse(a[:, 1])
@test reverse(view(a_disk, :, 1), 1) == reverse(a[:, 1], 1)
@test collect(reverse(a_disk)) == reverse(a)
# ERROR: ArgumentError: Can only subset chunks for sorted indices
@test_broken reverse(view(a_disk, :, 1), 5) == reverse(a[:, 1], 5)
@test_broken reverse(view(a_disk, :, 1), 5, 10) == reverse(a[:, 1], 5, 10)
@test collect(reverse(a_disk)) == collect(reverse(a_disk; dims=:)) ==
collect(reverse(a_disk; dims=(1, 2))) == reverse(a)
@test collect(reverse(a_disk; dims=2)) == reverse(a; dims=2)
@test replace(a_disk, 1 => 2) == replace(a, 1 => 2)
@test rotr90(a_disk) == rotr90(a)
Expand Down

0 comments on commit 48ba993

Please sign in to comment.