Skip to content

Commit

Permalink
properly support offset indices
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub authored Oct 8, 2023
1 parent a206b10 commit e9e858c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/array-lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function Base.getindex(x::SymArray, idx...)
meta = metadata(unwrap(x))
if shape(x) !== Unknown() && all(i -> i isa Integer, idx)
II = CartesianIndices(axes(x))
ii = CartesianIndex(idx)
@boundscheck begin
if !checkbounds(Bool, II, idx...)
if !in(ii, II)
throw(BoundsError(x, idx))
end
end
ii = II[idx...]
res = Term{eltype(symtype(x))}(getindex, [x, Tuple(ii)...]; metadata = meta)
elseif all(i -> symtype(i) <: Integer, idx)
shape(x) !== Unknown() && @boundscheck begin
Expand Down
2 changes: 1 addition & 1 deletion src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ end
function axes(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("axes of $A not known")
return map(x->1:length(x), s)
return s
end


Expand Down
16 changes: 16 additions & 0 deletions test/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,19 @@ end
@test !isequal(a, b) && !isequal(b, c) && !isequal(a, c)
@test hash(a) != hash(b) && hash(b) != hash(c) && hash(a) != hash(c)
end

@testset "Offset Indices" begin
@variables k[0:3]

@testset "i = $i" for i in 0:3
sym = unwrap(k[i])
@test operation(sym) === getindex
args = arguments(sym)
@test length(args) == 2
@test args[1] === unwrap(k)
@test args[2] === i
end

@test_throws BoundsError k[-1]
@test_throws BoundsError k[4]
end

0 comments on commit e9e858c

Please sign in to comment.