Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #26

Merged
merged 4 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ __precompile__()

module OffsetArrays

Base.@deprecate_binding (..) Colon()

using Base: Indices, tail
using Compat

Expand All @@ -23,6 +21,8 @@ OffsetArray{T,N}(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) =
(::Type{OffsetArray{T,N}}){T,N}(inds::Indices{N}) =
OffsetArray{T,N,Array{T,N}}(Array{T,N}(map(length, inds)), map(indexoffset, inds))
(::Type{OffsetArray{T}}){T,N}(inds::Indices{N}) = OffsetArray{T,N}(inds)
(::Type{OffsetArray{T,N}}){T,N}(inds::Vararg{AbstractUnitRange,N}) = OffsetArray{T,N}(inds)
(::Type{OffsetArray{T}}){T,N}(inds::Vararg{AbstractUnitRange,N}) = OffsetArray{T,N}(inds)
OffsetArray{T}(A::AbstractArray{T,0}) = OffsetArray{T,0,typeof(A)}(A, ())
OffsetArray{T,N}(::Type{T}, inds::Vararg{UnitRange{Int},N}) = OffsetArray{T,N}(inds)

Expand Down Expand Up @@ -125,7 +125,7 @@ Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
### Low-level utilities ###

# Computing a shifted index (subtracting the offset)
offset{N}(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) = _offset((), offsets, inds)
@inline offset{N}(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) = _offset((), offsets, inds)
_offset(out, ::Tuple{}, ::Tuple{}) = out
@inline _offset(out, offsets, inds) =
_offset((out..., inds[1]-offsets[1]), Base.tail(offsets), Base.tail(inds))
Expand Down Expand Up @@ -213,11 +213,4 @@ end
@inline unsafe_getindex(a::OffsetSubArray, I::Union{Integer,CartesianIndex}...) = unsafe_getindex(a, Base.IteratorsMD.flatten(I)...)
@inline unsafe_setindex!(a::OffsetSubArray, val, I::Union{Integer,CartesianIndex}...) = unsafe_setindex!(a, val, Base.IteratorsMD.flatten(I)...)

# Deprecations
import Base: zeros, ones
@deprecate zeros(T::Type, ind1::UnitRange, ind2::UnitRange, inds::UnitRange...) fill!(OffsetArray{T}((ind1, ind2, inds...)), zero(T))
@deprecate ones(T::Type, ind1::UnitRange, inds2::UnitRange, inds::UnitRange...) fill!(OffsetArray{T}((ind1, ind2, inds...)), one(T))
@deprecate zeros(ind1::UnitRange, ind2::UnitRange, inds::UnitRange...) fill!(OffsetArray{Float64}((ind1, ind2, inds...)), 0)
@deprecate ones(ind1::UnitRange, ind2::UnitRange, inds::UnitRange...) fill!(OffsetArray{Float64}((ind1, ind2, inds...)), 1)

end # module
17 changes: 11 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ using Base.Test
using OffsetArrays
using Compat

ambs = detect_ambiguities(Base, Core) # in case these have ambiguities of their own
@test isempty(setdiff(detect_ambiguities(OffsetArrays, Base, Core), ambs))
@test isempty(detect_ambiguities(OffsetArrays, Base, Core))

# Basics
for n = 0:5
a = OffsetArray(ones(Int,ntuple(d->1,n)), ntuple(x->x-1,n))
@test length(linearindices(a)) == 1
@test indices(a) == ntuple(x->x:x,n)
@test a[1] == 1
for a in (OffsetArray(ones(Int,ntuple(d->1,n)), ntuple(x->x-1,n)),
fill!(OffsetArray(Float64, ntuple(x->(0:0)+x, n)...), 1),
fill!(OffsetArray{Float64}(ntuple(x->(0:0)+x, n)), 1),
fill!(OffsetArray{Float64}(ntuple(x->(0:0)+x, n)...), 1),
fill!(OffsetArray{Float64,n}(ntuple(x->(0:0)+x, n)), 1),
fill!(OffsetArray{Float64,n}(ntuple(x->(0:0)+x, n)...), 1))
@test length(linearindices(a)) == 1
@test indices(a) == ntuple(x->x:x,n)
@test a[1] == 1
end
end
a0 = reshape([3])
a = OffsetArray(a0)
Expand Down