Skip to content

Commit 1c62740

Browse files
committed
Added support & tests for pointer(::OffsetArray, index::Integer).
1 parent 6b3405b commit 1c62740

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/OffsetArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ end
294294
A
295295
end
296296

297+
Base.strides(A::OffsetArray) = strides(parent(A))
298+
Base.elsize(::Type{OffsetArray{T,N,A}}) where {T,N,A} = Base.elsize(A)
297299
@inline Base.unsafe_convert(::Type{Ptr{T}}, A::OffsetArray{T}) where {T} = Base.unsafe_convert(Ptr{T}, parent(A))
298300

299301
# For fast broadcasting: ref https://discourse.julialang.org/t/why-is-there-a-performance-hit-on-broadcasting-with-offsetarrays/32194

test/runtests.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,22 @@ end
13911391
end
13921392

13931393
@testset "Pointer" begin
1394-
A = rand(4, 5)
1395-
O = OffsetArray(@view(A[1:3,1:2:5]), -1:1, -1:1)
1396-
@test pointer(A) === pointer(O)
1394+
a = OffsetVector(collect(10:20), 9);
1395+
@test 12 == a[12] == unsafe_load(pointer(a), 12 + (1 - firstindex(a))) == unsafe_load(pointer(a, 12))
1396+
1397+
A = OffsetArray(reshape(collect(10:130), (11,11)), 9, 9);
1398+
@test 21 == A[12] == unsafe_load(pointer(A), 12) == unsafe_load(pointer(A, 12))
1399+
@test 61 == A[52] == unsafe_load(pointer(A), 52) == unsafe_load(pointer(A, 52))
1400+
1401+
@test pointer(a) === pointer(parent(a))
1402+
@test pointer(A) === pointer(parent(A))
1403+
@test pointer(a, 12) === pointer(parent(a), 12 + (1 - firstindex(a)))
1404+
@test pointer(A, 12) === pointer(parent(A), 12)
13971405
if VERSION v"1.5"
1398-
@test pointer(A) === pointer(O')
1406+
@test pointer(a') === pointer(parent(a))
1407+
@test pointer(A') === pointer(parent(A))
1408+
@test pointer(a', 5) === pointer(parent(a), 5)
1409+
@test pointer(A', 15) === pointer(parent(A)', 15)
13991410
end
14001411
end
14011412

0 commit comments

Comments
 (0)