Skip to content

Commit

Permalink
* update hash to handle bigint
Browse files Browse the repository at this point in the history
* make number and bigint -equiv
* handle bigint & number in PAM
* minor refactor of private PAM lookup helper fn names
  • Loading branch information
swannodette committed Sep 26, 2023
1 parent 5477284 commit 91e8ee4
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,10 @@
h
(add-to-string-hash-cache k)))))

(defn- safe-value? [n]
(and (<= n js/Number.MAX_SAFE_INTEGER)
(>= n js/Number.MIN_SAFE_INTEGER)))

(defn hash
"Returns the hash code of its argument. Note this is the hash code
consistent with =."
Expand All @@ -1020,6 +1024,11 @@
(implements? IHash o)
(bit-xor (-hash o) 0)

(cljs.core/bigint? o)
(if (safe-value? o)
(hash (js/Number. o))
(hash-string (.toString o 32)))

(number? o)
(if ^boolean (js/isFinite o)
(js-mod (Math/floor o) 2147483647)
Expand Down Expand Up @@ -1434,7 +1443,17 @@

(extend-type number
IEquiv
(-equiv [x o] (identical? x o)))
(-equiv [x o]
(if (cljs.core/bigint? o)
(cljs.core/coercive-= x o)
(identical? x o))))

(extend-type bigint
IEquiv
(-equiv [x o]
(if (cljs.core/js-number? o)
(cljs.core/coercive-= x o)
(identical? x o))))

(declare with-meta)

Expand Down Expand Up @@ -6687,15 +6706,15 @@ reduces them without incurring seq initialization"

;;; PersistentArrayMap

(defn- array-index-of-nil? [arr]
(defn- array-index-of-nil [arr]
(let [len (alength arr)]
(loop [i 0]
(cond
(<= len i) -1
(nil? (aget arr i)) i
:else (recur (+ i 2))))))

(defn- array-index-of-keyword? [arr k]
(defn- array-index-of-keyword [arr k]
(let [len (alength arr)
kstr (.-fqn k)]
(loop [i 0]
Expand All @@ -6705,7 +6724,7 @@ reduces them without incurring seq initialization"
(identical? kstr (.-fqn (aget arr i)))) i
:else (recur (+ i 2))))))

(defn- array-index-of-symbol? [arr k]
(defn- array-index-of-symbol [arr k]
(let [len (alength arr)
kstr (.-str k)]
(loop [i 0]
Expand All @@ -6715,6 +6734,17 @@ reduces them without incurring seq initialization"
(identical? kstr (.-str (aget arr i)))) i
:else (recur (+ i 2))))))

(defn- equal-number? [x y]
(and (number? x) (number? y) (cljs.core/coercive-= x y)))

(defn- array-index-of-number [arr k]
(let [len (alength arr)]
(loop [i 0]
(cond
(<= len i) -1
(equal-number? k (aget arr i)) i
:else (recur (+ i 2))))))

(defn- array-index-of-identical? [arr k]
(let [len (alength arr)]
(loop [i 0]
Expand All @@ -6723,7 +6753,7 @@ reduces them without incurring seq initialization"
(identical? k (aget arr i)) i
:else (recur (+ i 2))))))

(defn- array-index-of-equiv? [arr k]
(defn- array-index-of-equiv [arr k]
(let [len (alength arr)]
(loop [i 0]
(cond
Expand All @@ -6733,17 +6763,20 @@ reduces them without incurring seq initialization"

(defn array-index-of [arr k]
(cond
(keyword? k) (array-index-of-keyword? arr k)
(keyword? k) (array-index-of-keyword arr k)

(or (string? k) (number? k))
(string? k)
(array-index-of-identical? arr k)

(symbol? k) (array-index-of-symbol? arr k)
(number? k)
(array-index-of-number arr k)

(symbol? k) (array-index-of-symbol arr k)

(nil? k)
(array-index-of-nil? arr)
(array-index-of-nil arr)

:else (array-index-of-equiv? arr k)))
:else (array-index-of-equiv arr k)))

(defn- array-map-index-of [m k]
(array-index-of (.-arr m) k))
Expand Down

0 comments on commit 91e8ee4

Please sign in to comment.