Skip to content

Commit

Permalink
Don't sort inplace in sortedunion (#381)
Browse files Browse the repository at this point in the history
* don't sort inplace in sortedunion

* Tests for maybeinplacesort
  • Loading branch information
jishnub committed Apr 7, 2024
1 parent b66ae4a commit c182b7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/blockbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ BroadcastStyle(::PseudoBlockStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(


# sortedunion can assume inputs are already sorted so this could be improved
sortedunion(a,b) = sort!(union(a,b))
maybeinplacesort!(v::StridedVector) = sort!(v)
maybeinplacesort!(v) = sort(v)
sortedunion(a,b) = maybeinplacesort!(union(a,b))
sortedunion(a::Base.OneTo, b::Base.OneTo) = Base.OneTo(max(last(a),last(b)))
sortedunion(a::AbstractUnitRange, b::AbstractUnitRange) = min(first(a),first(b)):max(last(a),last(b))
combine_blockaxes(a, b) = _BlockedUnitRange(sortedunion(blocklasts(a), blocklasts(b)))
Expand Down
12 changes: 12 additions & 0 deletions test/test_blockbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,16 @@ import BlockArrays: SubBlockIterator, BlockIndexRange, Diagonal
C = B + B
@test C[Block(1)] == 2B[Block(1)]
end

@testset "utilities" begin
for v in ([2,3,1], 2:4)
w = BlockArrays.maybeinplacesort!(v)
@test issorted(w)
@test v === w
end
v = 3:-1:2
w = BlockArrays.maybeinplacesort!(v)
@test issorted(w)
@test w == reverse(v)
end
end

0 comments on commit c182b7e

Please sign in to comment.