Skip to content

Commit 45f4aab

Browse files
committed
properly support offset indices
1 parent e2f5726 commit 45f4aab

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/array-lib.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ function Base.getindex(x::SymArray, idx...)
2222
meta = metadata(unwrap(x))
2323
if shape(x) !== Unknown() && all(i -> i isa Integer, idx)
2424
II = CartesianIndices(axes(x))
25+
ii = CartesianIndex(idx)
2526
@boundscheck begin
26-
if !checkbounds(Bool, II, idx...)
27+
if !in(ii, II)
2728
throw(BoundsError(x, idx))
2829
end
2930
end
30-
ii = II[idx...]
3131
res = Term{eltype(symtype(x))}(getindex, [x, Tuple(ii)...]; metadata = meta)
3232
elseif all(i -> symtype(i) <: Integer, idx)
3333
shape(x) !== Unknown() && @boundscheck begin

src/arrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ end
526526
function axes(A::Union{Arr, SymArray})
527527
s = shape(unwrap(A))
528528
s === Unknown() && error("axes of $A not known")
529-
return map(x->1:length(x), s)
529+
return s
530530
end
531531

532532

test/arrays.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,19 @@ end
393393
@test !isequal(a, b) && !isequal(b, c) && !isequal(a, c)
394394
@test hash(a) != hash(b) && hash(b) != hash(c) && hash(a) != hash(c)
395395
end
396+
397+
@testset "Offset Indices" begin
398+
@variables k[0:3]
399+
400+
@testset "i = $i" for i in 0:3
401+
sym = unwrap(k[i])
402+
@test operation(sym) === getindex
403+
args = arguments(sym)
404+
@test length(args) == 2
405+
@test args[1] === unwrap(k)
406+
@test args[2] === i
407+
end
408+
409+
@test_throws BoundsError k[-1]
410+
@test_throws BoundsError k[4]
411+
end

0 commit comments

Comments
 (0)