2018-06-11 21:50:54 +03:00
(definterface hash (Fn [(Ref a)] Int))
(defmodule String
2019-01-16 19:48:34 +03:00
(defn hash [k]
2018-06-11 21:50:54 +03:00
(let-do [a 31415
b 27183
vh 0]
(for [x 0 (length k)]
(do
2019-01-16 19:48:34 +03:00
(set! vh (+ (* a vh) (Char.to-int (char-at k x))))
2018-06-11 21:50:54 +03:00
(set! a (* a b))
(set! x (Int.inc x))))
(Int.abs vh)))
)
(defmodule Int
(defn hash [k] (the Int @k))
)
(defmodule Long
(defn hash [k] (to-int (the Long @k)))
)
(defmodule Bool
(defn hash [k] (if (the Bool @k) 1 0))
)
(defmodule Char
(defn hash [k] (to-int (the Char @k)))
)
(defmodule Float
(defn hash [k] (to-bytes @k))
)
(defmodule Double
(defn hash [k] (Long.to-int (to-bytes @k)))
)
2018-12-11 18:35:55 +03:00
(defmodule Pair
(defn hash [pair]
(let-do [code 17]
(set! code (+ (* 31 code) (hash (Pair.a pair))))
(set! code (+ (* 31 code) (hash (Pair.b pair))))
code))
)
2018-08-25 14:56:58 +03:00
(deftype (Bucket a b) [entries (Array (Pair a b))])
2018-06-11 21:50:54 +03:00
(defmodule Bucket
(defn empty []
(Bucket.init []))
2018-12-07 18:08:55 +03:00
(defn find [b k]
(let-do [ret -1
2018-06-11 22:23:34 +03:00
l (Array.length (Bucket.entries b))
2018-06-11 21:50:54 +03:00
es (entries b)]
(for [i 0 l]
2018-08-25 14:56:58 +03:00
(when (= (Pair.a (Array.nth es i)) k)
2018-06-11 21:50:54 +03:00
(do
2018-12-07 18:08:55 +03:00
(set! ret i)
2018-06-11 21:50:54 +03:00
(break))))
2018-12-07 18:08:55 +03:00
ret))
2018-12-07 18:45:19 +03:00
(defn get-idx [b i]
@(Pair.b (Array.nth (entries b) i)))
(defn set-idx [b i val]
(do (Array.aupdate! (entries &b) i &(fn [p] (Pair.set-b @p @val)))
b))
(defn push-back [b k v]
(do (Array.push-back! (entries &b) (Pair.init-from-refs k v))
b))
2018-12-18 15:57:02 +03:00
(defn get [b k default-value]
2018-12-07 18:45:19 +03:00
(let [i (find b k)]
(if (<= 0 i)
(get-idx b i)
2018-12-18 15:57:02 +03:00
@default-value)))
2018-12-03 21:45:23 +03:00
2019-02-11 22:24:17 +03:00
(defn get-maybe [b k]
(let [i (find b k)]
(if (<= 0 i)
2019-03-26 16:30:00 +03:00
;; The call to copy ('@') here is annoying - had to add it since sumtypes can't contain refs for now:
2019-03-26 12:23:27 +03:00
(Maybe.Just @(Pair.b (Array.nth (entries b) i)))
2019-02-11 22:24:17 +03:00
(Maybe.Nothing))))
2018-12-03 21:45:23 +03:00
(defn put [b k v]
2018-12-07 18:45:19 +03:00
(let [i (find &b k)]
(if (<= 0 i)
(set-idx b i v)
(push-back b k v))))
2018-06-11 21:50:54 +03:00
(defn contains? [b k]
2018-12-07 18:08:55 +03:00
(<= 0 (find b k)))
2018-06-11 21:50:54 +03:00
(defn remove [entries k]
2018-08-25 14:56:58 +03:00
(let-do [nentries (the (Array (Pair a b)) [])]
2018-06-11 22:23:34 +03:00
(for [i 0 (Array.length entries)]
(let [e (Array.nth entries i)]
2018-08-25 14:56:58 +03:00
(unless (= (Pair.a e) k)
2018-06-11 22:23:34 +03:00
(set! nentries (Array.push-back nentries @e)))))
2018-06-11 21:50:54 +03:00
nentries))
(defn shrink [b k]
2018-12-07 18:48:07 +03:00
(if (contains? &b k)
(let [nentries (remove (entries &b) k)]
(set-entries b nentries))
b))
2018-06-11 21:50:54 +03:00
)
(deftype (Map a b) [n-buckets Int buckets (Array (Bucket a b))])
(defmodule Map
2018-06-13 18:14:39 +03:00
(hidden dflt-len)
2018-06-11 21:50:54 +03:00
(def dflt-len 256)
2018-08-25 20:51:27 +03:00
(doc create "Create an empty map.")
2018-06-11 21:50:54 +03:00
(defn create []
2018-11-14 16:09:43 +03:00
(init dflt-len (Array.repeat dflt-len &Bucket.empty)))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc create-with-len "Create an empty map with a given number of buckets. High numbers reduce the possibility of hash collisions while increasing the memory footprint.")
2018-06-11 21:50:54 +03:00
(defn create-with-len [len]
2018-11-14 16:09:43 +03:00
(init len (Array.repeat len &Bucket.empty)))
2018-06-11 21:50:54 +03:00
2018-06-13 18:14:39 +03:00
(doc put "Put a a value v into map m, using the key k.")
2018-06-11 21:50:54 +03:00
(defn put [m k v]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
2018-11-14 16:21:12 +03:00
(update-buckets m &(fn [b]
2018-10-31 13:33:29 +03:00
(let [n (Array.nth &b idx)]
2018-12-07 18:45:19 +03:00
(Array.aset b idx (Bucket.put @n k v)))))))
2018-06-11 21:50:54 +03:00
2018-12-18 15:57:02 +03:00
(doc get-with-default "Get the value for the key k from map m. If it isn’ t found, the default is returned.")
(defn get-with-default [m k default-value]
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
(Bucket.get (Array.nth (buckets m) idx) k default-value)))
2018-08-25 20:51:27 +03:00
(doc get "Get the value for the key k from map m. If it isn’ t found, a zero element for the value type is returned.")
2018-06-11 21:50:54 +03:00
(defn get [m k]
2018-12-18 15:57:02 +03:00
(get-with-default m k &(zero)))
2018-06-11 21:50:54 +03:00
2019-02-11 22:24:17 +03:00
(doc get-maybe "Get the value for the key k from map m. It returns a Maybe type, meaning that if nothing is found, Nothing is returned.")
(defn get-maybe [m k]
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
(Bucket.get-maybe (Array.nth (buckets m) idx) k)))
2018-12-07 14:37:03 +03:00
(doc update "Update value at key k in map with function f, if it exists.")
(defn update [m k f]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(update-buckets m &(fn [b]
2018-12-07 18:52:35 +03:00
(let [n (Array.nth &b idx)
i (Bucket.find n k)]
(if (<= 0 i)
;; currently can't write a Bucket.update that takes f due to bug #347
(Array.aset b idx (Bucket.set-idx @n i &(f (Bucket.get-idx n i))))
2018-12-07 14:37:03 +03:00
b))))))
(doc update "Update value at key k in map with function f. If k doesn't exist in map, set k to (f v).")
(defn update-with-default [m k f v]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(update-buckets m &(fn [b]
2018-12-07 18:52:35 +03:00
(let [n (Array.nth &b idx)
i (Bucket.find n k)]
(if (<= 0 i)
(Array.aset b idx (Bucket.set-idx @n i &(f (Bucket.get-idx n i))))
(Array.aset b idx (Bucket.push-back @n k &(f v)))))))))
2018-12-07 14:37:03 +03:00
2018-06-13 18:14:39 +03:00
(doc length "Get the length of the map m.")
2018-06-11 21:50:54 +03:00
(defn length [m]
(let-do [c 0]
(for [i 0 @(n-buckets m)]
2018-06-11 22:23:34 +03:00
(set! c (+ c (Array.length (Bucket.entries (Array.nth (buckets m) i))))))
2018-06-11 21:50:54 +03:00
c))
2018-08-25 20:51:27 +03:00
(doc empty "Check whether the map m is empty.")
2018-06-11 21:50:54 +03:00
(defn empty? [m]
(= (length m) 0))
2018-06-13 18:14:39 +03:00
(doc contains? "Check whether the map m contains the key k.")
2018-06-11 21:50:54 +03:00
(defn contains? [m k]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
2018-06-11 22:23:34 +03:00
(Bucket.contains? (Array.nth (buckets m) idx) k)))
2018-06-11 21:50:54 +03:00
2018-06-13 18:14:39 +03:00
(doc remove "Remove the value under the key k from the map m.")
2018-06-11 21:50:54 +03:00
(defn remove [m k]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
2018-11-14 16:21:12 +03:00
(update-buckets m &(fn [b]
2018-10-31 13:33:29 +03:00
(let [n (Array.nth &b idx)]
2018-12-07 18:48:07 +03:00
(Array.aset b idx (Bucket.shrink @n k)))))))
2018-06-11 21:50:54 +03:00
2018-12-18 17:17:08 +03:00
(doc all? "Do all key-value pairs pass the given predicate (of two arguments)?")
(defn all? [pred m]
(let-do [ret true]
(for [i 0 @(n-buckets m)]
(let [bucket (Array.nth (buckets m) i)
len (Array.length (Bucket.entries bucket))
entries (Bucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(unless (~pred (Pair.a e) (Pair.b e))
(set! ret false))))))
ret))
(defn = [m1 m2]
(and (= (length m1) (length m2))
;; we could use contains? and get-with-default here to avoid requiring a (zero) for the value type
(all? &(fn [k v] (= v &(get m2 k))) m1)))
2018-08-25 20:51:27 +03:00
(doc for-each "Execute the binary function f for all keys and values in the map m.")
2018-06-11 21:50:54 +03:00
(defn for-each [m f]
(for [i 0 @(n-buckets m)]
2018-06-11 22:23:34 +03:00
(let [bucket (Array.nth (buckets m) i)
len (Array.length (Bucket.entries bucket))
2018-06-11 21:50:54 +03:00
entries (Bucket.entries bucket)]
(for [j 0 len]
2018-06-11 22:23:34 +03:00
(let [e (Array.nth entries j)]
2018-08-25 14:56:58 +03:00
(f (Pair.a e) (Pair.b e)))))))
2018-06-11 21:50:54 +03:00
2018-12-07 14:22:09 +03:00
(doc endo-map "Transform values of the given map in place. f gets two arguments, key and value, and should return new value")
(defn endo-map [f m]
(do
(for [i 0 @(n-buckets &m)]
(let [bucket (Array.nth (buckets &m) i)
len (Array.length (Bucket.entries bucket))
entries (Bucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(Array.aset! entries j (Pair.init @(Pair.a e)
(f (Pair.a e) (Pair.b e))))))))
m))
2018-12-07 14:11:05 +03:00
(doc kv-reduce "Reduce a map with a function of three arguments: state, key and value. Reduction order is not guaranteed.")
2018-12-07 13:55:27 +03:00
(defn kv-reduce [f init m]
(do
(for [i 0 @(n-buckets m)]
(let [bucket (Array.nth (buckets m) i)
len (Array.length (Bucket.entries bucket))
entries (Bucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(set! init (f init (Pair.a e) (Pair.b e)))))))
init))
2018-12-07 14:11:05 +03:00
(doc vals "Return an array of the values of the map. Order corresponds to order of (keys m)")
2018-12-07 14:04:18 +03:00
(defn vals [m]
(kv-reduce (fn [arr _ v] (Array.push-back arr @v))
[]
m))
2018-12-07 14:11:05 +03:00
(doc keys "Return an array of the keys of the map. Order corresponds to order of (vals m)")
2018-12-07 14:04:18 +03:00
(defn keys [m]
(kv-reduce (fn [arr k _] (Array.push-back arr @k))
[]
m))
2018-06-13 18:14:39 +03:00
(doc from-array "Create a map from the array a containing key-value pairs.")
2018-06-11 21:50:54 +03:00
(defn from-array [a]
(let-do [m (create)]
2018-06-11 22:23:34 +03:00
(for [i 0 (Array.length a)]
(let [e (Array.nth a i)
2018-06-11 21:50:54 +03:00
k (Pair.a e)
v (Pair.b e)]
2018-10-31 14:05:02 +03:00
(set! m (put m k v))))
2018-06-11 21:50:54 +03:00
m))
2018-08-06 21:47:09 +03:00
2018-12-07 22:24:36 +03:00
(doc to-array "Convert Map to Array of Pairs")
(defn to-array [m]
(kv-reduce (fn [arr k v] (Array.push-back arr (Pair.init-from-refs k v)))
[]
m))
2018-08-06 21:47:09 +03:00
(defn str [m]
2018-12-07 13:55:27 +03:00
(let [res (kv-reduce (fn [s k v]
(String.join @"" &[s @" " (prn @k) @" " (prn @v)]))
@"{"
m)]
2018-08-06 21:47:09 +03:00
(String.append &res " }")))
2018-06-11 21:50:54 +03:00
)
2018-08-25 15:18:37 +03:00
(deftype (SetBucket a) [entries (Array a)])
(defmodule SetBucket
(defn empty []
(SetBucket.init []))
(defn grow [b e]
(set-entries @b (Array.push-back @(entries b) e)))
(defn contains? [b k]
(let-do [e false
es (entries b)
l (Array.length es)]
(for [i 0 l]
(when (= (Array.nth es i) k)
(do
(set! e true)
(break))))
e))
(defn remove [entries k]
(let-do [nentries []]
(for [i 0 (Array.length entries)]
(let [e (Array.nth entries i)]
(unless (= e k)
(set! nentries (Array.push-back nentries @e)))))
nentries))
(defn shrink [b k]
(if (contains? b k)
(set-entries @b (remove (entries b) k))
@b))
)
(deftype (Set a) [n-buckets Int buckets (Array (SetBucket a))])
2018-06-11 21:50:54 +03:00
(defmodule Set
2018-08-25 20:51:27 +03:00
(hidden dflt-len)
2018-06-11 21:50:54 +03:00
(def dflt-len 256)
2018-08-25 20:51:27 +03:00
(doc create "Create an empty set.")
2018-06-11 21:50:54 +03:00
(defn create []
2018-11-14 16:09:43 +03:00
(init dflt-len (Array.repeat dflt-len &SetBucket.empty)))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc create-with-len "Create an empty set with a given number of buckets. Higher numbers decrease the probability of hash collisions while increasing the memory footprint.")
2018-06-11 21:50:54 +03:00
(defn create-with-len [len]
2018-11-14 16:09:43 +03:00
(init len (Array.repeat len &SetBucket.empty)))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc put "Put a a key k into the set s.")
(defn put [s k]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
2018-11-14 16:21:12 +03:00
(update-buckets s &(fn [b]
2018-10-31 13:33:29 +03:00
(let [n (Array.nth &b idx)]
2018-12-03 21:45:23 +03:00
(if (SetBucket.contains? n k)
b
(Array.aset b idx (SetBucket.grow n @k))))))))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc length "Get the length of set s.")
(defn length [s]
2018-06-11 21:50:54 +03:00
(let-do [c 0]
2018-08-25 20:51:27 +03:00
(for [i 0 @(n-buckets s)]
(set! c (+ c (Array.length (SetBucket.entries (Array.nth (buckets s) i))))))
2018-06-11 21:50:54 +03:00
c))
2018-08-25 20:51:27 +03:00
(doc empty? "Check whether the set s is empty.")
(defn empty? [s]
(= (length s) 0))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc contains? "Check whether the set s contains the key k.")
(defn contains? [s k]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets s))]
2018-08-25 20:51:27 +03:00
(SetBucket.contains? (Array.nth (buckets s) idx) k)))
2018-06-11 21:50:54 +03:00
2018-08-25 20:51:27 +03:00
(doc remove "Remove the key k from the set s.")
(defn remove [s k]
2018-12-01 12:05:11 +03:00
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
2018-11-14 16:21:12 +03:00
(update-buckets s &(fn [b]
2018-10-31 13:33:29 +03:00
(let [n (Array.nth &b idx)]
(Array.aset b idx (SetBucket.shrink n k)))))))
2018-06-11 21:50:54 +03:00
2018-12-18 16:58:58 +03:00
(doc all? "Does the predicate hold for all values in this set?")
(defn all? [pred set]
2018-12-12 13:50:05 +03:00
(let-do [ret true]
2018-12-18 16:58:58 +03:00
(foreach [bucket (buckets set)]
2018-12-12 13:50:05 +03:00
(foreach [e (SetBucket.entries bucket)]
2018-12-18 16:58:58 +03:00
(unless (~pred e)
2018-12-12 13:50:05 +03:00
(do
(set! ret false)
(break)))))
ret))
2018-12-18 16:58:58 +03:00
(doc subset? "Is set-a a subset of set-b?")
(defn subset? [set-a set-b]
(all? &(fn [e] (Set.contains? set-b e)) set-a))
2018-12-12 13:50:05 +03:00
(defn = [set-a set-b]
(and (= (Set.length set-a) (Set.length set-b))
(subset? set-a set-b)))
2018-08-25 20:51:27 +03:00
(doc for-each "Execute the unary function f for each element in the set s.")
(defn for-each [s f]
(for [i 0 @(n-buckets s)]
(let [bucket (Array.nth (buckets s) i)
2018-08-25 15:18:37 +03:00
len (Array.length (SetBucket.entries bucket))
entries (SetBucket.entries bucket)]
2018-06-11 21:50:54 +03:00
(for [j 0 len]
2018-06-11 22:23:34 +03:00
(let [e (Array.nth entries j)]
2018-08-25 15:18:37 +03:00
(f e))))))
2018-06-11 21:50:54 +03:00
2018-06-13 18:14:39 +03:00
(doc from-array "Create a set from the values in array a.")
2018-06-11 21:50:54 +03:00
(defn from-array [a]
2018-08-25 20:51:27 +03:00
(let-do [s (create)]
2018-06-11 22:23:34 +03:00
(for [i 0 (Array.length a)]
(let [e (Array.nth a i)]
2018-10-31 14:05:02 +03:00
(set! s (put s e))))
2018-08-25 20:51:27 +03:00
s))
2018-08-06 21:55:41 +03:00
2018-12-07 14:15:23 +03:00
(doc reduce "Reduce values of the set with function f. Order of reduction is not guaranteed")
(defn reduce [f init s]
(do
2018-08-25 20:51:27 +03:00
(for [i 0 @(n-buckets s)]
(let [bucket (Array.nth (buckets s) i)
2018-08-25 15:18:37 +03:00
len (Array.length (SetBucket.entries bucket))
entries (SetBucket.entries bucket)]
2018-08-06 21:55:41 +03:00
(for [j 0 len]
(let [e (Array.nth entries j)]
2018-12-07 14:15:23 +03:00
(set! init (f init e))))))
init))
2018-12-12 13:50:05 +03:00
(doc intersection "Set of elements that are in both set-a and set-b")
(defn intersection [set-a set-b]
(reduce (fn [s a] (if (Set.contains? set-b a) (Set.put s a) s))
(Set.create)
set-a))
(doc union "Set of elements that are in either set-a or set-b (or both)")
(defn union [set-a set-b]
(reduce Set.put
@set-a
set-b))
(doc difference "Set of elements that are in set-a but not set-b")
(defn difference [set-a set-b]
(reduce Set.remove
@set-a
set-b))
2018-12-07 22:24:36 +03:00
(doc to-array "Convert Set to Array of elements")
(defn to-array [s]
(reduce (fn [arr elt] (Array.push-back arr @elt)) [] s))
2018-12-07 14:15:23 +03:00
(defn str [set]
(let [res (reduce (fn [s e] (String.join @"" &[s @" " (prn e)]))
@"{"
set)]
2018-08-06 21:55:41 +03:00
(String.append &res " }")))
2018-06-11 21:50:54 +03:00
)