1
0
mirror of https://github.com/janet-lang/janet synced 2024-09-29 15:30:41 +00:00

Merge commit '0ea77cabfb30afc15433581f5888171c1f65aafd'

This commit is contained in:
Calvin Rose 2020-12-28 12:17:13 -06:00
commit 20bcd95279
2 changed files with 40 additions and 30 deletions

View File

@ -795,11 +795,27 @@
a a
(if (not= (> b a) (> b c)) b c))) (if (not= (> b a) (> b c)) b c)))
(defn sort-help [a lo hi by] (defn- insertion-sort [a lo hi by]
(for i (+ lo 1) (+ hi 1)
(def temp (in a i))
(var j (- i 1))
(while (and (>= j lo) (by temp (in a j)))
(set (a (+ j 1)) (in a j))
(-- j))
(set (a (+ j 1)) temp))
a)
(defn sort
"Sort an array in-place. Uses quick-sort and is not a stable sort."
[a &opt by]
(default by <)
(def stack @[[0 (- (length a) 1)]])
(while (not (empty? stack))
(def [lo hi] (array/pop stack))
(when (< lo hi) (when (< lo hi)
(def pivot (when (< (- hi lo) 32) (insertion-sort a lo hi by) (break))
(median-of-three (in a hi) (in a lo) (def pivot (median-of-three (in a hi) (in a lo) (in a (math/floor (/ (+ lo hi) 2)))))
(in a (math/floor (/ (+ lo hi) 2)))))
(var left lo) (var left lo)
(var right hi) (var right hi)
(while true (while true
@ -812,17 +828,12 @@
(++ left) (++ left)
(-- right)) (-- right))
(if (>= left right) (break))) (if (>= left right) (break)))
(sort-help a lo right by) (array/push stack [lo right])
(sort-help a left hi by)) (array/push stack [left hi])))
a) a)
(defn sort
"Sort an array in-place. Uses quick-sort and is not a stable sort."
[a &opt by]
(sort-help a 0 (- (length a) 1) (or by <)))
(undef median-of-three) (undef median-of-three)
(undef sort-help) (undef insertion-sort)
(defn sort-by (defn sort-by
`Returns a new sorted array that compares elements by invoking `Returns a new sorted array that compares elements by invoking

View File

@ -268,14 +268,13 @@ int32_t janet_hash(Janet x) {
if (isnan(num) || isinf(num) || num == 0) { if (isnan(num) || isinf(num) || num == 0) {
hash = 0; hash = 0;
} else { } else {
int e = 0; hash = (int32_t)num;
double rest = frexp(num, &e); hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
int64_t intrest = (int64_t)(rest * JANET_INTMAX_DOUBLE); hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
uint32_t accum = (uint32_t)((intrest >> 22) ^ intrest); hash = (hash >> 16) ^ hash;
accum ^= 0x9e3779b9 + (accum << 3) + (accum >> 2);
accum += (int32_t)(e); uint32_t lo = (uint32_t)(janet_u64(x) & 0xFFFFFFFF);
accum ^= 0x9e3779b9 + (accum << 3) + (accum >> 2); hash ^= lo + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash = (int32_t) accum;
} }
break; break;
} }