Skip to content

Commit

Permalink
Convert to parametric syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Nov 11, 2014
1 parent 1ff7445 commit dd63c47
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions base/subarray2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ for i = 1:4
push!(varsOther, :($sym::Union(Real, AbstractVector)))
push!(vars_toindex, :(Base.to_index($sym)))
ex = i == 1 ? quote
getindex(V::SubArray, $sym::Real) = getindex(V, Base.to_index($sym))
setindex!(V::SubArray, v, $sym::Real) = setindex!(V, v, Base.to_index($sym))
getindex(V::SubArray, $sym::AbstractVector{Bool}) = getindex(V, Base.to_index($sym))
setindex!(V::SubArray, v, $sym::AbstractVector{Bool}) = setindex!(V, v, Base.to_index($sym))
getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, $sym::Real) = getindex(V, Base.to_index($sym))
setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, $sym::Real) = setindex!(V, v, Base.to_index($sym))
getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, $sym::AbstractVector{Bool}) = getindex(V, Base.to_index($sym))
setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, $sym::AbstractVector{Bool}) = setindex!(V, v, Base.to_index($sym))
end : quote
getindex(V::SubArray, $(varsOther...)) = getindex(V, $(vars_toindex...))
setindex!(V::SubArray, v, $(varsOther...)) = setindex!(V, v, $(vars_toindex...))
getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, $(varsOther...)) = getindex(V, $(vars_toindex...))
setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, $(varsOther...)) = setindex!(V, v, $(vars_toindex...))
end
@eval begin
stagedfunction getindex(V::SubArray, $(varsInt...))
T, N, P, IV = V.parameters
stagedfunction getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, $(varsInt...))
exhead, ex = index_generate(ndims(P), IV, :V, [$(vars...)])
quote
$exhead
$ex
end
end
stagedfunction setindex!(V::SubArray, v, $(varsInt...))
T, N, P, IV = V.parameters
stagedfunction setindex!{T,N,P<:AbstractArray,IV}(V::SubArray{T,N,P,IV}, v, $(varsInt...))
println(P)
nd = -1
try
nd = ndims(P)
catch
println("catch")
error("try again")
end
println(nd)
exhead, ex = index_generate(ndims(P), IV, :V, [$(vars...)])
quote
$exhead
Expand All @@ -41,8 +48,7 @@ for i = 1:4
end
end
# V[] notation (extracts the first element)
stagedfunction getindex(V::SubArray)
T, N, P, IV = V.parameters
stagedfunction getindex{T,N,P,IV}(V::SubArray{T,N,P,IV})
Isyms = ones(Int, N)
exhead, ex = index_generate(ndims(P), IV, :V, Isyms)
quote
Expand All @@ -51,17 +57,15 @@ stagedfunction getindex(V::SubArray)
end
end
# Splatting variants
stagedfunction getindex(V::SubArray, I::Int...)
T, N, P, IV = V.parameters
stagedfunction getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, I::Int...)
Isyms = [:(I[$d]) for d = 1:length(I)]
exhead, ex = index_generate(ndims(P), IV, :V, Isyms)
quote
$exhead
$ex
end
end
stagedfunction setindex!(V::SubArray, v, I::Int...)
T, N, P, IV = V.parameters
stagedfunction setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, I::Int...)
Isyms = [:(I[$d]) for d = 1:length(I)]
exhead, ex = index_generate(ndims(P), IV, :V, Isyms)
quote
Expand All @@ -72,24 +76,24 @@ end

# Indexing with non-scalars. For now, this returns a copy, but changing that
# is just a matter of deleting the explicit call to copy.
getindex(V::SubArray, I::ViewIndex...) = copy(sub(V, I...))
getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, I::ViewIndex...) = copy(sub(V, I...))
getindex{T,N}(V::SubArray{T,N}, I::AbstractArray{Bool,N}) = copy(sub(V, find(I))) # this could be much better optimized
getindex{T,N}(V::SubArray{T,N}, I::Union(Real, AbstractVector)...) = getindex(V, Base.to_index(I)...)
getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, I::Union(Real, AbstractVector)...) = getindex(V, Base.to_index(I)...)

function setindex!{T}(V::SubArray{T,1}, v, I::AbstractArray{Bool,1})
function setindex!{T,P,IV}(V::SubArray{T,1,P,IV}, v, I::AbstractArray{Bool,1})
length(I) == length(V) || throw(DimensionMismatch("logical vector must match array length"))
setindex!(V, v, Base.to_index(I))
end
function setindex!{T,N}(V::SubArray{T,N}, v, I::AbstractArray{Bool,1})
function setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, I::AbstractArray{Bool,1})
length(I) == length(V) || throw(DimensionMismatch("logical vector must match array length"))
setindex!(V, v, Base.to_index(I))
end
function setindex!{T,N}(V::SubArray{T,N}, v, I::AbstractArray{Bool,N})
function setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, I::AbstractArray{Bool,N})
size(I) == size(V) || throw(DimensionMismatch("size of Boolean mask must match array size"))
_setindex!(V, v, find(I)) # this could be better optimized
end
setindex!{T,N}(V::SubArray{T,N}, v, I::Union(Real,AbstractVector)...) = setindex!(V, v, Base.to_index(I)...)
setindex!(V::SubArray, x, J::Union(Int,AbstractVector)...) = _setindex!(V, x, J...)
setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, v, I::Union(Real,AbstractVector)...) = setindex!(V, v, Base.to_index(I)...)
setindex!{T,N,P,IV}(V::SubArray{T,N,P,IV}, x, J::Union(Int,AbstractVector)...) = _setindex!(V, x, J...)
stagedfunction _setindex!(V::SubArray, x, J::Union(Real,AbstractVector)...)
gen_setindex_body(length(J))
end
Expand Down

0 comments on commit dd63c47

Please sign in to comment.