(a: Expression, b: Expression)
| 123 | if (aLex < bLex) return -1; |
| 124 | if (aLex > bLex) return +1; |
| 125 | } |
| 126 | return order(a, b); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sort terms of a sum with `addOrder` semantics, computing each term's |
| 131 | * degree/lexicographic keys once instead of on every comparison (`addOrder` |
| 132 | * re-walks both expressions per comparison — O(n log n) walks per sort). |
| 133 | * The comparator chain below returns exactly the values `addOrder` would, |
| 134 | * and `Array.sort` is stable, so the result order is identical. |
| 135 | */ |
| 136 | export function sortAddTerms( |
| 137 | xs: ReadonlyArray<Expression> |
| 138 | ): ReadonlyArray<Expression> { |
| 139 | if (xs.length <= 1) return [...xs]; |
| 140 | const keyed = xs.map((x) => ({ |
| 141 | x, |
| 142 | total: totalDegree(x), |
| 143 | max: maxDegree(x), |
| 144 | lex: revlex(x), |
| 145 | })); |
| 146 | keyed.sort((a, b) => { |
| 147 | if (a.total !== b.total) return b.total - a.total; |
| 148 | if (a.max !== b.max) return b.max - a.max; |
| 149 | if (a.lex || b.lex) { |
| 150 | if (!a.lex) return +1; |
| 151 | if (!b.lex) return -1; |
| 152 | if (a.lex < b.lex) return -1; |
| 153 | if (a.lex > b.lex) return +1; |
| 154 | } |
| 155 | return order(a.x, b.x); |
| 156 | }); |
| 157 | return keyed.map((k) => k.x); |
nothing calls this directly
no test coverage detected