Skip to content

Commit

Permalink
no_offset_view in Origin (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed May 17, 2022
1 parent 11c4946 commit d8c8d42
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OffsetArrays"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
version = "1.11.0"
version = "1.11.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
4 changes: 2 additions & 2 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix)
@eval @inline $FT(A::AbstractArray, origin::Origin; checkoverflow = true) = $FT(A, origin.index .- first.(axes(A)); checkoverflow = checkoverflow)
end

(o::Origin)(A::AbstractArray) = OffsetArray(A, o)
Origin(A::OffsetArray) = Origin(first.(axes(A)))
(o::Origin)(A::AbstractArray) = OffsetArray(no_offset_view(A), o)
Origin(A::AbstractArray) = Origin(first.(axes(A)))

# conversion-related methods
@inline OffsetArray{T}(M::AbstractArray, I...; kw...) where {T} = OffsetArray{T,ndims(M)}(M, I...; kw...)
Expand Down
27 changes: 27 additions & 0 deletions test/origin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,31 @@ using OffsetArrays: Origin
oa = OffsetArray.(a, Origin(0, -1))
@test get_origin.(oa) == [ (0,-1), (0,-1) ]
end

@testset "as a callable" begin
a = [1 2; 3 4];
@test OffsetArray(a, Origin(2)) == Origin(2)(a)
for (index, firstinds) in Any[(1, (1,1)), ((2,3), (2,3))]
b = Origin(index)(a)
@test first.(axes(b)) == firstinds
@test Origin(b) == Origin(firstinds)
@test Origin(OffsetArrays.no_offset_view(b)) == Origin(ntuple(_ -> 1, Val(ndims(b))))
end
# compatibility with other array types
@test Origin(Ones(2,2)) == Origin(1,1)
@test Origin(SMatrix{2,2,Int,4}(1,2,3,4)) == Origin(1,1)
end
@testset "display" begin
io = IOBuffer()
show(io, Origin(1))
@test String(take!(io)) == "Origin(1)"
show(io, Origin(1, 1))
@test String(take!(io)) == "Origin(1, 1)"
end

@testset "avoid overflow (issue #279)" begin
A = Origin(typemin(Int)+1)(rand(3,3))
B = Origin(typemax(Int)-4)(A)
@test first.(axes(B)) == ntuple(_ -> typemax(Int)-4, Val(ndims(B)))
end
end
19 changes: 0 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2594,25 +2594,6 @@ end

include("origin.jl")

@testset "Origin" begin
@testset "as a callable" begin
a = [1 2; 3 4];
@test OffsetArray(a, Origin(2)) === Origin(2)(a)
for (index, firstinds) in Any[(1, (1,1)), ((2,3), (2,3))]
b = Origin(index)(a)
@test first.(axes(b)) == firstinds
@test Origin(b) === Origin(firstinds)
end
end
@testset "display" begin
io = IOBuffer()
show(io, Origin(1))
@test String(take!(io)) == "Origin(1)"
show(io, Origin(1, 1))
@test String(take!(io)) == "Origin(1, 1)"
end
end

@testset "misc" begin
@test OffsetArrays._subtractoffset(Base.OneTo(2), 1) isa AbstractUnitRange{Int}
@test OffsetArrays._subtractoffset(Base.OneTo(2), 1) == 0:1
Expand Down

2 comments on commit d8c8d42

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/60411

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.11.1 -m "<description of version>" d8c8d42d337b04c3965de295eb8319d2f03b2f4d
git push origin v1.11.1

Please sign in to comment.