hash function seems to be stable now
This commit is contained in:
@@ -5,13 +5,12 @@
|
|||||||
; (doesn't work for UTF-16!)
|
; (doesn't work for UTF-16!)
|
||||||
(defn hash [f s]
|
(defn hash [f s]
|
||||||
(let [short-mod #(mod % 32767)
|
(let [short-mod #(mod % 32767)
|
||||||
; we cannot use Math/pow because it's imprecise
|
char-codes (map f
|
||||||
; and differs on JVM and JS
|
(filter #(not (contains? #{"\n" "\r"} %)) (map str s)))]
|
||||||
pow (fn [n e]
|
|
||||||
(reduce #(short-mod (* % %2)) 1
|
|
||||||
(repeat e n)))
|
|
||||||
char-codes (map #(f (str %)) s)]
|
|
||||||
(reduce
|
(reduce
|
||||||
#(short-mod (+ %
|
#(short-mod (+ %
|
||||||
(short-mod (* (first %2) (pow 31 (second %2))))))
|
(short-mod (* (first %2)
|
||||||
|
((if (odd? %)
|
||||||
|
bit-xor
|
||||||
|
bit-and) 16381 (second %2))))))
|
||||||
0 (map list char-codes (range)))))
|
0 (map list char-codes (range)))))
|
||||||
|
|||||||
Reference in New Issue
Block a user