Skip to content

Commit f06107e

Browse files
Lilith HafnerLilith Hafner
authored andcommitted
Fix and test sort!(OffsetArray(rand(200), -10))
1 parent bd8dbc3 commit f06107e

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

base/sort.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Sort
55
import ..@__MODULE__, ..parentmodule
66
const Base = parentmodule(@__MODULE__)
77
using .Base.Order
8-
using .Base: copymutable, LinearIndices, length, (:), iterate, elsize,
8+
using .Base: copymutable, LinearIndices, length, (:), iterate, elsize, checkbounds,
99
eachindex, axes, first, last, similar, zip, OrdinalRange, firstindex, lastindex,
1010
AbstractVector, @inbounds, AbstractRange, @eval, @inline, Vector, @noinline,
1111
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !,
@@ -605,7 +605,7 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, a::MergeSortAlg,
605605
hi-lo <= SMALL_THRESHOLD && return sort!(v, lo, hi, SMALL_ALGORITHM, o)
606606

607607
m = midpoint(lo, hi)
608-
t = workspace(v, t0, m-lo+1)
608+
t = workspace(v, t0, Base.OneTo(m-lo+1))
609609

610610
sort!(v, lo, m, a, o, t)
611611
sort!(v, m+1, hi, a, o, t)
@@ -683,7 +683,7 @@ function radix_sort!(v::AbstractVector{U}, lo::Integer, hi::Integer, bits::Unsig
683683
t::AbstractVector{U}, chunk_size=radix_chunk_size_heuristic(lo, hi, bits)) where U <: Unsigned
684684
# bits is unsigned for performance reasons.
685685
mask = UInt(1) << chunk_size - 1
686-
counts = Vector{UInt}(undef, mask+2)
686+
counts = Vector{Int}(undef, mask+2)
687687

688688
@inbounds for shift in 0:chunk_size:bits-1
689689

@@ -746,9 +746,9 @@ function sort!(v::AbstractVector{B}, lo::Integer, hi::Integer, a::AdaptiveSort,
746746
v
747747
end
748748

749-
workspace(v::AbstractVector, ::Nothing, len::Integer) = similar(v, len)
750-
function workspace(v::AbstractVector{T}, t::AbstractVector{T}, len::Integer) where T
751-
length(t) < len ? resize!(t, len) : t
749+
workspace(v::AbstractVector, ::Nothing, indices::AbstractUnitRange=eachindex(v)) = similar(v, indices)
750+
function workspace(v::AbstractVector{T}, t::AbstractVector{T}, indices::AbstractUnitRange=eachindex(v)) where T
751+
checkbounds(Bool, t, indices) ? t : similar(t, indices)
752752
end
753753
maybe_unsigned(x::Integer) = x # this is necessary to avoid calling unsigned on BigInt
754754
maybe_unsigned(x::BitSigned) = unsigned(x)
@@ -856,7 +856,7 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, a::AdaptiveSort,
856856
u[i] -= u_min
857857
end
858858

859-
u2 = radix_sort!(u, lo, hi, bits, reinterpret(U, workspace(v, t, hi)))
859+
u2 = radix_sort!(u, lo, hi, bits, reinterpret(U, workspace(v, t)))
860860
uint_unmap!(v, u2, lo, hi, o, u_min)
861861
end
862862

test/sorting.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ end
513513
@test issorted(a)
514514
end
515515

516+
@testset "sort!(::OffsetVector)" begin
517+
for length in vcat(0:5, [10, 300, 500, 1000])
518+
for offset in [-100000, -10, -1, 0, 1, 17, 1729]
519+
x = OffsetVector(rand(length), offset)
520+
sort!(x)
521+
@test issorted(x)
522+
end
523+
end
524+
end
525+
516526
@testset "sort!(::OffsetMatrix; dims)" begin
517527
x = OffsetMatrix(rand(5,5), 5, -5)
518528
sort!(x; dims=1)
@@ -656,11 +666,10 @@ end
656666

657667
@testset "workspace()" begin
658668
for v in [[1, 2, 3], [0.0]]
659-
for t0 in vcat([nothing], [similar(v,i) for i in 1:5]), len in 0:5
660-
t = Base.Sort.workspace(v, t0, len)
669+
for t0 in vcat([nothing], [similar(v,i) for i in 1:5]), axes in [0:5, 1:3, Base.OneTo(7), 3:2, -1:0]
670+
t = Base.Sort.workspace(v, t0, axes)
661671
@test eltype(t) == eltype(v)
662-
@test length(t) >= len
663-
@test firstindex(t) == 1
672+
@test checkbounds(Bool, t, axes)
664673
end
665674
end
666675
end
@@ -681,7 +690,7 @@ end
681690
end
682691
end
683692

684-
693+
# This testset is at the end of the file because it is slow
685694
@testset "searchsorted" begin
686695
numTypes = [ Int8, Int16, Int32, Int64, Int128,
687696
UInt8, UInt16, UInt32, UInt64, UInt128,

0 commit comments

Comments
 (0)