Skip to content

Commit 9589e4d

Browse files
authored
Merge pull request #170 from chriselrod/definepointer
Defined pointer method.
2 parents f20ecfc + 6dcded3 commit 9589e4d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.4.0"
3+
version = "1.4.1"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/OffsetArrays.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ 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)
299+
@inline Base.unsafe_convert(::Type{Ptr{T}}, A::OffsetArray{T}) where {T} = Base.unsafe_convert(Ptr{T}, parent(A))
300+
297301
# For fast broadcasting: ref https://discourse.julialang.org/t/why-is-there-a-performance-hit-on-broadcasting-with-offsetarrays/32194
298302
Base.dataids(A::OffsetArray) = Base.dataids(parent(A))
299303
Broadcast.broadcast_unalias(dest::OffsetArray, src::OffsetArray) = parent(dest) === parent(src) ? src : Broadcast.unalias(dest, src)

test/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,4 +1390,26 @@ end
13901390
@test arr == adapt(Array, s_arr)
13911391
end
13921392

1393+
@testset "Pointer" begin
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)
1405+
@test pointer(a) === pointer(a, firstindex(a))
1406+
@test pointer(A) === pointer(A, firstindex(A))
1407+
if VERSION v"1.5"
1408+
@test pointer(a') === pointer(parent(a))
1409+
@test pointer(A') === pointer(parent(A))
1410+
@test pointer(a', 5) === pointer(parent(a), 5)
1411+
@test pointer(A', 15) === pointer(parent(A)', 15)
1412+
end
1413+
end
1414+
13931415
include("origin.jl")

0 commit comments

Comments
 (0)