diff options
-rw-r--r-- | src/library/scala/collection/mutable/ArrayStack.scala | 17 | ||||
-rw-r--r-- | test/files/run/t4535.check | 3 | ||||
-rw-r--r-- | test/files/run/t4535.scala | 30 |
3 files changed, 49 insertions, 1 deletions
diff --git a/src/library/scala/collection/mutable/ArrayStack.scala b/src/library/scala/collection/mutable/ArrayStack.scala index 012105d7c4..b39aff2463 100644 --- a/src/library/scala/collection/mutable/ArrayStack.scala +++ b/src/library/scala/collection/mutable/ArrayStack.scala @@ -169,7 +169,22 @@ extends Seq[T] */ def +=(x: T): this.type = { push(x); this } - def result = new ArrayStack[T](table.reverse, index) + def result = { + reverseTable() + this + } + + private def reverseTable() { + var i = 0 + val until = index / 2 + while (i < until) { + val revi = index - i - 1 + val tmp = table(i) + table(i) = table(revi) + table(revi) = tmp + i += 1 + } + } /** Pop the top two elements off the stack, apply `f` to them and push the result * back on to the stack. diff --git a/test/files/run/t4535.check b/test/files/run/t4535.check new file mode 100644 index 0000000000..9d4ce0d535 --- /dev/null +++ b/test/files/run/t4535.check @@ -0,0 +1,3 @@ +ArrayStack(1, 2, 3) +ArrayStack(1, 2, 3, 4, 5, 6) +ArrayStack(6, 5, 4, 3, 2, 1)
\ No newline at end of file diff --git a/test/files/run/t4535.scala b/test/files/run/t4535.scala new file mode 100644 index 0000000000..91c13a28cd --- /dev/null +++ b/test/files/run/t4535.scala @@ -0,0 +1,30 @@ + + +import collection._ + + +// #4535 +object Test { + + def main(args: Array[String]) { + val as = new mutable.ArrayStack[Int] + as push 1 + as push 2 + as push 3 + println(as.reverse) + + as push 4 + as push 5 + as push 6 + println(as.reverse) + + println(as map { x => x }) + + for (i <- 0 until 100) { + as push i + assert(as == as.map(x => x)) + assert(as == as.reverse.reverse) + } + } + +} |