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 Jun 27, 2016
1 parent a0b2990 commit bc84f95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 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

0 comments on commit bc84f95

Please sign in to comment.