File tree Expand file tree Collapse file tree 2 files changed +17
-10
lines changed
library/src/scala/collection/mutable Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -478,11 +478,12 @@ object LinkedHashMap extends MapFactory[LinkedHashMap] {
478
478
479
479
def empty [K , V ] = new LinkedHashMap [K , V ]
480
480
481
- def from [K , V ](it : collection.IterableOnce [(K , V )]) =
482
- it match {
483
- case lhm : LinkedHashMap [K , V ] => lhm
484
- case _ => Growable .from(empty[K , V ], it)
485
- }
481
+ def from [K , V ](it : collection.IterableOnce [(K , V )]) = {
482
+ val newlhm = empty[K , V ]
483
+ newlhm.sizeHint(it.knownSize)
484
+ newlhm.addAll(it)
485
+ newlhm
486
+ }
486
487
487
488
def newBuilder [K , V ] = new GrowableBuilder (empty[K , V ])
488
489
Original file line number Diff line number Diff line change @@ -82,6 +82,11 @@ class LinkedHashSet[A]
82
82
83
83
def contains (elem : A ): Boolean = findEntry(elem) ne null
84
84
85
+ override def sizeHint (size : Int ): Unit = {
86
+ val target = tableSizeFor(((size + 1 ).toDouble / LinkedHashSet .defaultLoadFactor).toInt)
87
+ if (target > table.length) growTable(target)
88
+ }
89
+
85
90
override def add (elem : A ): Boolean = {
86
91
if (contentSize + 1 >= threshold) growTable(table.length * 2 )
87
92
val hash = computeHash(elem)
@@ -311,11 +316,12 @@ object LinkedHashSet extends IterableFactory[LinkedHashSet] {
311
316
312
317
override def empty [A ]: LinkedHashSet [A ] = new LinkedHashSet [A ]
313
318
314
- def from [E ](it : collection.IterableOnce [E ]) =
315
- it match {
316
- case lhs : LinkedHashSet [E ] => lhs
317
- case _ => Growable .from(empty[E ], it)
318
- }
319
+ def from [E ](it : collection.IterableOnce [E ]) = {
320
+ val newlhs = empty[E ]
321
+ newlhs.sizeHint(it.knownSize)
322
+ newlhs.addAll(it)
323
+ newlhs
324
+ }
319
325
320
326
def newBuilder [A ] = new GrowableBuilder (empty[A ])
321
327
You can’t perform that action at this time.
0 commit comments