Skip to content

Commit

Permalink
Implement Base.axes for MacheteOrder arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed May 16, 2022
1 parent 8e41eef commit f1e687f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/StarWarsArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ Base.setindex!(A::StarWarsArray, v, i::Int) =
Base.setindex!(A::StarWarsArray{T,N}, v, i::Vararg{Int,N}) where {T,N} =
setindex!(parent(A), v, index.(i, size(parent(A)), order(A))...)

Base.axes(A::StarWarsArray{T,N,P,MacheteOrder}) where {T,N,P} =
map(i->index.(i .+ 1, length(A), MacheteOrder), machete_view_index.(size(A)))

# Showing. Note: this is awful, but it does what I want
Base.show(io::IO, m::MIME"text/plain", A::StarWarsArray{T,N,P,MacheteOrder}) where {T,N,P} =
show(io, m,
view(parent(A),
map(i->StarWarsArrays.index.(i .+ 1, length(A), MacheteOrder),
StarWarsArrays.machete_view_index.(size(A)))...))
show(io, m, view(parent(A), axes(A)...))

end # module
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ end
@test_throws BoundsError v[0]
v[4] = -42
@test v[4] == -42
@test axes(v) == ([3, 4, 1, 2, 5, 6, 7, 8],)

m = StarWarsArray(reshape(collect(1:81), 9, 9), MacheteOrder)
@test order(m) == MacheteOrder
Expand All @@ -60,6 +61,7 @@ end
@test m[4] == -42
m[2] = 0
@test m[2,4] == 0
@test axes(m) == ([3, 4, 1, 2, 5, 6, 7, 8], [3, 4, 1, 2, 5, 6, 7, 8])

@test sprint(showerror, StarWarsArrays.StarWarsError(1, MacheteOrder)) ==
"StarWarsError: there is no episode 1 in MacheteOrder"
Expand Down

0 comments on commit f1e687f

Please sign in to comment.