Skip to content

sort: avoid use of ReverseOrdering for BitonicSort #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/sort.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Base.Order: Forward, Ordering, Perm, ReverseOrdering, ord
import Base.Order: Forward, Ordering, Perm, ord
import Base.Sort: Algorithm, lt, sort, sortperm


Expand Down Expand Up @@ -51,8 +51,7 @@ _sort(a::NTuple, alg, order) = sort!(Base.copymutable(a); alg=alg, order=order)
function swap_expr(i, j, rev)
ai = Symbol('a', i)
aj = Symbol('a', j)
order = rev ? :revorder : :order
return :( ($ai, $aj) = @inbounds lt($order, $ai, $aj) ? ($ai, $aj) : ($aj, $ai) )
return :( ($ai, $aj) = @inbounds lt(order, $ai, $aj) ⊻ $rev ? ($ai, $aj) : ($aj, $ai) )
end

function merge_exprs(idx, rev)
Expand Down Expand Up @@ -83,7 +82,6 @@ _sort(a::NTuple, alg, order) = sort!(Base.copymutable(a); alg=alg, order=order)
symlist = (Symbol('a', i) for i in idx)
return quote
@_inline_meta
revorder = Base.Order.ReverseOrdering(order)
($(symlist...),) = a
($(sort_exprs(idx)...);)
return ($(symlist...),)
Expand Down
3 changes: 3 additions & 0 deletions test/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ using StaticArrays, Test
vp = sortperm(v)
@test v[vp] == sort(v)
end

# stability
@test sortperm(SA[1, 1, 1, 0]) == SA[4, 1, 2, 3]
end

end