Browse Source

hash function seems to be stable now

master
Christian Mueller 14 years ago
parent
commit
d4d2cc244b
  1. 13
      src/NoteHub/crossover/lib.clj

13
src/NoteHub/crossover/lib.clj

@ -5,13 +5,12 @@ @@ -5,13 +5,12 @@
; (doesn't work for UTF-16!)
(defn hash [f s]
(let [short-mod #(mod % 32767)
; we cannot use Math/pow because it's imprecise
; and differs on JVM and JS
pow (fn [n e]
(reduce #(short-mod (* % %2)) 1
(repeat e n)))
char-codes (map #(f (str %)) s)]
char-codes (map f
(filter #(not (contains? #{"\n" "\r"} %)) (map str s)))]
(reduce
#(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)))))

Loading…
Cancel
Save