Skip to content

Commit f9edfe7

Browse files
committed
handle offset indices
1 parent a9ca954 commit f9edfe7

File tree

2 files changed

+74
-41
lines changed

2 files changed

+74
-41
lines changed

base/subarray.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ end
310310
function getindex(V::FastContiguousSubArray, i::AbstractUnitRange{Int})
311311
@inline
312312
@boundscheck checkbounds(V, i)
313-
@inbounds V.parent[V.offset1 .+ i]
313+
out = similar(V, axes(i))
314+
li = length(i)
315+
if li > 0
316+
copyto!(out, firstindex(out), V.parent, V.offset1 + first(i), li)
317+
end
318+
return out
314319
end
315320

316321
# For vector views with linear indexing, we disambiguate to favor the stride/offset
@@ -330,7 +335,13 @@ end
330335
function getindex(V::FastContiguousSubArray{<:Any, 1}, i::AbstractUnitRange{Int})
331336
@inline
332337
@boundscheck checkbounds(V, i)
333-
@inbounds V.parent[V.offset1 .+ i]
338+
v = @view V.parent[V.offset1 .+ UnitRange(i)]
339+
out = similar(V, axes(i))
340+
li = length(i)
341+
if li > 0
342+
copyto!(out, firstindex(out), V.parent, V.offset1 + first(i), li)
343+
end
344+
return out
334345
end
335346
@inline getindex(V::FastContiguousSubArray, i::Colon) = getindex(V, to_indices(V, (:,))...)
336347

@@ -356,7 +367,10 @@ end
356367
function setindex!(V::FastContiguousSubArray, x, i::AbstractUnitRange{Int})
357368
@inline
358369
@boundscheck checkbounds(V, i)
359-
@inbounds V.parent[V.offset1 .+ i] = x
370+
li = length(i)
371+
if li > 0
372+
copyto!(V.parent, V.offset1 + first(i), x, firstindex(x), li)
373+
end
360374
V
361375
end
362376

@@ -381,7 +395,10 @@ end
381395
function setindex!(V::FastContiguousSubArray{<:Any, 1}, x, i::AbstractUnitRange{Int})
382396
@inline
383397
@boundscheck checkbounds(V, i)
384-
@inbounds V.parent[V.offset1 .+ i] = x
398+
li = length(i)
399+
if li > 0
400+
copyto!(V.parent, V.offset1 + first(i), x, firstindex(x), li)
401+
end
385402
V
386403
end
387404
@inline setindex!(V::FastSubArray, x, i::Colon) = setindex!(V, x, to_indices(V, (i,))...)

test/subarray.jl

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ end
463463
c1 = @view a1[inds1]
464464
@test c1[axes(c1,1)] == c1[:] == a1[inds1]
465465

466+
inds12 = Base.IdentityUnitRange(Base.OneTo(4))
467+
c1 = @view a1[inds12]
468+
@test c1[axes(c1,1)] == c1[:] == a1[inds12]
469+
466470
inds2 = 3:2:5
467471
d1 = @view a1[inds2]
468472
@test d1[axes(d1,1)] == d1[:] == a1[inds2]
@@ -486,50 +490,62 @@ end
486490
c2 = @view a2[inds1]
487491
@test c2[axes(c2,1)] == c2[:] == a2[inds1]
488492

493+
inds12 = Base.IdentityUnitRange(Base.OneTo(4))
494+
c2 = @view a2[inds12]
495+
@test c2[axes(c2,1)] == c2[:] == a2[inds12]
496+
489497
inds2 = 2:2:4
490498
d2 = @view a2[inds2];
491499
@test d2[axes(d2,1)] == d2[:] == a2[inds2]
492500
end
493501
end
494502
end
495503
@testset "setindex!" begin
496-
a1 = rand(10);
497-
a12 = copy(a1);
498-
b1 = @view a1[:]; # 1D FastContiguousSubArray
499-
c1 = @view a1[eachindex(a1)]; # 1D FastContiguousSubArray
500-
d1 = @view a1[begin:1:end]; # 1D FastSubArray
501-
502-
ax1 = eachindex(a1);
503-
@test (b1[ax1] = a12; b1) == (c1[ax1] = a12; c1) == (d1[ax1] = a12; d1) == (a1[ax1] = a12; a1)
504-
@test (b1[:] = a12; b1) == (c1[:] = a12; c1) == (d1[:] = a12; d1) == (a1[:] = a12; a1)
505-
506-
# some arbitary indices
507-
ind1 = 2:4
508-
c1 = a12[ind1]
509-
@test (c1[axes(c1,1)] = a12[ind1]; c1) == (c1[:] = a12[ind1]; c1) == a12[ind1]
510-
511-
ind2 = 2:2:8
512-
d1 = a12[ind2]
513-
@test (d1[axes(d1,1)] = a12[ind2]; d1) == (d1[:] = a12[ind2]; d1) == a12[ind2]
514-
515-
a2 = rand(10, 10);
516-
a22 = copy(a2);
517-
a2v = vec(a22);
518-
b2 = @view a2[:, :]; # 2D FastContiguousSubArray
519-
c2 = @view a2[eachindex(a2)]; # 1D FastContiguousSubArray
520-
d2 = @view a2[begin:1:end]; # 1D FastSubArray
521-
522-
@test (b2[eachindex(b2)] = a2v; vec(b2)) == (c2[eachindex(c2)] = a2v; c2) == a2v
523-
@test (d2[eachindex(d2)] = a2v; d2) == a2v
524-
525-
# some arbitary indices
526-
inds1 = 3:9
527-
c2 = @view a2[inds1]
528-
@test (c2[eachindex(c2)] = @view(a22[inds1]); c2) == @view(a22[inds1])
529-
530-
inds2 = 3:3:9
531-
d2 = @view a2[inds2]
532-
@test (d2[eachindex(d2)] = @view(a22[inds2]); d2) == @view(a22[inds2])
504+
@testset "1D" begin
505+
a1 = rand(10);
506+
a12 = copy(a1);
507+
b1 = @view a1[:]; # 1D FastContiguousSubArray
508+
c1 = @view a1[eachindex(a1)]; # 1D FastContiguousSubArray
509+
d1 = @view a1[begin:1:end]; # 1D FastSubArray
510+
511+
ax1 = eachindex(a1);
512+
@test (b1[ax1] = a12; b1) == (c1[ax1] = a12; c1) == (d1[ax1] = a12; d1) == (a1[ax1] = a12; a1)
513+
@test (b1[:] = a12; b1) == (c1[:] = a12; c1) == (d1[:] = a12; d1) == (a1[:] = a12; a1)
514+
515+
# some arbitary indices
516+
ind1 = 2:4
517+
c1 = a12[ind1]
518+
@test (c1[axes(c1,1)] = a12[ind1]; c1) == (c1[:] = a12[ind1]; c1) == a12[ind1]
519+
520+
ind2 = 2:2:8
521+
d1 = a12[ind2]
522+
@test (d1[axes(d1,1)] = a12[ind2]; d1) == (d1[:] = a12[ind2]; d1) == a12[ind2]
523+
end
524+
525+
@testset "2D" begin
526+
a2 = rand(10, 10);
527+
a22 = copy(a2);
528+
a2v = vec(a22);
529+
b2 = @view a2[:, :]; # 2D FastContiguousSubArray
530+
c2 = @view a2[eachindex(a2)]; # 1D FastContiguousSubArray
531+
d2 = @view a2[begin:1:end]; # 1D FastSubArray
532+
533+
@test (b2[eachindex(b2)] = a2v; vec(b2)) == (c2[eachindex(c2)] = a2v; c2) == a2v
534+
@test (d2[eachindex(d2)] = a2v; d2) == a2v
535+
536+
# some arbitary indices
537+
inds1 = 3:9
538+
c2 = @view a2[inds1]
539+
@test (c2[eachindex(c2)] = @view(a22[inds1]); c2) == @view(a22[inds1])
540+
541+
inds1 = Base.IdentityUnitRange(Base.OneTo(4))
542+
c2 = @view a2[inds1]
543+
@test (c2[eachindex(c2)] = @view(a22[inds1]); c2) == @view(a22[inds1])
544+
545+
inds2 = 3:3:9
546+
d2 = @view a2[inds2]
547+
@test (d2[eachindex(d2)] = @view(a22[inds2]); d2) == @view(a22[inds2])
548+
end
533549
end
534550
end
535551

0 commit comments

Comments
 (0)