/
Growable.scala
100 lines (87 loc) · 2.89 KB
/
Growable.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala
package collection
package mutable
/** This trait forms part of collections that can be augmented
* using a `+=` operator and that can be cleared of all elements using
* a `clear` method.
*
* @define coll growable collection
* @define Coll `Growable`
* @define add add
* @define Add Add
*/
trait Growable[-A] extends Clearable {
/** ${Add}s a single element to this $coll.
*
* @param elem the element to $add.
* @return the $coll itself
*/
def addOne(elem: A): this.type
/** Alias for `addOne` */
@`inline` final def += (elem: A): this.type = addOne(elem)
//TODO This causes a conflict in StringBuilder; looks like a compiler bug
//@deprecated("Use addOne or += instead of append", "2.13.0")
//@`inline` final def append(elem: A): Unit = addOne(elem)
/** ${Add}s two or more elements to this $coll.
*
* @param elem1 the first element to $add.
* @param elem2 the second element to $add.
* @param elems the remaining elements to $add.
* @return the $coll itself
*/
@deprecated("Use `++=` aka `addAll` instead of varargs `+=`; infix operations with an operand of multiple args will be deprecated", "2.13.0")
@`inline` final def += (elem1: A, elem2: A, elems: A*): this.type = this += elem1 += elem2 ++= (elems: IterableOnce[A])
/** ${Add}s all elements produced by an IterableOnce to this $coll.
*
* @param xs the IterableOnce producing the elements to $add.
* @return the $coll itself.
*/
def addAll(xs: IterableOnce[A]): this.type = {
if (xs.asInstanceOf[AnyRef] eq this) addAll(Buffer.from(xs)) // avoid mutating under our own iterator
else {
val it = xs.iterator
while (it.hasNext) {
addOne(it.next())
}
}
this
}
/** Alias for `addAll` */
@`inline` final def ++= (xs: IterableOnce[A]): this.type = addAll(xs)
/** @return The number of elements in the collection under construction, if it can be cheaply computed,
* -1 otherwise. The default implementation always returns -1.
*/
def knownSize: Int = -1
}
object Growable {
/**
* Fills a `Growable` instance with the elements of a given iterable
* @param empty Instance to fill
* @param it Elements to add
* @tparam A Element type
* @return The filled instance
*/
def from[A](empty: Growable[A], it: collection.IterableOnce[A]): empty.type = empty ++= it
}
/** This trait forms part of collections that can be cleared
* with a clear() call.
*
* @define coll collection
*/
trait Clearable {
/** Clears the $coll's contents. After this operation, the
* $coll is empty.
*/
def clear(): Unit
}