From 9707594d37fd2d169974f1912dbb0ecc34ae378c Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Fri, 13 Jan 2023 12:01:08 -0600 Subject: [PATCH] rename QuickerSort to ScratchQuickSort (#48160) --- base/sort.jl | 34 +++++++++++++++++----------------- test/sorting.jl | 20 ++++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index 485b9d7fe1d14..985e0e8f597f3 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -86,7 +86,7 @@ issorted(itr; issorted(itr, ord(lt,by,rev,order)) function partialsort!(v::AbstractVector, k::Union{Integer,OrdinalRange}, o::Ordering) - _sort!(v, InitialOptimizations(QuickerSort(k)), o, (;)) + _sort!(v, InitialOptimizations(ScratchQuickSort(k)), o, (;)) maybeview(v, k) end @@ -950,12 +950,12 @@ end """ - QuickerSort(next::Algorithm=SMALL_ALGORITHM) <: Algorithm - QuickerSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}=lo, next::Algorithm=SMALL_ALGORITHM) <: Algorithm + ScratchQuickSort(next::Algorithm=SMALL_ALGORITHM) <: Algorithm + ScratchQuickSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}=lo, next::Algorithm=SMALL_ALGORITHM) <: Algorithm -Use the `QuickerSort` algorithm with the `next` algorithm as a base case. +Use the `ScratchQuickSort` algorithm with the `next` algorithm as a base case. -`QuickerSort` is like `QuickSort`, but utilizes scratch space to operate faster and allow +`ScratchQuickSort` is like `QuickSort`, but utilizes scratch space to operate faster and allow for the possibility of maintaining stability. If `lo` and `hi` are provided, finds and sorts the elements in the range `lo:hi`, reordering @@ -973,15 +973,15 @@ Characteristics: * *quadratic worst case runtime* in pathological cases (vanishingly rare for non-malicious input) """ -struct QuickerSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}, T<:Algorithm} <: Algorithm +struct ScratchQuickSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}, T<:Algorithm} <: Algorithm lo::L hi::H next::T end -QuickerSort(next::Algorithm=SMALL_ALGORITHM) = QuickerSort(missing, missing, next) -QuickerSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}) = QuickerSort(lo, hi, SMALL_ALGORITHM) -QuickerSort(lo::Union{Integer, Missing}, next::Algorithm=SMALL_ALGORITHM) = QuickerSort(lo, lo, next) -QuickerSort(r::OrdinalRange, next::Algorithm=SMALL_ALGORITHM) = QuickerSort(first(r), last(r), next) +ScratchQuickSort(next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(missing, missing, next) +ScratchQuickSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}) = ScratchQuickSort(lo, hi, SMALL_ALGORITHM) +ScratchQuickSort(lo::Union{Integer, Missing}, next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(lo, lo, next) +ScratchQuickSort(r::OrdinalRange, next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(first(r), last(r), next) # select a pivot, partition v[lo:hi] according # to the pivot, and store the result in t[lo:hi]. @@ -1020,7 +1020,7 @@ function partition!(t::AbstractVector, lo::Integer, hi::Integer, offset::Integer pivot_index end -function _sort!(v::AbstractVector, a::QuickerSort, o::Ordering, kw; +function _sort!(v::AbstractVector, a::ScratchQuickSort, o::Ordering, kw; t=nothing, offset=nothing, swap=false, rev=false) @getkw lo hi scratch @@ -1038,7 +1038,7 @@ function _sort!(v::AbstractVector, a::QuickerSort, o::Ordering, kw; end swap = !swap - # For QuickerSort(), a.lo === a.hi === missing, so the first two branches get skipped + # For ScratchQuickSort(), a.lo === a.hi === missing, so the first two branches get skipped if !ismissing(a.lo) && j <= a.lo # Skip sorting the lower part swap && copyto!(v, lo, t, lo+offset, j-lo) rev && reverse!(v, lo, j-1) @@ -1236,7 +1236,7 @@ the initial optimizations because they can change the input vector's type and or make them `UIntMappable`. If the input is not [`UIntMappable`](@ref), then we perform a presorted check and dispatch -to [`QuickerSort`](@ref). +to [`ScratchQuickSort`](@ref). Otherwise, we dispatch to [`InsertionSort`](@ref) for inputs with `length <= 40` and then perform a presorted check ([`CheckSorted`](@ref)). @@ -1268,7 +1268,7 @@ Consequently, we apply [`RadixSort`](@ref) for any reasonably long inputs that r stage. Finally, if the input has length less than 80, we dispatch to [`InsertionSort`](@ref) and -otherwise we dispatch to [`QuickerSort`](@ref). +otherwise we dispatch to [`ScratchQuickSort`](@ref). """ const DEFAULT_STABLE = InitialOptimizations( IsUIntMappable( @@ -1278,9 +1278,9 @@ const DEFAULT_STABLE = InitialOptimizations( ConsiderCountingSort( ConsiderRadixSort( Small{80}( - QuickerSort())))))), + ScratchQuickSort())))))), StableCheckSorted( - QuickerSort()))) + ScratchQuickSort()))) """ DEFAULT_UNSTABLE @@ -1485,7 +1485,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector, end # do partial quicksort - _sort!(ix, InitialOptimizations(QuickerSort(k)), Perm(ord(lt, by, rev, order), v), (;)) + _sort!(ix, InitialOptimizations(ScratchQuickSort(k)), Perm(ord(lt, by, rev, order), v), (;)) maybeview(ix, k) end diff --git a/test/sorting.jl b/test/sorting.jl index 1b79070d7e06e..691f0a0e2bc39 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -79,8 +79,8 @@ end end @testset "stability" begin - for Alg in [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.DEFAULT_STABLE, - Base.Sort.QuickerSort(missing, 1729), Base.Sort.QuickerSort(1729, missing)] + for Alg in [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.DEFAULT_STABLE, + Base.Sort.ScratchQuickSort(missing, 1729), Base.Sort.ScratchQuickSort(1729, missing)] @test issorted(sort(1:2000, alg=Alg, by=x->0)) @test issorted(sort(1:2000, alg=Alg, by=x->x÷100)) end @@ -333,7 +333,7 @@ end @test c == v # stable algorithms - for alg in [MergeSort, Base.Sort.QuickerSort(), Base.Sort.QuickerSort(1:n), Base.DEFAULT_STABLE] + for alg in [MergeSort, Base.Sort.ScratchQuickSort(), Base.Sort.ScratchQuickSort(1:n), Base.DEFAULT_STABLE] p = sortperm(v, alg=alg, rev=rev) p2 = sortperm(float(v), alg=alg, rev=rev) @test p == p2 @@ -381,7 +381,7 @@ end end v = randn_with_nans(n,0.1) - for alg in [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.Sort.QuickerSort(1, n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], + for alg in [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.Sort.ScratchQuickSort(1, n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], rev in [false,true] alg === InsertionSort && n >= 3000 && continue # test float sorting with NaNs @@ -588,7 +588,7 @@ end @testset "fallback" begin @test adaptive_sort_test(rand(1:typemax(Int32), len), by=x->x^2)# fallback - @test adaptive_sort_test(rand(Int, len), by=x->0, trusted=Base.Sort.QuickerSort()) + @test adaptive_sort_test(rand(Int, len), by=x->0, trusted=Base.Sort.ScratchQuickSort()) end @test adaptive_sort_test(rand(Int, 20)) # InsertionSort @@ -692,7 +692,7 @@ end # not allowed. Consequently, none of the behavior tested in this # testset is guaranteed to work in future minor versions of Julia. - safe_algs = [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE] + safe_algs = [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE] n = 1000 v = rand(1:5, n); @@ -899,8 +899,8 @@ end @test issorted(sort(rand(Int8, 600))) end -@testset "QuickerSort API" begin - bsqs = Base.Sort.QuickerSort +@testset "ScratchQuickSort API" begin + bsqs = Base.Sort.ScratchQuickSort @test bsqs(1, 2, MergeSort) === bsqs(1, 2, MergeSort) @test bsqs(missing, 2, MergeSort) === bsqs(missing, 2, MergeSort) @test bsqs(1, missing, MergeSort) === bsqs(1, missing, MergeSort) @@ -918,10 +918,10 @@ end @test bsqs() === bsqs(missing, missing, InsertionSort) end -@testset "QuickerSort allocations on non-concrete eltype" begin +@testset "ScratchQuickSort allocations on non-concrete eltype" begin v = Vector{Union{Nothing, Bool}}(rand(Bool, 10000)) @test 4 == @allocations sort(v) - @test 4 == @allocations sort(v; alg=Base.Sort.QuickerSort()) + @test 4 == @allocations sort(v; alg=Base.Sort.ScratchQuickSort()) # it would be nice if these numbers were lower (1 or 2), but these # test that we don't have O(n) allocations due to type instability end