|
1 | 1 | (ns ^{:no-doc true |
2 | 2 | :author "Nikita Prokopov"} |
3 | 3 | datascript.btset |
| 4 | + (:require |
| 5 | + [datascript.arrays :as da]) |
4 | 6 | (:import |
5 | | - [java.util Comparator] |
6 | | - [datascript SortedSet])) |
| 7 | + [java.util Comparator Arrays] |
| 8 | + [datascript SortedSet SortedSet$Leaf SortedSet$Node SortedSet$Edit Stitch])) |
| 9 | + |
7 | 10 |
|
8 | 11 | (defn btset-conj [set key cmp] |
9 | 12 | (.cons ^SortedSet set key cmp)) |
10 | 13 |
|
| 14 | + |
11 | 15 | (defn btset-disj [set key cmp] |
12 | 16 | (.disjoin ^SortedSet set key cmp)) |
13 | 17 |
|
| 18 | + |
14 | 19 | (defn slice |
15 | 20 | "`(slice set key)` returns iterator that contains all elements equal to the key. |
16 | 21 | `(slice set from to)` returns iterator for all Xs where from <= X <= to. |
|
22 | 27 | ([set from to cmp] |
23 | 28 | (.slice ^SortedSet set from to ^Comparator cmp))) |
24 | 29 |
|
| 30 | + |
25 | 31 | (defn rslice |
26 | 32 | "`(rslice set from to)` returns backwards iterator for all Xs where from <= X <= to. |
27 | 33 | `(rslice set from nil)` returns backwards iterator for all Xs where X <= from." |
|
30 | 36 | ([set from to cmp] |
31 | 37 | (.rslice ^SortedSet set from to ^Comparator cmp))) |
32 | 38 |
|
| 39 | + |
| 40 | +(defn- array-from-indexed [coll type from to] |
| 41 | + (cond |
| 42 | + (instance? clojure.lang.Indexed coll) |
| 43 | + (Stitch/indexedToArray type coll from to) |
| 44 | + |
| 45 | + (da/array? coll) |
| 46 | + (Arrays/copyOfRange coll from to (da/array-type type)))) |
| 47 | + |
| 48 | + |
| 49 | +(defn- split |
| 50 | + ([coll to type avg max] |
| 51 | + (persistent! (split (transient []) 0 coll to type avg max))) |
| 52 | + ([res from coll to type avg max] |
| 53 | + (let [len (- to from)] |
| 54 | + (cond |
| 55 | + (== 0 len) |
| 56 | + res |
| 57 | + |
| 58 | + (>= len (* 2 avg)) |
| 59 | + (recur (conj! res (array-from-indexed coll type from (+ from avg))) (+ from avg) coll to type avg max) |
| 60 | + |
| 61 | + (<= len max) |
| 62 | + (conj! res (array-from-indexed coll type from to)) |
| 63 | + |
| 64 | + :else |
| 65 | + (-> res |
| 66 | + (conj! (array-from-indexed coll type from (+ from (quot len 2)))) |
| 67 | + (conj! (array-from-indexed coll type (+ from (quot len 2)) to))))))) |
| 68 | + |
| 69 | + |
| 70 | +(defn from-sorted-array |
| 71 | + ([cmp keys] |
| 72 | + (from-sorted-array cmp keys (da/alength keys))) |
| 73 | + ([cmp keys len] |
| 74 | + (let [max SortedSet/MAX_LEN |
| 75 | + avg (quot (+ SortedSet/MIN_LEN max) 2) |
| 76 | + edit (SortedSet$Edit. false) |
| 77 | + ->Leaf (fn [keys] |
| 78 | + (SortedSet$Leaf. keys (count keys) edit)) |
| 79 | + ->Node (fn [children] |
| 80 | + (SortedSet$Node. |
| 81 | + (da/amap #(.maxKey ^SortedSet$Leaf %) Object children) |
| 82 | + children (count children) edit))] |
| 83 | + (loop [nodes (mapv ->Leaf (split keys len Object avg max))] |
| 84 | + (case (count nodes) |
| 85 | + 0 (SortedSet. cmp) |
| 86 | + 1 (SortedSet. {} cmp (first nodes) len edit) |
| 87 | + (recur (mapv ->Node (split nodes (count nodes) SortedSet$Leaf avg max)))))))) |
| 88 | + |
| 89 | + |
| 90 | +(defn ctor [^Comparator cmp keys] |
| 91 | + (let [arr (to-array keys) |
| 92 | + _ (da/asort arr cmp) |
| 93 | + len (Stitch/distinct cmp arr)] |
| 94 | + (from-sorted-array cmp arr len))) |
| 95 | + |
| 96 | + |
33 | 97 | (defn btset-by |
34 | 98 | ([cmp] (SortedSet. ^Comparator cmp)) |
35 | | - ([cmp & keys] |
36 | | - (into (btset-by cmp) keys))) |
| 99 | + ([cmp & keys] (ctor cmp keys))) |
| 100 | + |
37 | 101 |
|
38 | 102 | (defn btset |
39 | 103 | ([] (SortedSet.)) |
40 | | - ([& keys] |
41 | | - (into (btset) keys))) |
| 104 | + ([& keys] (ctor compare keys))) |
0 commit comments