@@ -1073,17 +1073,26 @@ Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
1073
1073
@test t. b. d isa Array
1074
1074
end
1075
1075
1076
- struct MyArray{T,N} <: AbstractArray{T,N}
1077
- A:: Array{T,N}
1076
+ for S in (1 , 2 , 3 )
1077
+ MyArray = Symbol (:MyArray , S)
1078
+ @eval begin
1079
+ struct $ MyArray{T,N} <: AbstractArray{T,N}
1080
+ A:: Array{T,N}
1081
+ end
1082
+ $ MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = $ MyArray (Array {T} (undef, sz))
1083
+ Base. IndexStyle (:: Type{<:$MyArray} ) = IndexLinear ()
1084
+ Base. getindex (A:: $MyArray , i:: Int ) = A. A[i]
1085
+ Base. setindex! (A:: $MyArray , val, i:: Int ) = A. A[i] = val
1086
+ Base. size (A:: $MyArray ) = Base. size (A. A)
1087
+ Base. BroadcastStyle (:: Type{<:$MyArray} ) = Broadcast. ArrayStyle {$MyArray} ()
1088
+ end
1078
1089
end
1079
- MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = MyArray (Array {T} (undef, sz))
1080
- Base. IndexStyle (:: Type{<:MyArray} ) = IndexLinear ()
1081
- Base. getindex (A:: MyArray , i:: Int ) = A. A[i]
1082
- Base. setindex! (A:: MyArray , val, i:: Int ) = A. A[i] = val
1083
- Base. size (A:: MyArray ) = Base. size (A. A)
1084
- Base. BroadcastStyle (:: Type{<:MyArray} ) = Broadcast. ArrayStyle {MyArray} ()
1085
- Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}} , :: Type{ElType} ) where ElType =
1086
- MyArray {ElType} (undef, size (bc))
1090
+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray1}} , :: Type{ElType} ) where ElType =
1091
+ MyArray1 {ElType} (undef, size (bc))
1092
+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray2}} , :: Type{ElType} ) where ElType =
1093
+ MyArray2 {ElType} (undef, size (bc))
1094
+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray1} , :: Broadcast.ArrayStyle{MyArray3} ) = Broadcast. ArrayStyle {MyArray1} ()
1095
+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray2} , S:: Broadcast.DefaultArrayStyle ) = S
1087
1096
1088
1097
@testset " broadcast" begin
1089
1098
s = StructArray {ComplexF64} ((rand (2 ,2 ), rand (2 ,2 )))
@@ -1101,19 +1110,34 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
1101
1110
# used inside of broadcast but we also test it here explicitly
1102
1111
@test isa (@inferred (Base. dataids (s)), NTuple{N, UInt} where {N})
1103
1112
1104
- s = StructArray {ComplexF64} ((MyArray (rand (2 )), MyArray (rand (2 ))))
1105
- @test_throws MethodError s .+ s
1113
+ # Make sure we can handle style with similar defined
1114
+ # And we can handle most conflict
1115
+ # s1 and s2 has similar defined, but s3 not
1116
+ # s2 are conflict with s1 and s3. (And it's weaker than DefaultArrayStyle)
1117
+ s1 = StructArray {ComplexF64} ((MyArray1 (rand (2 )), MyArray1 (rand (2 ))))
1118
+ s2 = StructArray {ComplexF64} ((MyArray2 (rand (2 )), MyArray2 (rand (2 ))))
1119
+ s3 = StructArray {ComplexF64} ((MyArray3 (rand (2 )), MyArray3 (rand (2 ))))
1120
+ s4 = StructArray {ComplexF64} ((rand (2 ), rand (2 )))
1121
+
1122
+ function _test_similar (a, b, c)
1123
+ try
1124
+ d = StructArray {ComplexF64} ((a. re .+ b. re .- c. re, a. im .+ b. im .- c. im))
1125
+ @test typeof (a .+ b .- c) == typeof (d)
1126
+ catch
1127
+ @test_throws MethodError a .+ b .- c
1128
+ end
1129
+ end
1130
+ for s in (s1,s2,s3,s4), s′ in (s1,s2,s3,s4), s″ in (s1,s2,s3,s4)
1131
+ _test_similar (s, s′, s″)
1132
+ end
1106
1133
1107
1134
# test for dimensionality track
1135
+ s = s1
1108
1136
@test Base. broadcasted (+ , s, s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
1109
1137
@test Base. broadcasted (+ , s, 1 : 2 ) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
1110
1138
@test Base. broadcasted (+ , s, reshape (1 : 2 ,1 ,2 )) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{2} }
1111
1139
@test Base. broadcasted (+ , reshape (1 : 2 ,1 ,1 ,2 ), s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{3} }
1112
-
1113
- a = StructArray ([1 ;2 + im])
1114
- b = StructArray ([1 ;;2 + im])
1115
- @test a .+ b == a .+ collect (b) == collect (a) .+ b == collect (a) .+ collect (b)
1116
- @test a .+ Any[1 ] isa StructArray
1140
+ @test Base. broadcasted (+ , s, MyArray1 (rand (2 ))) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{Any} }
1117
1141
1118
1142
# issue #185
1119
1143
A = StructArray (randn (ComplexF64, 3 , 3 ))
@@ -1125,6 +1149,23 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
1125
1149
# issue #189
1126
1150
v = StructArray ([(a= " s1" ,), (a= " s2" ,)])
1127
1151
@test @inferred (broadcast (el -> el. a, v)) == [" s1" , " s2" ]
1152
+
1153
+ @testset " ambiguity check" begin
1154
+ function _test (a, b, c)
1155
+ if a isa StructArray || b isa StructArray || c isa StructArray
1156
+ d = @inferred a .+ b .- c
1157
+ @test d == collect (a) .+ collect (b) .- collect (c)
1158
+ @test d isa StructArray
1159
+ end
1160
+ end
1161
+ testset = Any[StructArray ([1 ;2 + im]),
1162
+ 1 : 2 ,
1163
+ (1 ,2 ),
1164
+ ]
1165
+ for aa in testset, bb in testset, cc in testset
1166
+ _test (aa, bb, cc)
1167
+ end
1168
+ end
1128
1169
end
1129
1170
1130
1171
@testset " map" begin
0 commit comments