Skip to content

Commit

Permalink
Merge pull request #40 from fredrikekre/fe/uninitialized
Browse files Browse the repository at this point in the history
use uninitialized in OffsetArray constructors
  • Loading branch information
timholy authored Feb 2, 2018
2 parents ce24a51 + dc47fb8 commit a29db30
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ like Fortran.
```julia
julia> using OffsetArrays

julia> y = OffsetArray{Float64}(-1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);
julia> y = OffsetArray{Float64}(uninitialized, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);

julia> summary(y)
"OffsetArrays.OffsetArray{Float64,8,Array{Float64,8}} with indices -1:1×-7:7×-128:512×-5:5×-1:1×-3:3×-2:2×-1:1"
Expand Down
43 changes: 35 additions & 8 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,47 @@ OffsetArray(A::AbstractArray{T,N}, offsets::NTuple{N,Int}) where {T,N} =
OffsetArray(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) where {T,N} =
OffsetArray(A, offsets)

OffsetArray{T,N}(inds::Indices{N}) where {T,N} =
OffsetArray{T,N}(::Uninitialized, inds::Indices{N}) where {T,N} =
OffsetArray{T,N,Array{T,N}}(Array{T,N}(uninitialized, map(length, inds)), map(indexoffset, inds))
OffsetArray{T}(inds::Indices{N}) where {T,N} = OffsetArray{T,N}(inds)
OffsetArray{T,N}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
OffsetArray{T}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
OffsetArray{T}(::Uninitialized, inds::Indices{N}) where {T,N} = OffsetArray{T,N}(uninitialized, inds)
OffsetArray{T,N}(::Uninitialized, inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(uninitialized, inds)
OffsetArray{T}(::Uninitialized, inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(uninitialized, inds)
OffsetArray(A::AbstractArray{T,0}) where {T} = OffsetArray{T,0,typeof(A)}(A, ())

# OffsetVector constructors
OffsetVector(A::AbstractVector, offset) = OffsetArray(A, offset)
OffsetVector{T}(inds::AbstractUnitRange) where {T} = OffsetArray{T}(inds)
OffsetVector{T}(::Uninitialized, inds::AbstractUnitRange) where {T} = OffsetArray{T}(uninitialized, inds)

# deprecated constructors
using Base: @deprecate

# https://github.com/JuliaLang/julia/pull/19989
Base.@deprecate OffsetArray(::Type{T}, inds::Vararg{UnitRange{Int},N}) where {T,N} OffsetArray{T}(inds)
Base.@deprecate OffsetVector(::Type{T}, inds::AbstractUnitRange) where {T} OffsetVector{T}(inds)
@static if isdefined(Base, :Uninitialized)
@deprecate OffsetArray(::Type{T}, inds::Vararg{UnitRange{Int},N}) where {T,N} OffsetArray{T}(uninitialized, inds)
@deprecate OffsetVector(::Type{T}, inds::AbstractUnitRange) where {T} OffsetVector{T}(uninitialized, inds)
else
OffsetArray(::Type{T}, inds::Vararg{UnitRange{Int},N}) where {T,N} = OffsetArray{T}(inds)
OffsetVector(::Type{T}, inds::AbstractUnitRange) where {T} = OffsetVector{T}(inds)
end

# https://github.com/JuliaLang/julia/pull/24652
# Only activate deprecation if `uninitialized` is available from Base;
# should not rely on the user having `uninitialized` available from Compat
# and OffsetArrays.jl should probably not re-export Compat.uninitialized
@static if isdefined(Base, :Uninitialized)
@deprecate OffsetArray{T,N}(inds::Indices{N}) where {T,N} OffsetArray{T,N}(uninitialized, inds)
@deprecate OffsetArray{T}(inds::Indices{N}) where {T,N} OffsetArray{T}(uninitialized, inds)
@deprecate OffsetArray{T,N}(inds::Vararg{AbstractUnitRange,N}) where {T,N} OffsetArray{T,N}(uninitialized, inds)
@deprecate OffsetArray{T}(inds::Vararg{AbstractUnitRange,N}) where {T,N} OffsetArray{T}(uninitialized, inds)
@deprecate OffsetVector{T}(inds::AbstractUnitRange) where {T} OffsetVector{T}(uninitialized, inds)
else
OffsetArray{T,N}(inds::Indices{N}) where {T,N} = OffsetArray{T,N}(uninitialized, inds)
OffsetArray{T}(inds::Indices{N}) where {T,N} = OffsetArray{T}(uninitialized, inds)
OffsetArray{T,N}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(uninitialized, inds)
OffsetArray{T}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T}(uninitialized, inds)
OffsetVector{T}(inds::AbstractUnitRange) where {T} = OffsetVector{T}(uninitialized, inds)
end


# The next two are necessary for ambiguity resolution. Really, the
# second method should not be necessary.
Expand Down Expand Up @@ -141,7 +168,7 @@ end
### Convenience functions ###

Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
fill!(OffsetArray{typeof(x)}(inds), x)
fill!(OffsetArray{typeof(x)}(uninitialized, inds), x)
@inline Base.fill(x, ind1::UnitRange, inds::UnitRange...) = fill(x, (ind1, inds...))

### Low-level utilities ###
Expand Down
17 changes: 9 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat.Test
using OffsetArrays
using Compat
using Compat: axes, CartesianIndices, copyto!
using Compat.DelimitedFiles

Expand All @@ -8,10 +9,10 @@ using Compat.DelimitedFiles
# Basics
for n = 0:5
for a in (OffsetArray(ones(Int,ntuple(d->1,n)), ntuple(x->x-1,n)),
fill!(OffsetArray{Float64}(ntuple(x->x:x, n)), 1),
fill!(OffsetArray{Float64}(ntuple(x->x:x, n)...), 1),
fill!(OffsetArray{Float64,n}(ntuple(x->x:x, n)), 1),
fill!(OffsetArray{Float64,n}(ntuple(x->x:x, n)...), 1))
fill!(OffsetArray{Float64}(uninitialized, ntuple(x->x:x, n)), 1),
fill!(OffsetArray{Float64}(uninitialized, ntuple(x->x:x, n)...), 1),
fill!(OffsetArray{Float64,n}(uninitialized, ntuple(x->x:x, n)), 1),
fill!(OffsetArray{Float64,n}(uninitialized, ntuple(x->x:x, n)...), 1))
@test length(linearindices(a)) == 1
@test axes(a) == ntuple(x->x:x, n)
@test a[1] == 1
Expand All @@ -23,7 +24,7 @@ a = OffsetArray(a0)
@test ndims(a) == 0
@test a[] == 3

y = OffsetArray{Float64}(-1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1)
y = OffsetArray{Float64}(uninitialized, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1)
@test axes(y) == (-1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1)
y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
Expand Down Expand Up @@ -209,7 +210,7 @@ v = view(A0, 1:1, i1)
@test A[A .> 2] == [3,4]

# copyto!
a = OffsetArray{Int}((-3:-1,))
a = OffsetArray{Int}(uninitialized, (-3:-1,))
fill!(a, -1)
copyto!(a, (1,2)) # non-array iterables
@test a[-3] == 1
Expand Down Expand Up @@ -253,7 +254,7 @@ copyto!(a, -3, b, 2)
@test a[-3] == 2
@test a[-2] == a[-1] == -1
@test_throws BoundsError copyto!(a, -3, b, 1, 4)
am = OffsetArray{Int}((1:1, 7:9)) # for testing linear indexing
am = OffsetArray{Int}(uninitialized, (1:1, 7:9)) # for testing linear indexing
fill!(am, -1)
copyto!(am, b)
@test am[1] == 1
Expand Down Expand Up @@ -367,5 +368,5 @@ end
local v = rand(5)
@test OffsetVector(v, -2) == OffsetArray(v, -2)
@test OffsetVector(v, -2:2) == OffsetArray(v, -2:2)
@test typeof(OffsetVector{Float64}(-2:2)) == typeof(OffsetArray{Float64}(-2:2))
@test typeof(OffsetVector{Float64}(uninitialized, -2:2)) == typeof(OffsetArray{Float64}(uninitialized, -2:2))
end

0 comments on commit a29db30

Please sign in to comment.