summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/ArrayStack.scala
blob: 4cfebaf76dc6f0713767e223e7b8e3e2e0832998 (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection
package mutable

import generic._

private object Utils {
  def growArray(x: Array[AnyRef]) = {
    val y = new Array[AnyRef](x.length * 2)
    Array.copy(x, 0, y, 0, x.length)
    y
  }

  def clone(x: Array[AnyRef]) = {
    val y = new Array[AnyRef](x.length)
    Array.copy(x, 0, y, 0, x.length)
    y
  }
}

/**
 * Simple stack class backed by an array. Should be significantly faster
 * than the standard mutable stack.
 *
 * @author David MacIver
 * @since  2.7
 */
@cloneable @serializable @SerialVersionUID(8565219180626620510L)
class ArrayStack[T] private(private var table : Array[AnyRef],
                            private var index : Int) extends scala.collection.Seq[T] with Cloneable[ArrayStack[T]] {
  def this() = this(new Array[AnyRef](1), 0)

  /** Retrieve n'th element from stack, where top of stack has index 0 */
  def apply(n: Int): T =
    table(index - 1 - n).asInstanceOf[T]

  /** The number of elements in the stack */
  def length = index

  /**
   * Push an element onto the stack.
   *
   * @param x The element to push
   */
  def push(x: T) {
    if (index == table.length) table = Utils.growArray(table)
    table(index) = x.asInstanceOf[AnyRef]
    index += 1
  }

  /**
   * Pop the top element off the stack.
   */
  def pop: T = {
    if (index == 0) error("Stack empty")
    index -= 1
    val x = table(index).asInstanceOf[T]
    table(index) = null
    x
  }

  /** View the top element of the stack. */
  @deprecated("use top instead")
  def peek = top

  /**
   * View the top element of the stack.
   */
  def top: T = table(index - 1).asInstanceOf[T]

  /**
   * Duplicate the top element of the stack.
   */
  def dup = push(top)

  /**
   * Empties the stack.
   */
  def clear {
    index = 0
    table = new Array(1)
  }

  /**
   * Empties the stack, passing all elements on it in FIFO order to the
   * provided function.
   *
   * @param f The function to drain to.
   */
  def drain(f: T => Unit) = while (!isEmpty) f(pop)

  /**
   * Pushes all the provided elements onto the stack.
   *
   * @param x The source of elements to push
   */
  def ++=(x: scala.collection.Iterable[T]): this.type = { x.foreach(this +=(_)); this }


  /**
   * Pushes all the provided elements onto the stack.
   *
   * @param x The source of elements to push
   */
  def ++=(x: Iterator[T]): this.type = { x.foreach(this +=(_)); this }

  /**
   *  Alias for push.
   *
   *  @param x The element to push
   */
  def +=(x: T): this.type = { push(x); this }

  /**
   * Pop the top two elements off the stack, apply f to them and push the result
   * back on to the stack.
   *
   * @param f The combining function
   */
  def combine(f: (T, T) => T) = push(f(pop, pop));

  /**
   * Repeatedly combine the top elements of the stack until the stack contains only
   * one element.
   */
  def reduceWith(f: (T, T) => T) = while(size > 1) combine(f)

  override def size = index

  /**
   * Evaluates the expression, preserving the contents of the stack so that
   * any changes the evaluation makes to the stack contents will be undone after
   * it completes.
   *
   * @param action The action to run.
   */
  def preserving[T](action: => T) = {
    val oldIndex = index
    val oldTable = Utils.clone(table)

    try {
      action
    } finally {
      index = oldIndex
      table = oldTable
    }
  }

  override def isEmpty: Boolean = index == 0

  /**
   * Iterates over the stack in LIFO order.
   */
  def iterator: Iterator[T] = new Iterator[T] {
    var currentIndex = index
    def hasNext = currentIndex > 0
    def next = {
      currentIndex -= 1
      table(currentIndex).asInstanceOf[T]
    }
  }

  override def foreach[U](f: T =>  U) {
    var currentIndex = index
    while (currentIndex > 0) {
      currentIndex -= 1
      f(table(currentIndex).asInstanceOf[T])
    }
  }

  override def clone = new ArrayStack[T](Utils.clone(table), index)
}