Skip to content

Commit

Permalink
Updates for subarray for new indices
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 7, 2016
1 parent 8852f94 commit 05c0528
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
5 changes: 0 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1313,11 +1313,6 @@ _lookup(ind, r::AbstractUnitRange) = ind+first(r)
_div(ind, d::Integer) = div(ind, d), 1, d
_div(ind, r::AbstractUnitRange) = (d = unsafe_length(r); (div(ind, d), first(r), d))

smart_ind2sub(shape::NTuple{1}, ind) = (ind,)
smart_ind2sub(shape, ind) = ind2sub(shape, ind)
smart_sub2ind(shape::NTuple{1}, i) = (i,)
smart_sub2ind(shape, I...) = (@_inline_meta; sub2ind(shape, I...))

# Vectorized forms
function sub2ind{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, I::AbstractVector{T}...)
I1 = I[1]
Expand Down
18 changes: 10 additions & 8 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ viewindexing(I::Tuple{Vararg{Any}}) = LinearSlow()
viewindexing(I::Tuple{AbstractArray, Vararg{Any}}) = LinearSlow()

dimlength(r::Range) = length(r)
dimlength(i::Integer) = i
dimlength(i::Integer) = Int(i)

# Simple utilities
size(V::SubArray) = V.dims
Expand Down Expand Up @@ -79,11 +79,17 @@ function view{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR
@boundscheck checkbounds(A, I...)
unsafe_view(reshape(A, Val{N}), I...)
end

function unsafe_view{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
@_inline_meta
J = to_indexes(I...)
SubArray(A, J, map(dimlength, index_shape(A, J...)))
end
function unsafe_view{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
@_inline_meta
idxs = reindex(V, V.indexes, to_indexes(I...))
SubArray(V.parent, idxs, map(dimlength, (index_shape(V.parent, idxs...))))
end

# Re-indexing is the heart of a view, transforming A[i, j][x, y] to A[i[x], j[y]]
#
Expand Down Expand Up @@ -169,12 +175,6 @@ function setindex!(V::FastContiguousSubArray, x, i::Real)
V
end

function unsafe_view{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
@_inline_meta
idxs = reindex(V, V.indexes, to_indexes(I...))
SubArray(V.parent, idxs, index_shape(V.parent, idxs...))
end

linearindexing{T<:FastSubArray}(::Type{T}) = LinearFast()
linearindexing{T<:SubArray}(::Type{T}) = LinearSlow()

Expand Down Expand Up @@ -261,7 +261,9 @@ unsafe_convert{T,N,P,I<:Tuple{Vararg{RangeIndex}}}(::Type{Ptr{T}}, V::SubArray{T

pointer(V::FastSubArray, i::Int) = pointer(V.parent, V.offset1 + V.stride1*i)
pointer(V::FastContiguousSubArray, i::Int) = pointer(V.parent, V.offset1 + i)
pointer(V::SubArray, i::Int) = pointer(V, smart_ind2sub(shape(V), i))
pointer(V::SubArray, i::Int) = _pointer(V, i)
_pointer{T}(V::SubArray{T,1}, i::Int) = pointer(V, (i,))
_pointer(V::SubArray, i::Int) = pointer(V, ind2sub(indices(V), i))

function pointer{T,N,P<:Array,I<:Tuple{Vararg{RangeIndex}}}(V::SubArray{T,N,P,I}, is::Tuple{Vararg{Int}})
index = first_index(V)
Expand Down
15 changes: 11 additions & 4 deletions test/subarray.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

using Base.Test
# import Base: ViewIndex, dimsizeexpr, rangetype, merge_indexes, first_index, stride1expr, tailsize
using Base.Cartesian

print_underestimates = false

######## Utilities ###########

Expand Down Expand Up @@ -336,6 +332,7 @@ sA = view(A, 2:2, 1:5, :)
@test parentindexes(sA) == (2:2, 1:5, :)
@test Base.parentdims(sA) == [1:3;]
@test size(sA) == (1, 5, 8)
@test indices(sA) === (Base.OneTo(1), Base.OneTo(5), Base.OneTo(8))
@test sA[1, 2, 1:8][:] == [5:15:120;]
sA[2:5:end] = -1
@test all(sA[2:5:end] .== -1)
Expand All @@ -355,6 +352,7 @@ test_bounds(sA)
sA = view(A, 1:3, 3:3, 2:5)
@test Base.parentdims(sA) == [1:3;]
@test size(sA) == (3,1,4)
@test indices(sA) === (Base.OneTo(3), Base.OneTo(1), Base.OneTo(4))
@test sA == A[1:3,3:3,2:5]
@test sA[:] == A[1:3,3,2:5][:]
test_bounds(sA)
Expand All @@ -367,6 +365,12 @@ sA = view(A, 1:2:3, 1:3:5, 1:2:8)
# Test with mixed types
@test sA[:, Int16[1,2], big(2)] == [31 40; 33 42]
test_bounds(sA)
sA = view(A, 1:1, 1:5, [1 3; 4 2])
@test ndims(sA) == 4
@test indices(sA) === (Base.OneTo(1), Base.OneTo(5), Base.OneTo(2), Base.OneTo(2))
sA = view(A, 1:2, 3, [1 3; 4 2])
@test ndims(sA) == 3
@test indices(sA) === (Base.OneTo(2), Base.OneTo(2), Base.OneTo(2))

# sub logical indexing #4763
A = view([1:10;], 5:8)
Expand All @@ -384,6 +388,7 @@ sA = view(A, 2, :, 1:8)
@test parentindexes(sA) == (2, :, 1:8)
@test Base.parentdims(sA) == [2:3;]
@test size(sA) == (5, 8)
@test indices(sA) === (Base.OneTo(5), Base.OneTo(8))
@test strides(sA) == (3,15)
@test sA[2, 1:8][:] == [5:15:120;]
@test sA[:,1] == [2:3:14;]
Expand All @@ -395,11 +400,13 @@ test_bounds(sA)
sA = view(A, 1:3, 1:5, 5)
@test Base.parentdims(sA) == [1:2;]
@test size(sA) == (3,5)
@test indices(sA) === (Base.OneTo(3),Base.OneTo(5))
@test strides(sA) == (1,3)
test_bounds(sA)
sA = view(A, 1:2:3, 3, 1:2:8)
@test Base.parentdims(sA) == [1,3]
@test size(sA) == (2,4)
@test indices(sA) === (Base.OneTo(2), Base.OneTo(4))
@test strides(sA) == (2,30)
@test sA[:] == A[sA.indexes...][:]
test_bounds(sA)
Expand Down

0 comments on commit 05c0528

Please sign in to comment.