@@ -1414,5 +1414,29 @@ end
1414
1414
end
1415
1415
1416
1416
@testset " reshape for offset arrays" begin
1417
- @test_throws ArgumentError reshape (Base. IdentityUnitRange (0 : 1 ), (2 ,1 ))
1417
+ r = reshape (Base. IdentityUnitRange (0 : 1 ), (2 ,1 ))
1418
+ @test r[eachindex (r)] == [0 : 1 ;]
1419
+
1420
+ struct ZeroBasedArray{T,N,A<: AbstractArray{T,N} } <: AbstractArray{T,N}
1421
+ a :: A
1422
+ function ZeroBasedArray (a:: AbstractArray )
1423
+ Base. require_one_based_indexing (a)
1424
+ new {eltype(a), ndims(a), typeof(a)} (a)
1425
+ end
1426
+ end
1427
+ Base. parent (z:: ZeroBasedArray ) = z. a
1428
+ Base. size (z:: ZeroBasedArray ) = size (parent (z))
1429
+ Base. axes (z:: ZeroBasedArray ) = map (x -> Base. IdentityUnitRange (0 : x - 1 ), size (parent (z)))
1430
+ Base. getindex (z:: ZeroBasedArray{<:Any, N} , i:: Vararg{Int,N} ) where {N} = parent (z)[map (x -> x + 1 , i)... ]
1431
+ Base. setindex! (z:: ZeroBasedArray{<:Any, N} , val, i:: Vararg{Int,N} ) where {N} = parent (z)[map (x -> x + 1 , i)... ] = val
1432
+
1433
+ z = ZeroBasedArray (collect (1 : 4 ))
1434
+ r2 = reshape (z, (4 , 1 ))
1435
+ @test r2[CartesianIndices (r2)] == r2[LinearIndices (r2)]
1436
+ r2[firstindex (r2)] = 34
1437
+ @test z[0 ] == 34
1438
+ r2[eachindex (r2)] = r2 .* 2
1439
+ for (i, j) in zip (eachindex (r2), eachindex (z))
1440
+ @test r2[i] == z[j]
1441
+ end
1418
1442
end
0 commit comments