@@ -43,6 +43,7 @@ export # not exported by Base
43
43
SMALL_ALGORITHM,
44
44
SMALL_THRESHOLD
45
45
46
+ abstract type Algorithm end
46
47
47
48
# # functions requiring only ordering ##
48
49
@@ -420,7 +421,7 @@ for (sym, exp, type) in [
420
421
(:mn , :(throw (ArgumentError (" mn is needed but has not been computed" ))), :(eltype (v))),
421
422
(:mx , :(throw (ArgumentError (" mx is needed but has not been computed" ))), :(eltype (v))),
422
423
(:scratch , nothing , :(Union{Nothing, Vector})), # could have different eltype
423
- (:allow_legacy_dispatch , true , Bool )]
424
+ (:legacy_dispatch_entry , nothing , Union{Nothing, Algorithm} )]
424
425
usym = Symbol (:_ , sym)
425
426
@eval function $usym (v, o, kw)
426
427
# using missing instead of nothing because scratch could === nothing.
@@ -483,8 +484,6 @@ internal or recursive calls.
483
484
"""
484
485
function _sort! end
485
486
486
- abstract type Algorithm end
487
-
488
487
489
488
"""
490
489
MissingOptimization(next) <: Algorithm
@@ -508,12 +507,12 @@ struct WithoutMissingVector{T, U} <: AbstractVector{T}
508
507
new {nonmissingtype(eltype(data)), typeof(data)} (data)
509
508
end
510
509
end
511
- Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i)
510
+ Base. @propagate_inbounds function Base. getindex (v:: WithoutMissingVector , i:: Integer )
512
511
out = v. data[i]
513
512
@assert ! (out isa Missing)
514
513
out:: eltype (v)
515
514
end
516
- Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i)
515
+ Base. @propagate_inbounds function Base. setindex! (v:: WithoutMissingVector , x, i:: Integer )
517
516
v. data[i] = x
518
517
v
519
518
end
@@ -574,8 +573,10 @@ function _sort!(v::AbstractVector, a::MissingOptimization, o::Ordering, kw)
574
573
# we can assume v is equal to eachindex(o.data) which allows a copying partition
575
574
# without allocations.
576
575
lo_i, hi_i = lo, hi
577
- for (i,x) in zip (eachindex (o. data), o. data)
578
- if ismissing (x) == (o. order == Reverse) # should i go at the beginning?
576
+ cv = eachindex (o. data) # equal to copy(v)
577
+ for i in lo: hi
578
+ x = o. data[cv[i]]
579
+ if ismissing (x) == (o. order == Reverse) # should x go at the beginning/end?
579
580
v[lo_i] = i
580
581
lo_i += 1
581
582
else
@@ -2119,25 +2120,25 @@ end
2119
2120
# Support 3-, 5-, and 6-argument versions of sort! for calling into the internals in the old way
2120
2121
sort! (v:: AbstractVector , a:: Algorithm , o:: Ordering ) = sort! (v, firstindex (v), lastindex (v), a, o)
2121
2122
function sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering )
2122
- _sort! (v, a, o, (; lo, hi, allow_legacy_dispatch = false ))
2123
+ _sort! (v, a, o, (; lo, hi, legacy_dispatch_entry = a ))
2123
2124
v
2124
2125
end
2125
2126
sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering , _) = sort! (v, lo, hi, a, o)
2126
2127
function sort! (v:: AbstractVector , lo:: Integer , hi:: Integer , a:: Algorithm , o:: Ordering , scratch:: Vector )
2127
- _sort! (v, a, o, (; lo, hi, scratch, allow_legacy_dispatch = false ))
2128
+ _sort! (v, a, o, (; lo, hi, scratch, legacy_dispatch_entry = a ))
2128
2129
v
2129
2130
end
2130
2131
2131
2132
# Support dispatch on custom algorithms in the old way
2132
2133
# sort!(::AbstractVector, ::Integer, ::Integer, ::MyCustomAlgorithm, ::Ordering) = ...
2133
2134
function _sort! (v:: AbstractVector , a:: Algorithm , o:: Ordering , kw)
2134
- @getkw lo hi scratch allow_legacy_dispatch
2135
- if allow_legacy_dispatch
2135
+ @getkw lo hi scratch legacy_dispatch_entry
2136
+ if legacy_dispatch_entry === a
2137
+ # This error prevents infinite recursion for unknown algorithms
2138
+ throw (ArgumentError (" Base.Sort._sort!(::$(typeof (v)) , ::$(typeof (a)) , ::$(typeof (o)) , ::Any) is not defined" ))
2139
+ else
2136
2140
sort! (v, lo, hi, a, o)
2137
2141
scratch
2138
- else
2139
- # This error prevents infinite recursion for unknown algorithms
2140
- throw (ArgumentError (" Base.Sort._sort!(::$(typeof (v)) , ::$(typeof (a)) , ::$(typeof (o)) ) is not defined" ))
2141
2142
end
2142
2143
end
2143
2144
0 commit comments