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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection.mutable
/** This class is used internally to implement data structures that
* are based on resizable arrays.
* //todo enrich with more efficient operations
*
* @author Matthias Zenger, Burak Emir
* @version 1.0, 03/05/2004
*/
trait ResizableArray[A] extends Seq[A] {
protected val initialSize: Int = 16
protected var array: Array[A] = new Array[A](initialSize)
protected var size: Int = 0
//##########################################################################
// implement/override methods of Seq[A]
/** Returns the length of this resizable array.
*/
def length: Int = size
def apply(i: Int) = array(i)
/** Fills the given array <code>xs</code> with the elements of
* this sequence starting at position <code>start</code>.
*
* @param xs the array to fill.
* @param start starting index.
*/
override def copyToArray[B >: A](xs: Array[B], start: Int): Unit =
Array.copy(array, 0, xs, start, size)
/** Copy all elements to a buffer
* @param The buffer to which elements are copied
*/
override def copyToBuffer[B >: A](dest: Buffer[B]): Unit =
dest.++=(array.asInstanceOf[Array[B]], 0, size)
/** Returns a new iterator over all elements of this resizable array.
*/
def elements: Iterator[A] = new Iterator[A] {
var i = 0
def hasNext: Boolean = i < size
def next: A = { i = i + 1; array(i - 1) }
}
//##########################################################################
/** ensure that the internal array has at n cells */
protected def ensureSize(n: Int): Unit =
if (n > array.length) {
var newsize = array.length * 2
while (n > newsize)
newsize = newsize * 2
val newar: Array[A] = new Array(newsize)
Array.copy(array, 0, newar, 0, size)
array = newar
}
/** Swap two elements of this array.
*/
protected def swap(a: Int, b: Int): Unit = {
val h = array(a)
array(a) = array(b)
array(b) = h
}
/** Move parts of the array.
*/
protected def copy(m: Int, n: Int, len: Int) =
Array.copy(array, m, array, n, len)
}
|