From d4d2cc244b3b20413d3681f28f6f55d4f403e361 Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Sat, 2 Jun 2012 13:08:55 +0200 Subject: [PATCH] hash function seems to be stable now --- src/NoteHub/crossover/lib.clj | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/NoteHub/crossover/lib.clj b/src/NoteHub/crossover/lib.clj index 0187005..95fb5bc 100644 --- a/src/NoteHub/crossover/lib.clj +++ b/src/NoteHub/crossover/lib.clj @@ -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)))))