Skip to content

Commit

Permalink
* add hash-long
Browse files Browse the repository at this point in the history
* add hash-double
* reported test case
  • Loading branch information
swannodette committed Jan 22, 2024
1 parent 22e3dc4 commit 53a57b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,16 @@
h1 (m3-mix-H1 m3-seed k1)]
(m3-fmix h1 4))))

(defn hash-long [high low]
(bit-xor high low))

(defn hash-double [f]
(let [arr (doto (js/Float64Array. 1) (aset 0 f))
buf (.-buffer arr)
low (.getInt32 (js/DataView. buf 0 4))
high (.getInt32 (js/DataView. buf 4 4))]
(hash-long high low)))

(defn ^number m3-hash-unencoded-chars [in]
(let [h1 (loop [i 1 h1 m3-seed]
(if (< i (.-length in))
Expand Down Expand Up @@ -1021,7 +1031,9 @@

(number? o)
(if ^boolean (js/isFinite o)
(js-mod (Math/floor o) 2147483647)
(if-not (.isInteger js/Number o)
(hash-double o)
(js-mod (Math/floor o) 2147483647))
(case o
##Inf
2146435072
Expand Down
5 changes: 5 additions & 0 deletions src/test/cljs/cljs/hashing_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@
(deftest test-cljs-1818
(is (= (hash true) 1231))
(is (= (hash false) 1237)))

(deftest test-cljs-3410
(testing "Small floats should not hash the same"
(is (not= (hash-double -0.32553251) (hash-double -0.0000032553251)))
(is (not= (hash -0.32553251) (hash -0.0000032553251)))))

0 comments on commit 53a57b6

Please sign in to comment.