diff options
author | Paul Phillips <paulp@improving.org> | 2012-05-03 12:05:58 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-05-03 12:16:21 -0700 |
commit | 264cef8f9677c59395166da9be0af0bfe83abfa5 (patch) | |
tree | 41db0c14e6704edb9a06e92af0d4f6cd1c2f22bf /test/files/continuations-run | |
parent | 453d615fb3c6d0db3a0a43c9232bc12584e39107 (diff) | |
download | scala-264cef8f9677c59395166da9be0af0bfe83abfa5.tar.gz scala-264cef8f9677c59395166da9be0af0bfe83abfa5.tar.bz2 scala-264cef8f9677c59395166da9be0af0bfe83abfa5.zip |
Test cases for SI-5472, SI-5399, SI-5685.
Diffstat (limited to 'test/files/continuations-run')
-rw-r--r-- | test/files/continuations-run/t5472.check | 1 | ||||
-rw-r--r-- | test/files/continuations-run/t5472.scala | 90 |
2 files changed, 91 insertions, 0 deletions
diff --git a/test/files/continuations-run/t5472.check b/test/files/continuations-run/t5472.check new file mode 100644 index 0000000000..d42e80c18e --- /dev/null +++ b/test/files/continuations-run/t5472.check @@ -0,0 +1 @@ +List(23, 23) diff --git a/test/files/continuations-run/t5472.scala b/test/files/continuations-run/t5472.scala new file mode 100644 index 0000000000..3e3c76b32a --- /dev/null +++ b/test/files/continuations-run/t5472.scala @@ -0,0 +1,90 @@ +import scala.annotation._ +import scala.util.continuations._ +import java.util.concurrent.atomic._ + +object Test { + def main(args: Array[String]) { + val map = Map("foo" -> 1, "bar" -> 2) + reset { + val mapped = + for { + (location, accessors) <- new ContinuationizedParallelIterable(map) + } yield { + shiftUnit0[Int, Unit](23) + } + println(mapped.toList) + } + } +} + +final class ContinuationizedParallelIterable[+A](protected val underline: Iterable[A]) { + def toList = underline.toList.sortBy(_.toString) + + final def filter(p: A => Boolean @suspendable): ContinuationizedParallelIterable[A] @suspendable = + shift( + new AtomicInteger(1) with ((ContinuationizedParallelIterable[A] => Unit) => Unit) { + private val results = new AtomicReference[List[A]](Nil) + + @tailrec + private def add(element: A) { + val old = results.get + if (!results.compareAndSet(old, element :: old)) { + add(element) + } + } + + override final def apply(continue: ContinuationizedParallelIterable[A] => Unit) { + for (element <- underline) { + super.incrementAndGet() + reset { + val pass = p(element) + if (pass) { + add(element) + } + if (super.decrementAndGet() == 0) { + continue(new ContinuationizedParallelIterable(results.get)) + } + } + } + if (super.decrementAndGet() == 0) { + continue(new ContinuationizedParallelIterable(results.get)) + } + } + }) + + final def foreach[U](f: A => U @suspendable): Unit @suspendable = + shift( + new AtomicInteger(1) with ((Unit => Unit) => Unit) { + override final def apply(continue: Unit => Unit) { + for (element <- underline) { + super.incrementAndGet() + reset { + f(element) + if (super.decrementAndGet() == 0) { + continue() + } + } + } + if (super.decrementAndGet() == 0) { + continue() + } + } + }) + + final def map[B: Manifest](f: A => B @suspendable): ContinuationizedParallelIterable[B] @suspendable = + shift( + new AtomicInteger(underline.size) with ((ContinuationizedParallelIterable[B] => Unit) => Unit) { + override final def apply(continue: ContinuationizedParallelIterable[B] => Unit) { + val results = new Array[B](super.get) + for ((element, i) <- underline.view zipWithIndex) { + reset { + val result = f(element) + results(i) = result + if (super.decrementAndGet() == 0) { + continue(new ContinuationizedParallelIterable(results)) + } + } + } + } + }) +} |