Skip to content

Commit

Permalink
Make midpoint() backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
LSchwerdt committed Feb 7, 2023
1 parent 0d4aa65 commit 4d6e518
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/SortingAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -986,12 +986,16 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
end
end

# midpoint was added to Base.sort in version 1.4 and later moved to Base
# -> redefine for compatibility with earlier versions
_midpoint(lo::Integer,hi::Integer) = lo + ((hi - lo) >>> 0x01)

function pagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, buf::AbstractVector{T}, blockLocation, o=Base.Order.Forward) where T
len = hi + 1 -lo
if len <= Base.SMALL_THRESHOLD
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
end
m = Base.midpoint(lo,hi)
m = _midpoint(lo,hi)
pagedmergesort!(v,lo,m,buf,blockLocation,o)
pagedmergesort!(v,m+1,hi,buf,blockLocation,o)
if len <= length(buf)
Expand All @@ -1007,7 +1011,7 @@ function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, b
if len <= Base.SMALL_THRESHOLD
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
end
m = Base.midpoint(lo,hi)
m = _midpoint(lo,hi)
if len > threadingThreshold
thr = Threads.@spawn threaded_pagedmergesort!(v,lo,m,bufs,blockLocations,c,threadingThreshold,o)
threaded_pagedmergesort!(v,m+1,hi,bufs,blockLocations,c,threadingThreshold,o)
Expand Down

0 comments on commit 4d6e518

Please sign in to comment.