summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/ListBuffer.scala
blob: b031a4a9aaf9d2d979f16af460beceb647d20746 (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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection.mutable


import Predef._

/**
 */
final class ListBuffer[A] extends Buffer[A] {
  private var start: List[A] = Nil
  private var last: ::[A] = _
  private var exported: boolean = false

  /** Prepend a single element to this buffer.
   *
   *  @param x  the element to prepend.
   */
  def +: (x: A): Buffer[A] = {
    if (exported) copy()
    val newElem = new scala.:: (x, start)
    if (start.isEmpty) last = newElem
    start = newElem
    this
  }

  /** Append a single element to this buffer.
   *
   *  @param x  the element to append.
   */
  override def += (x: A): unit = {
    if (exported) copy()
    if (start.isEmpty) {
      last = new scala.:: (x, Nil)
      start = last
    } else {
      val last1 = last
      last = new scala.:: (x, Nil)
      last1.tl = last
    }
  }

  /** Remove a single element from the buffer and return
   *  the identity of the buffer. Same as ``this -= x; this''
   *
   *  @param x  the element to remove.
   */
  def - (x: A): Buffer[A] = { this -= x; this }

  /** Remove a single element from this buffer.
   *
   *  @param x  the element to remove.
   */
  override def -= (x: A): unit = {
    if (exported) copy()
    if (start.isEmpty) {}
    else if (start.head == x) start = start.tail
    else {
      var cursor = start
      while (!cursor.tail.isEmpty && cursor.tail.head != x)
        cursor = cursor.tail
      if (!cursor.tail.isEmpty)
        cursor.asInstanceOf[scala.::[A]].tl = cursor.tail.tail
    }
  }

  /** Converts this buffer to a list
   */
  override def toList: List[A] = {
    exported = !start.isEmpty
    start
  }

  /** Prepends the elements of this buffer to a given list
   *
   *  @param xs   the list to which elements are prepended
   */
  def prependToList(xs: List[A]): List[A] =
    if (start.isEmpty) xs
    else { last.tl = xs; toList }

  /** Clears the buffer contents.
   */
  def clear: unit = {
    start = Nil
    exported = false
  }

  /** Copy contents of this buffer */
  private def copy() = {
    var cursor = start
    val limit = last.tail
    clear
    while (cursor ne limit) {
      this += cursor.head
      cursor = cursor.tail
    }
  }

  /** Returns the length of this buffer.
   */
  def length: int = start.length

  private def noElem(n: int): All =
    error("element " + n + " does not exist in buffer")

  /** Returns the <code>n</code>th element of this list. This method
   *  yields an error if the element does not exist.
   */
  def apply(n: Int): A = try {
    start(n)
  } catch {
    case ex: Error => noElem(n)
  }

  /** Replace element at index <code>n</code> with the new element
   *  <code>newelem</code>.
   *
   *  @param n       the index of the element to replace.
   *  @param x       the new element.
   */
  def update(n: Int, x: A): unit = try {
    if (exported) copy()
    if (n == 0) {
      val newElem = new scala.:: (x, start.tail);
      if (last eq start) last = newElem
      start = newElem
    } else {
      var cursor = start;
      var i = 1;
      while (i < n) {
        cursor = cursor.tail
        i = i + 1
      }
      val newElem = new scala.:: (x, cursor.tail.tail)
      if (last eq cursor.tail) last = newElem
      cursor.asInstanceOf[scala.::[A]].tl = newElem
    }
  } catch {
    case ex: Error => noElem(n)
  }

  /** Inserts new elements at the index <code>n</code>. Opposed to method
   *  <code>update</code>, this method will not replace an element with a new
   *  one. Instead, it will insert a new element at index <code>n</code>.
   *
   *  @param n     the index where a new element will be inserted.
   *  @param iter  the iterable object providing all elements to insert.
   */
  def insertAll(n: Int, iter: Iterable[A]): unit = try {
    if (exported) copy()
    var elems = iter.elements.toList.reverse
    if (n == 0) {
      while (!elems.isEmpty) {
        val newElem = new scala.:: (elems.head, start);
        if (start.isEmpty) last = newElem
        start = newElem
        elems = elems.tail
      }
    } else {
      var cursor = start
      var i = 1
      while (i < n) {
        cursor = cursor.tail
        i = i + 1
      }
      while (!elems.isEmpty) {
        val newElem = new scala.:: (elems.head, cursor.tail);
        if (cursor.tail.isEmpty) last = newElem
        cursor.asInstanceOf[scala.::[A]].tl = newElem
        elems = elems.tail
      }
    }
  } catch {
    case ex: Error => noElem(n)
  }

  /** Removes the element on a given index position.
   *
   *  @param n       the index which refers to the element to delete.
   *  @return n      the element that was formerly at posiition `n'
   *  @pre           an element exists at position `n'
   */
  def remove(n: Int): A = try {
    if (exported) copy()
    var old = start.head;
    if (n == 0) {
      start = start.tail
    } else {
      var cursor = start
      var i = 1
      while (i < n) {
        cursor = cursor.tail
        i = i + 1
      }
      old = cursor.tail.head
      if (last eq cursor.tail) last = cursor.asInstanceOf[scala.::[A]]
      cursor.asInstanceOf[scala.::[A]].tl = cursor.tail.tail
    }
    old
  } catch {
    case ex: Error => noElem(n)
  }

  /** Returns an iterator over all elements of this list.
   *  Note: the iterator can be affected by insertions, updates and deletions
   *  that are performed afterwards on the buffer. To get iterator an over the current
   *  buffer snapshot, use  toList.elements
   */
  override def elements = new Iterator[A] {
    var cursor: List[A] = null
    def hasNext: Boolean = !start.isEmpty && cursor != last
    def next: A =
      if (!hasNext) {
        error("next on empty Iterator")
      } else {
        if (cursor == null) cursor = start else cursor = cursor.tail
        cursor.head
      }
  }

  /** Return a clone of this buffer.
   *
   *  @return a <code>ListBuffer</code> with the same elements.
   */
  override def clone(): Buffer[A] = (new ListBuffer[A]) ++ this

  /** Checks if two buffers are structurally identical.
   *
   *  @return true, iff both buffers contain the same sequence of elements.
   */
  override def equals(obj: Any): Boolean = obj match {
    case that: ListBuffer[A] =>
      (this.length == that.length &&
       elements.zip(that.elements).forall {
         case Pair(thiselem, thatelem) => thiselem == thatelem
       })
    case _ => false
  }

  /** Defines the prefix of the string representation.
   */
  override protected def stringPrefix: String = "ListBuffer"
}