Guest User

Untitled

a guest
Nov 6th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn stack-hash
  2.     "Creates a hash of in which fits in size with minimal rehashing given various sizes. Optional n argument can be specified if it has been precomputed"
  3.     [in size]
  4.     (let [n (Math/ceil (/ (Math/log size) (Math/log 2)))
  5.           mask (int (- (Math/pow 2 n) 1))]
  6.         (loop [in in]
  7.             (let [Hv (loop [i 0
  8.                             h 0]
  9.                         (if (> (count in) i)
  10.                             (recur  (+ i 1)
  11.                                 (mod (+ (* h 31) (int (.charAt in i))) Integer/MAX_VALUE))
  12.                         h))
  13.                   first-four (bit-and Hv mask)]
  14.                 (if (< first-four size)
  15.                     first-four
  16.                     (recur (str first-four)))))
  17.     ))
Advertisement
Add Comment
Please, Sign In to add comment