|
546 | 546 | "Returns a new collection of coll with a mapping from key k to |
547 | 547 | value v added to it.")) |
548 | 548 |
|
| 549 | +(defprotocol IFind |
| 550 | + "Protocol for implementing entry finding in collections." |
| 551 | + (-find [coll k])) |
| 552 | + |
549 | 553 | (defprotocol IMap |
550 | 554 | "Protocol for adding mapping functionality to collections." |
551 | 555 | #_(-assoc-ex [coll k v]) |
@@ -2023,6 +2027,10 @@ reduces them without incurring seq initialization" |
2023 | 2027 | "Returns true if coll implements Associative" |
2024 | 2028 | [x] (satisfies? IAssociative x)) |
2025 | 2029 |
|
| 2030 | +(defn ^boolean ifind? |
| 2031 | + "Returns true if coll implements IFind" |
| 2032 | + [x] (satisfies? IFind x)) |
| 2033 | + |
2026 | 2034 | (defn ^boolean sequential? |
2027 | 2035 | "Returns true if coll satisfies ISequential" |
2028 | 2036 | [x] (satisfies? ISequential x)) |
@@ -2237,7 +2245,9 @@ reduces them without incurring seq initialization" |
2237 | 2245 | (when (and (not (nil? coll)) |
2238 | 2246 | (associative? coll) |
2239 | 2247 | (contains? coll k)) |
2240 | | - [k (get coll k)])) |
| 2248 | + (if (ifind? coll) |
| 2249 | + (-find coll k) |
| 2250 | + [k (get coll k)]))) |
2241 | 2251 |
|
2242 | 2252 | (defn ^boolean distinct? |
2243 | 2253 | "Returns true if no two of the arguments are =" |
@@ -5152,6 +5162,10 @@ reduces them without incurring seq initialization" |
5152 | 5162 | (-assoc-n coll k v) |
5153 | 5163 | (throw (js/Error. "Vector's key for assoc must be a number.")))) |
5154 | 5164 |
|
| 5165 | + IFind |
| 5166 | + (-find [coll k] |
| 5167 | + [k (get coll k)]) |
| 5168 | + |
5155 | 5169 | IVector |
5156 | 5170 | (-assoc-n [coll n val] |
5157 | 5171 | (cond |
@@ -5438,6 +5452,10 @@ reduces them without incurring seq initialization" |
5438 | 5452 | (-assoc-n coll key val) |
5439 | 5453 | (throw (js/Error. "Subvec's key for assoc must be a number.")))) |
5440 | 5454 |
|
| 5455 | + IFind |
| 5456 | + (-find [coll key] |
| 5457 | + [key (get coll key)]) |
| 5458 | + |
5441 | 5459 | IVector |
5442 | 5460 | (-assoc-n [coll n val] |
5443 | 5461 | (let [v-pos (+ start n)] |
@@ -5954,6 +5972,10 @@ reduces them without incurring seq initialization" |
5954 | 5972 | true |
5955 | 5973 | false)) |
5956 | 5974 |
|
| 5975 | + IFind |
| 5976 | + (-find [coll k] |
| 5977 | + [k (get coll k)]) |
| 5978 | + |
5957 | 5979 | IKVReduce |
5958 | 5980 | (-kv-reduce [coll f init] |
5959 | 5981 | (let [len (alength keys)] |
@@ -6126,7 +6148,7 @@ reduces them without incurring seq initialization" |
6126 | 6148 | (-lastIndexOf coll x (count coll))) |
6127 | 6149 | (lastIndexOf [coll x start] |
6128 | 6150 | (-lastIndexOf coll x start)) |
6129 | | - |
| 6151 | + |
6130 | 6152 | IMeta |
6131 | 6153 | (-meta [coll] _meta) |
6132 | 6154 |
|
@@ -6154,7 +6176,7 @@ reduces them without incurring seq initialization" |
6154 | 6176 |
|
6155 | 6177 | IHash |
6156 | 6178 | (-hash [coll] (hash-ordered-coll coll)) |
6157 | | - |
| 6179 | + |
6158 | 6180 | ISeq |
6159 | 6181 | (-first [coll] |
6160 | 6182 | [(aget arr i) (aget arr (inc i))]) |
@@ -6261,7 +6283,7 @@ reduces them without incurring seq initialization" |
6261 | 6283 | IIterable |
6262 | 6284 | (-iterator [this] |
6263 | 6285 | (PersistentArrayMapIterator. arr 0 (* cnt 2))) |
6264 | | - |
| 6286 | + |
6265 | 6287 | ISeqable |
6266 | 6288 | (-seq [coll] |
6267 | 6289 | (persistent-array-map-seq arr 0 nil)) |
@@ -6302,6 +6324,11 @@ reduces them without incurring seq initialization" |
6302 | 6324 | (-contains-key? [coll k] |
6303 | 6325 | (not (== (array-map-index-of coll k) -1))) |
6304 | 6326 |
|
| 6327 | + IFind |
| 6328 | + (-find [coll k] |
| 6329 | + (let [idx (array-map-index-of coll k)] |
| 6330 | + [(aget arr idx) (get coll k)])) |
| 6331 | + |
6305 | 6332 | IMap |
6306 | 6333 | (-dissoc [coll k] |
6307 | 6334 | (let [idx (array-map-index-of coll k)] |
@@ -6472,7 +6499,7 @@ reduces them without incurring seq initialization" |
6472 | 6499 | tcoll) |
6473 | 6500 | (throw (js/Error. "dissoc! after persistent!"))))) |
6474 | 6501 |
|
6475 | | -(declare TransientHashMap PersistentHashMap) |
| 6502 | +(declare TransientHashMap) |
6476 | 6503 |
|
6477 | 6504 | (defn- array->transient-hash-map [len arr] |
6478 | 6505 | (loop [out (transient (.-EMPTY PersistentHashMap)) |
@@ -7184,8 +7211,6 @@ reduces them without incurring seq initialization" |
7184 | 7211 | (recur (inc j)))))) |
7185 | 7212 | (ArrayNodeSeq. meta nodes i s nil)))) |
7186 | 7213 |
|
7187 | | -(declare TransientHashMap) |
7188 | | - |
7189 | 7214 | (deftype HashMapIter [nil-val root-iter ^:mutable seen] |
7190 | 7215 | Object |
7191 | 7216 | (hasNext [_] |
@@ -7301,6 +7326,12 @@ reduces them without incurring seq initialization" |
7301 | 7326 | :else (not (identical? (.inode-lookup root 0 (hash k) k lookup-sentinel) |
7302 | 7327 | lookup-sentinel)))) |
7303 | 7328 |
|
| 7329 | + IFind |
| 7330 | + (-find [coll k] |
| 7331 | + (if has-nil? |
| 7332 | + [nil nil-val] |
| 7333 | + (.inode-find root 0 (hash k) k nil))) |
| 7334 | + |
7304 | 7335 | IMap |
7305 | 7336 | (-dissoc [coll k] |
7306 | 7337 | (cond (nil? k) (if has-nil? |
@@ -7738,6 +7769,10 @@ reduces them without incurring seq initialization" |
7738 | 7769 | (-assoc [node k v] |
7739 | 7770 | (assoc [key val] k v)) |
7740 | 7771 |
|
| 7772 | + IFind |
| 7773 | + (-find [node k] |
| 7774 | + [key val]) |
| 7775 | + |
7741 | 7776 | IVector |
7742 | 7777 | (-assoc-n [node n v] |
7743 | 7778 | (-assoc-n [key val] n v)) |
@@ -7890,6 +7925,10 @@ reduces them without incurring seq initialization" |
7890 | 7925 | (-assoc [node k v] |
7891 | 7926 | (assoc [key val] k v)) |
7892 | 7927 |
|
| 7928 | + IFind |
| 7929 | + (-find [node k] |
| 7930 | + [key val]) |
| 7931 | + |
7893 | 7932 | IVector |
7894 | 7933 | (-assoc-n [node n v] |
7895 | 7934 | (-assoc-n [key val] n v)) |
@@ -8128,6 +8167,10 @@ reduces them without incurring seq initialization" |
8128 | 8167 | (-contains-key? [coll k] |
8129 | 8168 | (not (nil? (.entry-at coll k)))) |
8130 | 8169 |
|
| 8170 | + IFind |
| 8171 | + (-find [coll k] |
| 8172 | + (.entry-at coll k)) |
| 8173 | + |
8131 | 8174 | IMap |
8132 | 8175 | (-dissoc [coll k] |
8133 | 8176 | (let [found (array nil) |
|
0 commit comments