@@ -44,6 +44,7 @@ export # not exported by Base
44
44
SMALL_ALGORITHM,
45
45
SMALL_THRESHOLD
46
46
47
+ abstract type Algorithm end
47
48
48
49
# # functions requiring only ordering ##
49
50
@@ -436,7 +437,7 @@ for (sym, exp, type) in [
436
437
(:mn , :(throw (ArgumentError (" mn is needed but has not been computed" ))), :(eltype (v))),
437
438
(:mx , :(throw (ArgumentError (" mx is needed but has not been computed" ))), :(eltype (v))),
438
439
(:scratch , nothing , :(Union{Nothing, Vector})), # could have different eltype
439
- (:allow_legacy_dispatch , true , Bool )]
440
+ (:legacy_dispatch_entry , nothing , Union{Nothing, Algorithm} )]
440
441
usym = Symbol (:_ , sym)
441
442
@eval function $usym (v, o, kw)
442
443
# using missing instead of nothing because scratch could === nothing.
@@ -499,8 +500,6 @@ internal or recursive calls.
499
500
"""
500
501
function _sort! end
501
502
502
- abstract type Algorithm end
503
-
504
503
505
504
"""
506
505
MissingOptimization(next) <: Algorithm
@@ -524,12 +523,12 @@ struct WithoutMissingVector{T, U} <: AbstractVector{T}
524
523
new {nonmissingtype(eltype(data)), typeof(data)} (data)
525
524
end
526
525
end
527
- Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i)
526
+ Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i:: Integer )
528
527
out = v. data[i]
529
528
@assert ! (out isa Missing)
530
529
out:: eltype (v)
531
530
end
532
- Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i)
531
+ Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i:: Integer )
533
532
v. data[i] = x
534
533
v
535
534
end
@@ -590,8 +589,9 @@ function _sort!(v::AbstractVector, a::MissingOptimization, o::Ordering, kw)
590
589
# we can assume v is equal to eachindex(o.data) which allows a copying partition
591
590
# without allocations.
592
591
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]]
595
595
if ismissing (x) == (o. order == Reverse) # should x go at the beginning/end?
596
596
v[lo_i] = i
597
597
lo_i += 1
@@ -2149,25 +2149,25 @@ end
2149
2149
# Support 3-, 5-, and 6-argument versions of sort! for calling into the internals in the old way
2150
2150
sort! (v:: AbstractVector , a:: Algorithm , o:: Ordering ) = sort! (v, firstindex (v), lastindex (v), a, o)
2151
2151
function 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 ))
2153
2153
v
2154
2154
end
2155
2155
sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering , _) = sort! (v, lo, hi, a, o)
2156
2156
function 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 ))
2158
2158
v
2159
2159
end
2160
2160
2161
2161
# Support dispatch on custom algorithms in the old way
2162
2162
# sort!(::AbstractVector, ::Integer, ::Integer, ::MyCustomAlgorithm, ::Ordering) = ...
2163
2163
function _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
2166
2169
sort! (v, lo, hi, a, o)
2167
2170
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" ))
2171
2171
end
2172
2172
end
2173
2173
0 commit comments