@@ -82,18 +82,15 @@ sealed class ListSet[A] extends AbstractSet[A]
82
82
83
83
override def ++ (xs : GenTraversableOnce [A ]): ListSet [A ] =
84
84
xs match {
85
- // we want to avoid to use of iterator as it causes allocations
86
- // during reverseList
87
- case ls : ListSet [A ] =>
88
- if (ls eq this ) this
89
- else {
90
- val lsSize = ls.size
91
- if (lsSize == 0 ) this
92
- else if (isEmpty) ls
85
+ case _ : this .type => this
86
+ case _ if xs.isEmpty => this
87
+ // we want to avoid using iterator as it causes allocations during reverseList
88
+ case ls : ListSet [A ] =>
89
+ if (isEmpty) ls
93
90
else {
94
- @ tailrec def skip (ls : ListSet [A ], count : Int ): ListSet [A ] = {
91
+ // optimize add non-empty ListSet
92
+ @ tailrec def skip (ls : ListSet [A ], count : Int ): ListSet [A ] =
95
93
if (count == 0 ) ls else skip(ls.next, count - 1 )
96
- }
97
94
98
95
@ tailrec def containsLimited (n : ListSet [A ], e : A , end : ListSet [A ]): Boolean =
99
96
(n ne end) && (e == n.elem || containsLimited(n.next, e, end))
@@ -104,6 +101,7 @@ sealed class ListSet[A] extends AbstractSet[A]
104
101
// We hope to get some structural sharing so find the tail of the
105
102
// ListSet that are `eq` (or if there are not any then the ends of the lists),
106
103
// and we optimise the add to only iterate until we reach the common end
104
+ val lsSize = ls.size
107
105
val thisSize = this .size
108
106
val remaining = Math .min(thisSize, lsSize)
109
107
var thisTail = skip(this , thisSize - remaining)
@@ -151,16 +149,13 @@ sealed class ListSet[A] extends AbstractSet[A]
151
149
case 4 => pending3
152
150
case _ => pending(pendingCount - 5 )
153
151
}
154
- val r = result
152
+ val r = result
155
153
result = new r.Node (elem)
156
154
pendingCount -= 1
157
155
}
158
156
result
159
157
}
160
- }
161
- case _ =>
162
- if (xs.isEmpty) this
163
- else (repr /: xs) (_ + _)
158
+ case _ => xs.foldLeft(repr)(_ + _)
164
159
}
165
160
166
161
def iterator : Iterator [A ] = {
0 commit comments