Skip to content

Commit db5cbfd

Browse files
quollswannodette
authored andcommitted
CLJS-3454: New set instances are created when redundant data is added
1 parent e46a486 commit db5cbfd

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9304,7 +9304,10 @@ reduces them without incurring seq initialization"
93049304

93059305
ICollection
93069306
(-conj [coll o]
9307-
(PersistentHashSet. meta (assoc hash-map o nil) nil))
9307+
(let [m (-assoc hash-map o nil)]
9308+
(if (identical? m hash-map)
9309+
coll
9310+
(PersistentHashSet. meta m nil))))
93089311

93099312
IEmptyableCollection
93109313
(-empty [coll] (-with-meta (.-EMPTY PersistentHashSet) meta))
@@ -9341,7 +9344,10 @@ reduces them without incurring seq initialization"
93419344

93429345
ISet
93439346
(-disjoin [coll v]
9344-
(PersistentHashSet. meta (-dissoc hash-map v) nil))
9347+
(let [m (-dissoc hash-map v)]
9348+
(if (identical? m hash-map)
9349+
coll
9350+
(PersistentHashSet. meta m nil))))
93459351

93469352
IFn
93479353
(-invoke [coll k]
@@ -9459,7 +9465,10 @@ reduces them without incurring seq initialization"
94599465

94609466
ICollection
94619467
(-conj [coll o]
9462-
(PersistentTreeSet. meta (assoc tree-map o nil) nil))
9468+
(let [m (-assoc tree-map o nil)]
9469+
(if (identical? m tree-map)
9470+
coll
9471+
(PersistentTreeSet. meta m nil))))
94639472

94649473
IEmptyableCollection
94659474
(-empty [coll] (PersistentTreeSet. meta (-empty tree-map) 0))
@@ -9513,7 +9522,10 @@ reduces them without incurring seq initialization"
95139522

95149523
ISet
95159524
(-disjoin [coll v]
9516-
(PersistentTreeSet. meta (dissoc tree-map v) nil))
9525+
(let [m (-dissoc tree-map v)]
9526+
(if (identical? m tree-map)
9527+
coll
9528+
(PersistentTreeSet. meta m nil))))
95179529

95189530
IFn
95199531
(-invoke [coll k]

src/test/cljs/cljs/core_test.cljs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,27 @@
737737
(is (= #{1 2} (hash-set 1 2 2)))
738738
(is (= #{1 2} (apply hash-set [1 2 2]))))
739739

740+
(deftest test-ordered-set
741+
(is (= #{1 2} (sorted-set 1 2 2)))
742+
(is (= [1 2 3] (seq (sorted-set 2 3 1))))
743+
(is (= #{1 2} (apply sorted-set [1 2 2]))))
744+
745+
(deftest test-3454-conj
746+
(is (= #{1 2 3} (conj #{1 2} 3)))
747+
(is (= #{1 2 3} (conj (sorted-set 1 2) 3)))
748+
(let [s #{1 2}
749+
ss (sorted-set 1 2)]
750+
(is (identical? s (conj s 2)))
751+
(is (identical? ss (conj ss 2)))))
752+
753+
(deftest test-3454-disj
754+
(is (= #{1 2} (disj #{1 2 3} 3)))
755+
(is (= #{1 2} (disj (sorted-set 1 2 3) 3)))
756+
(let [s #{1 2}
757+
ss (sorted-set 1 2)]
758+
(is (identical? s (disj s 3)))
759+
(is (identical? ss (disj ss 3)))))
760+
740761
(deftest test-585
741762
(is (= (last (map identity (into [] (range 32)))) 31))
742763
(is (= (into #{} (range 32))

0 commit comments

Comments
 (0)