@@ -5,7 +5,7 @@ module Sort
55import  .. @__MODULE__ , .. parentmodule
66const  Base =  parentmodule (@__MODULE__ )
77using  . 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)
682682function  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
747748end 
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 
752753end 
753754maybe_unsigned (x:: Integer ) =  x #  this is necessary to avoid calling unsigned on BigInt
754755maybe_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)
861862end 
862863
@@ -1393,8 +1394,8 @@ end
13931394
13941395function  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
14001401end 
0 commit comments