@@ -44,6 +44,7 @@ export # not exported by Base
4444 SMALL_ALGORITHM,
4545 SMALL_THRESHOLD
4646
47+ abstract type Algorithm end
4748
4849# # functions requiring only ordering ##
4950
@@ -436,7 +437,7 @@ for (sym, exp, type) in [
436437 (:mn , :(throw (ArgumentError (" mn is needed but has not been computed" ))), :(eltype (v))),
437438 (:mx , :(throw (ArgumentError (" mx is needed but has not been computed" ))), :(eltype (v))),
438439 (:scratch , nothing , :(Union{Nothing, Vector})), # could have different eltype
439- (:allow_legacy_dispatch , true , Bool )]
440+ (:legacy_dispatch_entry , nothing , Union{Nothing, Algorithm} )]
440441 usym = Symbol (:_ , sym)
441442 @eval function $usym (v, o, kw)
442443 # using missing instead of nothing because scratch could === nothing.
@@ -499,8 +500,6 @@ internal or recursive calls.
499500"""
500501function _sort! end
501502
502- abstract type Algorithm end
503-
504503
505504"""
506505 MissingOptimization(next) <: Algorithm
@@ -524,12 +523,12 @@ struct WithoutMissingVector{T, U} <: AbstractVector{T}
524523 new {nonmissingtype(eltype(data)), typeof(data)} (data)
525524 end
526525end
527- Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i)
526+ Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i:: Integer )
528527 out = v. data[i]
529528 @assert ! (out isa Missing)
530529 out:: eltype (v)
531530end
532- Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i)
531+ Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i:: Integer )
533532 v. data[i] = x
534533 v
535534end
@@ -590,8 +589,9 @@ function _sort!(v::AbstractVector, a::MissingOptimization, o::Ordering, kw)
590589 # we can assume v is equal to eachindex(o.data) which allows a copying partition
591590 # without allocations.
592591 lo_i, hi_i = lo, hi
593- for i in eachindex (o. data) # equal to copy(v)
594- x = o. data[i]
592+ cv = eachindex (o. data) # equal to copy(v)
593+ for i in lo: hi
594+ x = o. data[cv[i]]
595595 if ismissing (x) == (o. order == Reverse) # should x go at the beginning/end?
596596 v[lo_i] = i
597597 lo_i += 1
@@ -2149,25 +2149,25 @@ end
21492149# Support 3-, 5-, and 6-argument versions of sort! for calling into the internals in the old way
21502150sort! (v:: AbstractVector , a:: Algorithm , o:: Ordering ) = sort! (v, firstindex (v), lastindex (v), a, o)
21512151function sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering )
2152- _sort! (v, a, o, (; lo, hi, allow_legacy_dispatch = false ))
2152+ _sort! (v, a, o, (; lo, hi, legacy_dispatch_entry = a ))
21532153 v
21542154end
21552155sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering , _) = sort! (v, lo, hi, a, o)
21562156function sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering , scratch:: Vector )
2157- _sort! (v, a, o, (; lo, hi, scratch, allow_legacy_dispatch = false ))
2157+ _sort! (v, a, o, (; lo, hi, scratch, legacy_dispatch_entry = a ))
21582158 v
21592159end
21602160
21612161# Support dispatch on custom algorithms in the old way
21622162# sort!(::AbstractVector, ::Integer, ::Integer, ::MyCustomAlgorithm, ::Ordering) = ...
21632163function _sort! (v:: AbstractVector , a:: Algorithm , o:: Ordering , kw)
2164- @getkw lo hi scratch allow_legacy_dispatch
2165- if allow_legacy_dispatch
2164+ @getkw lo hi scratch legacy_dispatch_entry
2165+ if legacy_dispatch_entry === a
2166+ # This error prevents infinite recursion for unknown algorithms
2167+ throw (ArgumentError (" Base.Sort._sort!(::$(typeof (v)) , ::$(typeof (a)) , ::$(typeof (o)) , ::Any) is not defined" ))
2168+ else
21662169 sort! (v, lo, hi, a, o)
21672170 scratch
2168- else
2169- # This error prevents infinite recursion for unknown algorithms
2170- throw (ArgumentError (" Base.Sort._sort!(::$(typeof (v)) , ::$(typeof (a)) , ::$(typeof (o)) ) is not defined" ))
21712171 end
21722172end
21732173
0 commit comments