1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 23:40:27 +00:00

special case common sort usages

This commit is contained in:
primo-ppcg 2023-08-15 11:15:38 +07:00
parent 8df7364319
commit cdd7083c86

View File

@ -805,21 +805,31 @@
###
###
(defn- median-of-three [a b c]
(if (not= (> a b) (> a c))
a
(if (not= (> b a) (> b c)) b c)))
(defmacro- median-of-three
[x y z]
~(if (<= ,x ,y)
(if (<= ,y ,z) ,y (if (<= ,z ,x) ,x ,z))
(if (<= ,z ,y) ,y (if (<= ,x ,z) ,x ,z))))
(defmacro- sort-partition-template
[ind before? left right pivot]
~(do
(while (,before? (in ,ind ,left) ,pivot) (++ ,left))
(while (,before? ,pivot (in ,ind ,right)) (-- ,right))))
(defn- sort-help [a lo hi before?]
(when (< lo hi)
(def pivot
(median-of-three (in a hi) (in a lo)
(in a (math/floor (/ (+ lo hi) 2)))))
(def [x y z] [(in a lo)
(in a (div (+ lo hi) 2))
(in a hi)])
(def pivot (median-of-three x y z))
(var left lo)
(var right hi)
(while true
(while (before? (in a left) pivot) (++ left))
(while (before? pivot (in a right)) (-- right))
(case before?
< (sort-partition-template a < left right pivot)
> (sort-partition-template a > left right pivot)
(sort-partition-template a before? left right pivot))
(when (<= left right)
(def tmp (in a left))
(set (a left) (in a right))
@ -827,8 +837,10 @@
(++ left)
(-- right))
(if (>= left right) (break)))
(sort-help a lo right before?)
(sort-help a left hi before?))
(if (< lo right)
(sort-help a lo right before?))
(if (< left hi)
(sort-help a left hi before?)))
a)
(defn sort
@ -836,7 +848,8 @@
If a `before?` comparator function is provided, sorts elements using that,
otherwise uses `<`.``
[ind &opt before?]
(sort-help ind 0 (- (length ind) 1) (or before? <)))
(default before? <)
(sort-help ind 0 (- (length ind) 1) before?))
(defn sort-by
``Sorts `ind` in-place by calling a function `f` on each element and