@@ -23,7 +23,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
23
23
def isEmpty = elems == null
24
24
def nonEmpty = elems != null
25
25
26
- inline def foreach (inline op : T => Unit ): Unit = {
26
+ @ inline def foreach (@ inline op : T => Unit ): Unit = {
27
27
def sharedOp (x : T ) = op(x)
28
28
elems match {
29
29
case null =>
@@ -34,10 +34,10 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
34
34
}
35
35
}
36
36
37
- /** Like `foreach`, but completely inlines `op`, atthe price of generating the code twice.
37
+ /** Like `foreach`, but completely @ inlines `op`, atthe price of generating the code twice.
38
38
* Should be used only of `op` is small
39
39
*/
40
- inline def foreachInlined (inline op : T => Unit ): Unit = elems match {
40
+ @ inline def foreachInlined (@ inline op : T => Unit ): Unit = elems match {
41
41
case null =>
42
42
case elems : Array [AnyRef ] => def elem (i : Int ) = elems(i).asInstanceOf [T ]
43
43
var i = 0
@@ -58,7 +58,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
58
58
}
59
59
60
60
/** `f` is pulled out, not duplicated */
61
- inline def map [U <: AnyRef ](inline f : T => U ): Lst [U ] = {
61
+ @ inline def map [U <: AnyRef ](@ inline f : T => U ): Lst [U ] = {
62
62
def op (x : T ) = f(x)
63
63
elems match {
64
64
case null => Empty
@@ -142,7 +142,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
142
142
}
143
143
def filterNot (p : T => Boolean ): Lst [T ] = filter(! p(_))
144
144
145
- inline def exists (inline p : T => Boolean ): Boolean = {
145
+ @ inline def exists (@ inline p : T => Boolean ): Boolean = {
146
146
def op (x : T ) = p(x)
147
147
elems match {
148
148
case null => false
@@ -155,7 +155,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
155
155
}
156
156
}
157
157
158
- inline def forall (inline p : T => Boolean ): Boolean = {
158
+ @ inline def forall (@ inline p : T => Boolean ): Boolean = {
159
159
def op (x : T ) = p(x)
160
160
elems match {
161
161
case null => true
@@ -168,7 +168,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
168
168
}
169
169
}
170
170
171
- inline def contains [U >: T ](x : U ): Boolean = elems match {
171
+ @ inline def contains [U >: T ](x : U ): Boolean = elems match {
172
172
case null => false
173
173
case elems : Array [AnyRef ] => def elem (i : Int ) = elems(i).asInstanceOf [T ]
174
174
var i = 0
@@ -178,7 +178,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
178
178
elem == x
179
179
}
180
180
181
- inline def foldLeft [U ](z : U )(inline f : (U , T ) => U ) = {
181
+ @ inline def foldLeft [U ](z : U )(@ inline f : (U , T ) => U ) = {
182
182
def op (x : U , y : T ) = f(x, y)
183
183
elems match {
184
184
case null => z
@@ -192,7 +192,7 @@ class Lst[+T <: AnyRef](val elems: AnyRef) extends AnyVal {
192
192
}
193
193
}
194
194
195
- inline def /: [U ](z : U )(inline op : (U , T ) => U ) = foldLeft(z)(op)
195
+ @ inline def /: [U ](z : U )(@ inline op : (U , T ) => U ) = foldLeft(z)(op)
196
196
197
197
def reduceLeft [U >: T ](op : (U , U ) => U ) = elems match {
198
198
case elems : Array [AnyRef ] => def elem (i : Int ) = elems(i).asInstanceOf [T ]
@@ -422,25 +422,47 @@ object Lst {
422
422
val equalsFn = (x : AnyRef , y : AnyRef ) => x `equals` y
423
423
424
424
class Buffer [T <: AnyRef ] {
425
- var len = 0
425
+ private var len = 0
426
426
private var elem : T = _
427
427
private var elems : Arr = _
428
428
429
- def += (x : T ) = {
429
+ def size = len
430
+
431
+ /** pre: len > 0, n > 1 */
432
+ private def ensureSize (n : Int ) =
433
+ if (len == 1 ) {
434
+ elems = new Arr (n `max` 16 )
435
+ elems(0 ) = elem
436
+ }
437
+ else if (len < n) {
438
+ val newLen = n `max` len << 2
439
+ val newElems = new Arr (newLen)
440
+ System .arraycopy(elems, 0 , newElems, 0 , len)
441
+ elems = newElems
442
+ }
443
+
444
+ def += (x : T ): this .type = {
430
445
if (len == 0 ) elem = x
431
446
else {
432
- if (len == 1 ) {
433
- elems = new Arr (16 )
434
- elems(0 ) = elem
435
- }
436
- else if (len == elems.length) {
437
- val newElems = new Arr (elems.length * 2 )
438
- System .arraycopy(elems, 0 , newElems, 0 , elems.length)
439
- elems = newElems
440
- }
447
+ ensureSize(len + 1 )
441
448
elems(len) = x
442
449
}
443
450
len += 1
451
+ this
452
+ }
453
+
454
+ def ++= (xs : Lst [T ]): this .type = {
455
+ xs.elems match {
456
+ case null => this
457
+ case elems2 : Array [AnyRef ] =>
458
+ if (len == 0 ) elems = elems2
459
+ else {
460
+ ensureSize(len + elems2.length)
461
+ System .arraycopy(elems2, 0 , elems, len, elems2.length)
462
+ }
463
+ case elem : T @ unchecked => this += elem
464
+ }
465
+ this
444
466
}
445
467
446
468
def toLst : Lst [T ] =
0 commit comments