Skip to content

Commit cd9b075

Browse files
authored
avoid dynamic dispatch in no_offset_view(::Array) (#175)
* avoid dynamic dispatch in no_offset_view(::Array) * version bump to v1.4.2
1 parent 233a7b9 commit cd9b075

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
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.1"
3+
version = "1.4.2"
44

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

src/OffsetArrays.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ end
377377
"""
378378
no_offset_view(A)
379379
380-
Return an `AbstractArray` that shares structure and has the same type and size as the
381-
argument, but has 1-based indexing. May just return the argument when applicable. Not
382-
exported.
380+
Return an `AbstractArray` that shares structure and underlying data with the argument,
381+
but uses 1-based indexing. May just return the argument when applicable.
382+
Not exported.
383383
384384
The default implementation uses `OffsetArrays`, but other types should use something more
385385
specific to remove a level of indirection when applicable.
@@ -403,14 +403,14 @@ julia> A
403403
"""
404404
function no_offset_view(A::AbstractArray)
405405
if Base.has_offset_axes(A)
406-
OffsetArray(A, map(r->1-first(r), axes(A)))
406+
OffsetArray(A, Origin(1))
407407
else
408408
A
409409
end
410410
end
411411

412412
no_offset_view(A::OffsetArray) = no_offset_view(parent(A))
413-
413+
no_offset_view(a::Array) = a
414414

415415
####
416416
# work around for segfault in searchsorted*

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,12 +1309,25 @@ function Base.getindex(A::NegativeArray{T,N}, I::Vararg{Int,N}) where {T,N}
13091309
getindex(A.parent, (I .+ size(A.parent) .+ 1)...)
13101310
end
13111311

1312+
struct PointlessWrapper{T,N, A <: AbstractArray{T,N}} <: AbstractArray{T,N}
1313+
parent :: A
1314+
end
1315+
Base.parent(x::PointlessWrapper) = x.parent
1316+
Base.size(x::PointlessWrapper) = size(parent(x))
1317+
Base.axes(x::PointlessWrapper) = axes(parent(x))
1318+
13121319
@testset "no offset view" begin
13131320
# OffsetArray fallback
13141321
A = randn(3, 3)
1322+
@inferred no_offset_view(A)
13151323
O1 = OffsetArray(A, -1:1, 0:2)
13161324
O2 = OffsetArray(O1, -2:0, -3:(-1))
13171325
@test no_offset_view(O2) A
1326+
@inferred no_offset_view(O1)
1327+
@inferred no_offset_view(O2)
1328+
1329+
P = PointlessWrapper(A)
1330+
@test no_offset_view(P) P
13181331

13191332
# generic fallback
13201333
A = collect(reshape(1:12, 3, 4))

0 commit comments

Comments
 (0)