blob: 67fb4918eaf34edf18a0f67414c4c4f4dd7d0105 (
plain) (
blame)
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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id: ArrayBuffer.scala 18387 2009-07-24 15:28:37Z odersky $
package scala.collection.mutable
import scala.collection.generic._
import scala.reflect.ClassManifest
import scala.runtime.BoxedArray
/** A builder class for arrays */
class ArrayBuilder[A](manifest: ClassManifest[A]) extends Builder[A, BoxedArray[A]] {
private var elems: BoxedArray[A] = _
private var capacity: Int = 0
private var size: Int = 0
private def mkArray(size: Int): BoxedArray[A] = {
if (manifest != null) {
val newelems = manifest.newArray(size)
if (this.size > 0) Array.copy(elems.value, 0, newelems.value, 0, this.size)
newelems
} else { // !!!
val newelems = new scala.runtime.BoxedAnyArray[A](size)
if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
newelems
}
}
private def resize(size: Int) {
elems = mkArray(size)
capacity = size
}
override def sizeHint(size: Int) {
if (capacity < size) resize(size)
}
private def ensureSize(size: Int) {
if (capacity < size) {
var newsize = if (capacity == 0) 16 else capacity * 2
while (newsize < size) newsize *= 2
resize(newsize)
}
}
def +=(elem: A): this.type = {
ensureSize(size + 1)
elems(size) = elem
size += 1
this
}
def clear() {
size = 0
}
def result() = {
if (capacity != 0 && capacity == size) elems
else mkArray(size)
}
// todo: add ++=
}
|