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 30bc06f
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/Sorting/quicksort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,14 @@ argsortnans!(I::AbstractArray, A::AbstractArray{<:Integer}, iₗ::Int=firstindex
end

# Sort `A`, assuming no NaNs
function quicksort!(A, iₗ=firstindex(A), iᵤ=lastindex(A))
if issortedrange(A, iₗ, iᵤ)
# If already sorted, we're done here
return A
end
function quicksort!(A::TA, iₗ=firstindex(A), iᵤ=lastindex(A)) where {TA<:AbstractArray}
# If already sorted, we're done here
issortedrange(A, iₗ, iᵤ) && return A::TA

# Otherwise, we have to sort
N = iᵤ - iₗ + 1
if isantisortedrange(A, iₗ, iᵤ)
vreverse!(A, iₗ, iᵤ)
return A
elseif N == 3
# We know we are neither sorted nor antisorted, so only four possibilities remain
iₘ = iₗ + 1
Expand All @@ -137,7 +135,6 @@ function quicksort!(A, iₗ=firstindex(A), iᵤ=lastindex(A))
A[iₗ], A[iₘ], A[iᵤ] = b, c, a # b ≤ c ≤ a
end
end
return A
else
# Pick a pivot for partitioning
iₚ = iₗ + (N >> 2)
Expand Down Expand Up @@ -169,23 +166,22 @@ 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
end
return A::TA
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))
if issortedrange(A, iₗ, iᵤ)
# If already sorted, we're done here
return I, A
end
function argsort!(I::TI, A::TA, iₗ::Int=firstindex(A), iᵤ::Int=lastindex(A)) where {TI<:AbstractArray, TA<:AbstractArray}
# If already sorted, we're done here
issortedrange(A, iₗ, iᵤ) && return (I, A)::Tuple{TI, TA}

# Otherwise, we have to sort
N = iᵤ - iₗ + 1
if isantisortedrange(A, iₗ, iᵤ)
vreverse!(A, iₗ, iᵤ)
vreverse!(I, iₗ, iᵤ)
return I, A
elseif N == 3
# We know we are neither sorted nor antisorted, so only four possibilities remain
iₘ = iₗ + 1
Expand All @@ -207,7 +203,6 @@ function argsort!(I::AbstractArray, A::AbstractArray, iₗ::Int=firstindex(A), i
I[iₗ], I[iₘ], I[iᵤ] = I[iₘ], I[iᵤ], I[iₗ]
end
end
return I, A
else
# Pick a pivot for partitioning
iₚ = iₗ + (N >> 2)
Expand Down Expand Up @@ -242,21 +237,20 @@ 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}
end
return (I, A)::Tuple{TI, TA}
end

function nansort!(A)
iₗ, iᵤ =firstindex(A), lastindex(A)
A, iₗ, iᵤ = sortnans!(A, iₗ, iᵤ)
@inline function nansort!(A)
A, iₗ, iᵤ = sortnans!(A)
quicksort!(A, iₗ, iᵤ)
return A
end
export nansort!
function nanargsort!(I, A)
iₗ, iᵤ =firstindex(A), lastindex(A)
I, A, iₗ, iᵤ = argsortnans!(I, A, iₗ, iᵤ)
@inline function nanargsort!(I, A)
I, A, iₗ, iᵤ = argsortnans!(I, A)
argsort!(I, A, iₗ, iᵤ)
return I, A
end
Expand Down

2 comments on commit 30bc06f

@brenhinkeller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register
Release notes:

  • Fix early return for empty ranges in argsortnans!
  • Potential improvements to type stability in nansort!/nanargsort!

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/122112

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.45 -m "<description of version>" 30bc06f2bac512fa893f05e46b7e291c4ce9e7d1
git push origin v0.6.45

Please sign in to comment.