Skip to content

Commit

Permalink
Attempt to improve type stability
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Dec 28, 2024
1 parent 9aef19a commit c4d4be3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Sorting/quicksort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ argsortnans!(I::AbstractArray, A::AbstractArray{<:Integer}, iₗ::Int=firstindex
end

# Sort `A`, assuming no NaNs
function quicksort!(A, iₗ=firstindex(A), iᵤ=lastindex(A))
function quicksort!(A::TA, iₗ=firstindex(A), iᵤ=lastindex(A)) where {TA<:AbstractArray}
if issortedrange(A, iₗ, iᵤ)
# If already sorted, we're done here
return A
Expand Down Expand Up @@ -169,13 +169,14 @@ function quicksort!(A, iₗ=firstindex(A), iᵤ=lastindex(A))
iₚ = iₗ + Nₗ - 1
A[iₗ], A[iₚ] = A[iₚ], A[iₗ]
# Recurse: sort both upper and lower partitions
quicksort!(A, iₗ, iₚ)
quicksort!(A, iₚ+1, iᵤ)
quicksort!(A, iₗ, iₚ)::TA
quicksort!(A, iₚ+1, iᵤ)::TA
return A
end
end

# Argsort: sort A and permute I to match `A`, assuming no NaNs
function argsort!(I::AbstractArray, A::AbstractArray, iₗ::Int=firstindex(A), iᵤ::Int=lastindex(A))
function argsort!(I::TI, A::TA, iₗ::Int=firstindex(A), iᵤ::Int=lastindex(A)) where {TI<:AbstractArray, TA<:AbstractArray}
if issortedrange(A, iₗ, iᵤ)
# If already sorted, we're done here
return I, A
Expand Down Expand Up @@ -242,8 +243,9 @@ function argsort!(I::AbstractArray, A::AbstractArray, iₗ::Int=firstindex(A), i
A[iₗ], A[iₚ] = A[iₚ], A[iₗ]
I[iₗ], I[iₚ] = I[iₚ], I[iₗ]
# Recurse: sort both upper and lower partitions
argsort!(I, A, iₗ, iₚ)
argsort!(I, A, iₚ+1, iᵤ)
argsort!(I, A, iₗ, iₚ)::Tuple{TI, TA}
argsort!(I, A, iₚ+1, iᵤ)::Tuple{TI, TA}
return I, A
end
end

Expand Down

0 comments on commit c4d4be3

Please sign in to comment.