Skip to content

Commit 00cf44b

Browse files
sostockmichikawa07
andauthored
Faster sortexp (#575)
Co-authored-by: michikawa07 <[email protected]>
1 parent 9e329e8 commit 00cf44b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/display.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function show(io::IO, x::Unitlike)
170170
showoperators = get(io, :showoperators, false)
171171
first = ""
172172
sep = showoperators ? "*" : " "
173-
foreach(sortexp(typeof(x).parameters[1])) do y
173+
foreach(sortexp(x)) do y
174174
print(io,first)
175175
showrep(io,y)
176176
first = sep
@@ -182,10 +182,9 @@ end
182182
sortexp(xs)
183183
Sort units to show positive exponents first.
184184
"""
185-
function sortexp(xs)
186-
vcat([x for x in xs if power(x) >= 0],
187-
[x for x in xs if power(x) < 0])
188-
end
185+
sortexp(xs) = sort!(collect(xs), by = u->signbit(power(u)), alg = InsertionSort)
186+
@generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,)
187+
@generated sortexp(::Units{U}) where U = (sortexp(U)...,)
189188

190189
"""
191190
showrep(io::IO, x::Unit)

0 commit comments

Comments
 (0)