Skip to content

Commit 7d3dd99

Browse files
authored
Use Base.memoryindex in Julia 1.13 for parentindices (#31)
1 parent cefb989 commit 7d3dd99

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/basic.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@ function Base.iterate(x::MemoryView, i::Int = 1)
2323
return (@inbounds x[i], i + 1)
2424
end
2525

26-
# Note: For zero-sized elements, this always returns 1:x.len, which may not be
27-
# the correct indices. However, the result is indistinguishable from the "correct"
28-
# result, so it doesn't matter
29-
function Base.parentindices(x::MemoryView)
30-
elz = Base.elsize(x)
31-
return if iszero(elz)
32-
(1:(x.len),)
33-
else
34-
byte_offset = pointer(x.ref) - pointer(x.ref.mem)
35-
elem_offset = div(byte_offset % UInt, elz % UInt) % Int
36-
((elem_offset + 1):(elem_offset + x.len),)
26+
# Base.memoryindex exists in Julia 1.13 onwards.
27+
@static if VERSION < v"1.13"
28+
# Note: For zero-sized elements, this always returns 1:x.len, which may not be
29+
# the correct indices. However, the result is indistinguishable from the "correct"
30+
# result, so it doesn't matter
31+
function Base.parentindices(x::MemoryView)
32+
elz = Base.elsize(x)
33+
return if iszero(elz)
34+
(1:(x.len),)
35+
else
36+
byte_offset = pointer(x.ref) - pointer(x.ref.mem)
37+
elem_offset = div(byte_offset % UInt, elz % UInt) % Int
38+
((elem_offset + 1):(elem_offset + x.len),)
39+
end
40+
end
41+
else
42+
function Base.parentindices(x::MemoryView)
43+
start = Base.memoryindex(x.ref)
44+
return (start:(start + length(x) - 1),)
3745
end
3846
end
3947

40-
# TODO: If `Core.memoryrefoffset` is made public, use this impl instead
41-
# function Base.parentindices(x::MemoryView)
42-
# start = Core.memoryrefoffset(x.ref)
43-
# return (start:(start + length(x) - 1),)
44-
# end
45-
4648
function Base.copy(x::MemoryView)
4749
isempty(x) && return x
4850
newmem = @inbounds x.ref.mem[only(parentindices(x))]

0 commit comments

Comments
 (0)