Skip to content

Commit b2c223b

Browse files
authored
Merge pull request #272 from clojure/cljs-3454/set-identical
CLJS-3454: New set instances are created when redundant data is added
2 parents 249faa2 + 4eff03d commit b2c223b

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9308,7 +9308,10 @@ reduces them without incurring seq initialization"
93089308

93099309
ICollection
93109310
(-conj [coll o]
9311-
(PersistentHashSet. meta (assoc hash-map o nil) nil))
9311+
(let [m (-assoc hash-map o nil)]
9312+
(if (identical? m hash-map)
9313+
coll
9314+
(PersistentHashSet. meta m nil))))
93129315

93139316
IEmptyableCollection
93149317
(-empty [coll] (-with-meta (.-EMPTY PersistentHashSet) meta))
@@ -9345,7 +9348,10 @@ reduces them without incurring seq initialization"
93459348

93469349
ISet
93479350
(-disjoin [coll v]
9348-
(PersistentHashSet. meta (-dissoc hash-map v) nil))
9351+
(let [m (-dissoc hash-map v)]
9352+
(if (identical? m hash-map)
9353+
coll
9354+
(PersistentHashSet. meta m nil))))
93499355

93509356
IFn
93519357
(-invoke [coll k]
@@ -9463,7 +9469,10 @@ reduces them without incurring seq initialization"
94639469

94649470
ICollection
94659471
(-conj [coll o]
9466-
(PersistentTreeSet. meta (assoc tree-map o nil) nil))
9472+
(let [m (-assoc tree-map o nil)]
9473+
(if (identical? m tree-map)
9474+
coll
9475+
(PersistentTreeSet. meta m nil))))
94679476

94689477
IEmptyableCollection
94699478
(-empty [coll] (PersistentTreeSet. meta (-empty tree-map) 0))
@@ -9517,7 +9526,10 @@ reduces them without incurring seq initialization"
95179526

95189527
ISet
95199528
(-disjoin [coll v]
9520-
(PersistentTreeSet. meta (dissoc tree-map v) nil))
9529+
(let [m (-dissoc tree-map v)]
9530+
(if (identical? m tree-map)
9531+
coll
9532+
(PersistentTreeSet. meta m nil))))
95219533

95229534
IFn
95239535
(-invoke [coll k]
@@ -13002,7 +13014,10 @@ reduces them without incurring seq initialization"
1300213014

1300313015
ISet
1300413016
(-disjoin [coll v]
13005-
(Set. meta (-dissoc hash-map v) nil))
13017+
(let [new-hash-map (-dissoc hash-map v)]
13018+
(if (identical? new-hash-map hash-map)
13019+
coll
13020+
(Set. meta new-hash-map nil))))
1300613021

1300713022
IEditableCollection
1300813023
(-as-transient [coll]

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)