Skip to content

Commit

Permalink
default dotview to getindex, using view only for AbstractArray
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jul 27, 2016
1 parent 46ffc87 commit df4a58d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 7 additions & 12 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,25 +443,20 @@ end
############################################################

# x[...] .= f.(y...) ---> broadcast!(f, dotview(x, ...), y...).
# The dotview function defaults to view, but we override it in
# The dotview function defaults to getindex, but we override it in
# a few cases to get the expected in-place behavior without affecting
# explicit calls to view. (All of this can go away if slices
# are changed to generate views by default.)

dotview(args...) = view(args...)
dotview(args...) = getindex(args...)
dotview(A::AbstractArray, args...) = view(A, args...)
# avoid splatting penalty in common cases:
for nargs = 0:5
args = Symbol[Symbol("x",i) for i = 1:nargs]
eval(Expr(:(=), Expr(:call, :dotview, args...), Expr(:call, :view, args...)))
eval(Expr(:(=), Expr(:call, :dotview, args...),
Expr(:call, :getindex, args...)))
eval(Expr(:(=), Expr(:call, :dotview, :(A::AbstractArray), args...),
Expr(:call, :view, :A, args...)))
end

# for a[i...] .= ... where a is an array-of-arrays, just pass a[i...] directly
# to broadcast!
dotview{T<:AbstractArray,N,I<:Integer}(a::AbstractArray{T,N}, i::Vararg{I,N}) =
a[i...]

# dict[k] .= ... should work if dict[k] is an array
dotview(a::Associative, k) = a[k]
dotview(a::Associative, k1, k2, ks...) = a[tuple(k1,k2,ks...)]

end # module
2 changes: 1 addition & 1 deletion doc/manual/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ calls do not allocate new arrays over and over again for the results
except that, as above, the ``broadcast!`` loop is fused with any nested
"dot" calls. For example, ``X .= sin.(Y)`` is equivalent to
``broadcast!(sin, X, Y)``, overwriting ``X`` with ``sin.(Y)`` in-place.
If the left-hand side is a ``getindex`` expression, e.g.
If the left-hand side is an array-indexing expression, e.g.
``X[2:end] .= sin.(Y)``, then it translates to ``broadcast!`` on a ``view``,
e.g. ``broadcast!(sin, view(X, 2:endof(X)), Y)``, so that the left-hand
side is updated in-place.
Expand Down

0 comments on commit df4a58d

Please sign in to comment.