@@ -5,7 +5,7 @@ module Broadcast
5
5
using Base. Cartesian
6
6
using Base: promote_op, promote_eltype, promote_eltype_op, @get! , _msk_end, unsafe_bitgetindex, linearindices, tail, OneTo, to_shape
7
7
import Base: .+ , .- , .* , ./ , .\ , .// , .== , .< , .!= , .<= , .÷ , .% , .<< , .>> , .^
8
- export broadcast, broadcast!, bitbroadcast
8
+ export broadcast, broadcast!, bitbroadcast, dotview
9
9
export broadcast_getindex, broadcast_setindex!
10
10
11
11
# # Broadcasting utilities ##
@@ -440,4 +440,28 @@ for (sigA, sigB) in ((BitArray, BitArray),
440
440
end
441
441
end
442
442
443
+ # ###########################################################
444
+
445
+ # x[...] .= f.(y...) ---> broadcast!(f, dotview(x, ...), y...).
446
+ # The dotview function defaults to view, but we override it in
447
+ # a few cases to get the expected in-place behavior without affecting
448
+ # explicit calls to view. (All of this can go away if slices
449
+ # are changed to generate views by default.)
450
+
451
+ dotview (args... ) = view (args... )
452
+ # avoid splatting penalty in common cases:
453
+ for nargs = 0 : 5
454
+ args = Symbol[Symbol (" x" ,i) for i = 1 : nargs]
455
+ eval (Expr (:(= ), Expr (:call , :dotview , args... ), Expr (:call , :view , args... )))
456
+ end
457
+
458
+ # for a[i...] .= ... where a is an array-of-arrays, just pass a[i...] directly
459
+ # to broadcast!
460
+ dotview {T<:AbstractArray,N,I<:Integer} (a:: AbstractArray{T,N} , i:: Vararg{I,N} ) =
461
+ a[i... ]
462
+
463
+ # dict[k] .= ... should work if dict[k] is an array
464
+ dotview (a:: Associative , k) = a[k]
465
+ dotview (a:: Associative , k1, k2, ks... ) = a[tuple (k1,k2,ks... )]
466
+
443
467
end # module
0 commit comments