Skip to content

Commit 039b793

Browse files
committed
Even more simplifications
1 parent a99ca73 commit 039b793

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

base/broadcast.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ containertype(ct1, ct2) = promote_containertype(containertype(ct1), containertyp
3333
@inline containertype(ct1, ct2, cts...) = promote_containertype(containertype(ct1), containertype(ct2, cts...))
3434

3535
promote_containertype(ct1, ct2) = Array
36+
promote_containertype{T<:AbstractArray}(::Type{T}, ::Type{Tuple}) = Array
37+
promote_containertype{T<:AbstractArray}(::Type{Tuple}, ::Type{T}) = Array
3638
promote_containertype(::Type{Array}, ::Type{VecOrMat}) = Array
3739
promote_containertype(::Type{VecOrMat}, ::Type{Array}) = Array
38-
promote_containertype(::Type{VecOrMat}, ::Type{Tuple}) = Array
39-
promote_containertype(::Type{Tuple}, ::Type{VecOrMat}) = Array
40-
promote_containertype{T<:Array}(::Type{T}, ::ScalarType) = T
41-
promote_containertype{T<:Array}(::ScalarType, ::Type{T}) = T
4240
promote_containertype(::Type{Tuple}, ::ScalarType) = Tuple
4341
promote_containertype(::ScalarType, ::Type{Tuple}) = Tuple
4442
promote_containertype(::Type{Any}, ::Type{Nullable}) = Nullable
4543
promote_containertype(::Type{Nullable}, ::Type{Any}) = Nullable
44+
promote_containertype(T::Type, ::ScalarType) = T
45+
promote_containertype(::ScalarType, T::Type) = T
4646
promote_containertype{T}(::Type{T}, ::Type{T}) = T
4747

4848
## Calculate the broadcast indices of the arguments, or error if incompatible
@@ -52,7 +52,7 @@ broadcast_indices(A) = broadcast_indices(containertype(A), A)
5252
broadcast_indices(::ScalarType, A) = ()
5353
broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
5454
broadcast_indices(::Type{Array}, A::Ref) = ()
55-
broadcast_indices{T<:Array}(::Type{T}, A) = indices(A)
55+
broadcast_indices{T<:AbstractArray}(::Type{T}, A) = indices(A)
5656
@inline broadcast_indices(A, B...) = broadcast_shape((), broadcast_indices(A), map(broadcast_indices, B)...)
5757
# shape (i.e., tuple-of-indices) inputs
5858
broadcast_shape(shape::Tuple) = shape

base/sparse/higherorderfns.jl

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module HigherOrderFns
55
# This module provides higher order functions specialized for sparse arrays,
66
# particularly map[!]/broadcast[!] for SparseVectors and SparseMatrixCSCs at present.
77
import Base: map, map!, broadcast, broadcast!
8-
import Base.Broadcast: _containertype, promote_containertype,
9-
broadcast_indices, broadcast_c, broadcast_c!
8+
import Base.Broadcast: _containertype, promote_containertype, broadcast_c, broadcast_c!
109

1110
using Base: front, tail, to_shape
1211
using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseArray,
@@ -857,20 +856,14 @@ end
857856

858857
# (9) broadcast[!] over combinations of broadcast scalars and sparse vectors/matrices
859858

860-
# broadcast shape promotion for combinations of sparse arrays and other types
861-
broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A)
862859
# broadcast container type promotion for combinations of sparse arrays and other types
863860
_containertype{T<:SparseVecOrMat}(::Type{T}) = AbstractSparseArray
864861
# combinations of sparse arrays with broadcast scalars should yield sparse arrays
865862
promote_containertype{T<:AbstractSparseArray}(::Type{VecOrMat}, ::Type{T}) = AbstractSparseArray
866863
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{VecOrMat}) = AbstractSparseArray
867-
promote_containertype{T<:AbstractSparseArray}(::Type{Any}, ::Type{T}) = AbstractSparseArray
868-
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Any}) = AbstractSparseArray
869864
# combinations of sparse arrays with anything else should fall back to generic dense broadcast
870865
promote_containertype{T<:AbstractSparseArray}(::Type{Array}, ::Type{T}) = Array
871-
promote_containertype{T<:AbstractSparseArray}(::Type{Tuple}, ::Type{T}) = Array
872866
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Array}) = Array
873-
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Tuple}) = Array
874867

875868
# broadcast[!] entry points for combinations of sparse arrays and other (scalar) types
876869
@inline function broadcast_c{T<:AbstractSparseArray,N}(f, ::Type{T}, mixedargs::Vararg{Any,N})

test/broadcast.jl

+9-12
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,12 @@ Base.size(A::Array19745) = size(A.data)
393393

394394
Base.Broadcast._containertype{T<:Array19745}(::Type{T}) = Array19745
395395

396-
# This way of defining promote_containertype methods is discouraged. The recommended
397-
# way is by defining definitions for combinations of tight containers types
398-
Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Array19745}) = Array19745
399-
Base.Broadcast.promote_containertype(::Type{Array19745}, ct) = Array19745
400-
Base.Broadcast.promote_containertype(ct, ::Type{Array19745}) = Array19745
401-
402-
Base.Broadcast.broadcast_indices(::Type{Array19745}, A) = indices(A)
403-
Base.Broadcast.broadcast_indices(::Type{Array19745}, A::Ref) = ()
396+
# Only define promote_containertype methods with tight container types
397+
# (scalars are properly handled by default)
398+
Base.Broadcast.promote_containertype{T<:AbstractArray}(::Type{Array19745}, ::Type{T}) = Array19745
399+
Base.Broadcast.promote_containertype{T<:AbstractArray}(::Type{T}, ::Type{Array19745}) = Array19745
400+
Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Tuple}) = Array19745
401+
Base.Broadcast.promote_containertype(::Type{Tuple}, ::Type{Array19745}) = Array19745
404402

405403
getfield19745(x::Array19745) = x.data
406404
getfield19745(x) = x
@@ -411,10 +409,9 @@ Base.Broadcast.broadcast_c(f, ::Type{Array19745}, A, Bs...) =
411409
@testset "broadcasting for custom AbstractArray" begin
412410
a = randn(10)
413411
aa = Array19745(a)
414-
@test a .+ 1 == @inferred(aa .+ 1)
415-
@test a .* a' == @inferred(aa .* aa')
416-
@test isa(aa .+ 1, Array19745)
417-
@test isa(aa .* aa', Array19745)
412+
@test a .+ 1 == @inferred(aa .+ 1)::Array19745
413+
@test a .* a' == @inferred(aa .* aa')::Array19745
414+
@test (aa .+ [1])::Array19745 == (aa .+ (1,))::Array19745
418415
end
419416

420417
# broadcast should only "peel off" one container layer

0 commit comments

Comments
 (0)