Skip to content

Commit d8c8d42

Browse files
authored
no_offset_view in Origin (#280)
1 parent 11c4946 commit d8c8d42

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
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.11.0"
3+
version = "1.11.1"
44

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

src/OffsetArrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix)
210210
@eval @inline $FT(A::AbstractArray, origin::Origin; checkoverflow = true) = $FT(A, origin.index .- first.(axes(A)); checkoverflow = checkoverflow)
211211
end
212212

213-
(o::Origin)(A::AbstractArray) = OffsetArray(A, o)
214-
Origin(A::OffsetArray) = Origin(first.(axes(A)))
213+
(o::Origin)(A::AbstractArray) = OffsetArray(no_offset_view(A), o)
214+
Origin(A::AbstractArray) = Origin(first.(axes(A)))
215215

216216
# conversion-related methods
217217
@inline OffsetArray{T}(M::AbstractArray, I...; kw...) where {T} = OffsetArray{T,ndims(M)}(M, I...; kw...)

test/origin.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,31 @@ using OffsetArrays: Origin
4848
oa = OffsetArray.(a, Origin(0, -1))
4949
@test get_origin.(oa) == [ (0,-1), (0,-1) ]
5050
end
51+
52+
@testset "as a callable" begin
53+
a = [1 2; 3 4];
54+
@test OffsetArray(a, Origin(2)) == Origin(2)(a)
55+
for (index, firstinds) in Any[(1, (1,1)), ((2,3), (2,3))]
56+
b = Origin(index)(a)
57+
@test first.(axes(b)) == firstinds
58+
@test Origin(b) == Origin(firstinds)
59+
@test Origin(OffsetArrays.no_offset_view(b)) == Origin(ntuple(_ -> 1, Val(ndims(b))))
60+
end
61+
# compatibility with other array types
62+
@test Origin(Ones(2,2)) == Origin(1,1)
63+
@test Origin(SMatrix{2,2,Int,4}(1,2,3,4)) == Origin(1,1)
64+
end
65+
@testset "display" begin
66+
io = IOBuffer()
67+
show(io, Origin(1))
68+
@test String(take!(io)) == "Origin(1)"
69+
show(io, Origin(1, 1))
70+
@test String(take!(io)) == "Origin(1, 1)"
71+
end
72+
73+
@testset "avoid overflow (issue #279)" begin
74+
A = Origin(typemin(Int)+1)(rand(3,3))
75+
B = Origin(typemax(Int)-4)(A)
76+
@test first.(axes(B)) == ntuple(_ -> typemax(Int)-4, Val(ndims(B)))
77+
end
5178
end

test/runtests.jl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,25 +2594,6 @@ end
25942594

25952595
include("origin.jl")
25962596

2597-
@testset "Origin" begin
2598-
@testset "as a callable" begin
2599-
a = [1 2; 3 4];
2600-
@test OffsetArray(a, Origin(2)) === Origin(2)(a)
2601-
for (index, firstinds) in Any[(1, (1,1)), ((2,3), (2,3))]
2602-
b = Origin(index)(a)
2603-
@test first.(axes(b)) == firstinds
2604-
@test Origin(b) === Origin(firstinds)
2605-
end
2606-
end
2607-
@testset "display" begin
2608-
io = IOBuffer()
2609-
show(io, Origin(1))
2610-
@test String(take!(io)) == "Origin(1)"
2611-
show(io, Origin(1, 1))
2612-
@test String(take!(io)) == "Origin(1, 1)"
2613-
end
2614-
end
2615-
26162597
@testset "misc" begin
26172598
@test OffsetArrays._subtractoffset(Base.OneTo(2), 1) isa AbstractUnitRange{Int}
26182599
@test OffsetArrays._subtractoffset(Base.OneTo(2), 1) == 0:1

0 commit comments

Comments
 (0)