-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to fix Broadcast.broadcast_shape inference
- Loading branch information
1 parent
ec8c7f5
commit 92567da
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
##### | ||
##### From TupleTools.jl | ||
##### | ||
function _split(t::Tuple) | ||
N = length(t) | ||
M = N >> 1 | ||
return ntuple(i -> t[i], M), ntuple(i -> t[i + M], N - M) | ||
end | ||
function _merge(t1::Tuple, t2::Tuple, lt, by, rev) | ||
if lt(by(first(t1)), by(first(t2))) != rev | ||
return (first(t1), _merge(tail(t1), t2, lt, by, rev)...) | ||
else | ||
return (first(t2), _merge(t1, tail(t2), lt, by, rev)...) | ||
end | ||
end | ||
_merge(::Tuple{}, t2::Tuple, lt, by, rev) = t2 | ||
_merge(t1::Tuple, ::Tuple{}, lt, by, rev) = t1 | ||
_merge(::Tuple{}, ::Tuple{}, lt, by, rev) = () | ||
|
||
tuple_sort(t::Tuple; lt=isless, by=identity, rev::Bool=false) = _tuple_sort(t, lt, by, rev) | ||
@inline function _tuple_sort(t::Tuple, lt=isless, by=identity, rev::Bool=false) | ||
t1, t2 = _split(t) | ||
t1s = _tuple_sort(t1, lt, by, rev) | ||
t2s = _tuple_sort(t2, lt, by, rev) | ||
return _merge(t1s, t2s, lt, by, rev) | ||
end | ||
_tuple_sort(t::Tuple{Any}, lt=isless, by=identity, rev::Bool=false) = t | ||
_tuple_sort(t::Tuple{}, lt=isless, by=identity, rev::Bool=false) = t | ||
|
||
|
||
###### | ||
###### tuple_union | ||
###### | ||
|
||
struct DistinctElems{T<:Tuple} | ||
elems::T | ||
end | ||
tuple_union(a::Tuple, b::Tuple) = distinct_elems(DistinctElems(()), a..., b...).elems | ||
|
||
distinct_elems(x::DistinctElems) = x | ||
|
||
distinct_elems(x::DistinctElems, r1) = | ||
r1 in x.elems ? x : DistinctElems((x.elems..., r1)) | ||
|
||
function distinct_elems(x::DistinctElems, r1, remaining...) | ||
return if r1 in x.elems | ||
distinct_elems(x, remaining...) | ||
else | ||
distinct_elems(DistinctElems((x.elems..., r1)), remaining...) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using BlockArrays | ||
using Test | ||
using Random | ||
|
||
@testset "Tuple Tools" begin | ||
@testset "tuple_sort" begin | ||
n = 10 | ||
p = randperm(n) | ||
t = (p...,) | ||
@test @inferred(BlockArrays.tuple_sort((1,))) == (1,) | ||
@test @inferred(BlockArrays.tuple_sort(())) == () | ||
@inferred(BlockArrays.tuple_sort(t; rev=true)) == (sort(p; rev=true)...,) | ||
@test @inferred(BlockArrays.tuple_sort(t; rev=false)) == (sort(p; rev=false)...,) | ||
@test BlockArrays.tuple_sort((2, 1, 3.0)) === (1, 2, 3.0) | ||
|
||
shape1 = (BlockArrays._BlockedUnitRange((2,)),); | ||
shape2 = (BlockArrays._BlockedUnitRange((2,)),); | ||
bl1 = BlockArrays.blocklasts(shape1[1]) | ||
bl2 = BlockArrays.blocklasts(shape2[1]) | ||
# @show BlockArrays.tuple_union(bl1,bl2) | ||
@test BlockArrays.sortedunion(bl1, bl2) == (2,) | ||
end | ||
|
||
# from Base | ||
@testset "tuple_union" begin | ||
for S in (identity,) | ||
s = BlockArrays.tuple_union(S((1,2)), S((3,4))) | ||
@test s == S((1,2,3,4)) | ||
s = BlockArrays.tuple_union(S((5,6,7,8)), S((7,8,9))) | ||
@test s == S((5,6,7,8,9)) | ||
s = BlockArrays.tuple_union(S((1,3,5,7)), (2,3,4,5)) | ||
@test s == S((1,3,5,7,2,4)) | ||
end | ||
end | ||
end |