Skip to content

Commit 62e1fb7

Browse files
Lilith HafnerLilith Hafner
authored andcommitted
Convert to 1-based indexing rather than generalize to arbitrary indexing
1 parent f06107e commit 62e1fb7

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

base/sort.jl

Lines changed: 10 additions & 9 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, checkbounds,
8+
using .Base: copymutable, LinearIndices, length, (:), iterate, elsize,
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, Base.OneTo(m-lo+1))
608+
t = workspace(v, t0, m-lo+1)
609609

610610
sort!(v, lo, m, a, o, t)
611611
sort!(v, m+1, hi, a, o, t)
@@ -682,8 +682,9 @@ end
682682
function radix_sort!(v::AbstractVector{U}, lo::Integer, hi::Integer, bits::Unsigned,
683683
t::AbstractVector{U}, chunk_size=radix_chunk_size_heuristic(lo, hi, bits)) where U <: Unsigned
684684
# bits is unsigned for performance reasons.
685+
Base.require_one_based_indexing(v, t)
685686
mask = UInt(1) << chunk_size - 1
686-
counts = Vector{Int}(undef, mask+2)
687+
counts = Vector{UInt}(undef, mask+2)
687688

688689
@inbounds for shift in 0:chunk_size:bits-1
689690

@@ -746,9 +747,9 @@ function sort!(v::AbstractVector{B}, lo::Integer, hi::Integer, a::AdaptiveSort,
746747
v
747748
end
748749

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)
750+
workspace(v::AbstractVector, ::Nothing, len::Integer) = similar(v, len)
751+
function workspace(v::AbstractVector{T}, t::AbstractVector{T}, len::Integer) where T
752+
length(t) < len ? resize!(t, len) : t
752753
end
753754
maybe_unsigned(x::Integer) = x # this is necessary to avoid calling unsigned on BigInt
754755
maybe_unsigned(x::BitSigned) = unsigned(x)
@@ -856,7 +857,7 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, a::AdaptiveSort,
856857
u[i] -= u_min
857858
end
858859

859-
u2 = radix_sort!(u, lo, hi, bits, reinterpret(U, workspace(v, t)))
860+
u2 = radix_sort!(view(u, lo:hi), 1, hi-lo+1, bits, reinterpret(U, workspace(v, t, hi-lo+1)))
860861
uint_unmap!(v, u2, lo, hi, o, u_min)
861862
end
862863

@@ -1393,8 +1394,8 @@ end
13931394

13941395
function uint_unmap!(v::AbstractVector, u::AbstractVector{U}, lo::Integer, hi::Integer,
13951396
order::Ordering, offset::U=zero(U)) where U <: Unsigned
1396-
@inbounds for i in lo:hi
1397-
v[i] = uint_unmap(eltype(v), u[i]+offset, order)
1397+
@inbounds for (i, j) in zip(lo:hi, eachindex(u))
1398+
v[i] = uint_unmap(eltype(v), u[j]+offset, order)
13981399
end
13991400
v
14001401
end

test/sorting.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,11 @@ end
666666

667667
@testset "workspace()" begin
668668
for v in [[1, 2, 3], [0.0]]
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)
669+
for t0 in vcat([nothing], [similar(v,i) for i in 1:5]), len in 0:5
670+
t = Base.Sort.workspace(v, t0, len)
671671
@test eltype(t) == eltype(v)
672-
@test checkbounds(Bool, t, axes)
672+
@test length(t) >= len
673+
@test firstindex(t) == 1
673674
end
674675
end
675676
end

0 commit comments

Comments
 (0)