Skip to content

Commit

Permalink
Faster sortexp (#575)
Browse files Browse the repository at this point in the history
Co-authored-by: michikawa07 <michikawa.ryohei@gmail.com>
  • Loading branch information
sostock and michikawa07 authored Feb 28, 2023
1 parent 9e329e8 commit 00cf44b
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function show(io::IO, x::Unitlike)
showoperators = get(io, :showoperators, false)
first = ""
sep = showoperators ? "*" : " "
foreach(sortexp(typeof(x).parameters[1])) do y
foreach(sortexp(x)) do y
print(io,first)
showrep(io,y)
first = sep
Expand All @@ -182,10 +182,9 @@ end
sortexp(xs)
Sort units to show positive exponents first.
"""
function sortexp(xs)
vcat([x for x in xs if power(x) >= 0],
[x for x in xs if power(x) < 0])
end
sortexp(xs) = sort!(collect(xs), by = u->signbit(power(u)), alg = InsertionSort)
@generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,)
@generated sortexp(::Units{U}) where U = (sortexp(U)...,)

"""
showrep(io::IO, x::Unit)
Expand Down

0 comments on commit 00cf44b

Please sign in to comment.