Skip to content

Commit 64339dc

Browse files
authored
Don't sort inplace in sortedunion (#381)
* don't sort inplace in sortedunion * Tests for maybeinplacesort
1 parent f879d94 commit 64339dc

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: src/blockbroadcast.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ BroadcastStyle(::PseudoBlockStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(
2929

3030

3131
# sortedunion can assume inputs are already sorted so this could be improved
32-
sortedunion(a,b) = sort!(union(a,b))
32+
maybeinplacesort!(v::StridedVector) = sort!(v)
33+
maybeinplacesort!(v) = sort(v)
34+
sortedunion(a,b) = maybeinplacesort!(union(a,b))
3335
sortedunion(a::Base.OneTo, b::Base.OneTo) = Base.OneTo(max(last(a),last(b)))
3436
sortedunion(a::AbstractUnitRange, b::AbstractUnitRange) = min(first(a),first(b)):max(last(a),last(b))
3537
combine_blockaxes(a, b) = _BlockedUnitRange(sortedunion(blocklasts(a), blocklasts(b)))

Diff for: test/test_blockbroadcast.jl

+12
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,16 @@ import BlockArrays: SubBlockIterator, BlockIndexRange, Diagonal
278278
C = B + B
279279
@test C[Block(1)] == 2B[Block(1)]
280280
end
281+
282+
@testset "utilities" begin
283+
for v in ([2,3,1], 2:4)
284+
w = BlockArrays.maybeinplacesort!(v)
285+
@test issorted(w)
286+
@test v === w
287+
end
288+
v = 3:-1:2
289+
w = BlockArrays.maybeinplacesort!(v)
290+
@test issorted(w)
291+
@test w == reverse(v)
292+
end
281293
end

0 commit comments

Comments
 (0)