Commit 1edee838 authored by h2b's avatar h2b

Letting parallel set-pixels return a future for completion check.

parent ba6ce9e6
......@@ -150,30 +150,47 @@ abstract class Screen (override val p0: Pixel, override val width: Int,
* Sets all pixels in a rectangle with diagonal opposite corners at `p1` and `p2`
* to a computed color using parallel operations.
*
* The returned value can be used if further operations need to be informed
* when this operation is done (e.g., by setting a callback). The future is
* completed when either all pixels are set or some exception occurs. Note
* that in case of an exception some pixel settings might still continue
* afterwards.
*
* @param p1
* @param p2
* @param col computing function that maps pixel coordinates to colors
* @return a future with no specific value indicating the completion of this
* operation
*/
def setPixelsParallel (p1: Pixel, p2: Pixel) (col: Pixel Color): Unit = {
def setPixelsParallel (p1: Pixel, p2: Pixel) (col: Pixel Color): Future[Unit] = {
val left = p1.x min p2.x
val right = p1.x max p2.x
val bottom = p1.y min p2.y
val top = p1.y max p2.y
for (y bottom to top) Future {
val futures = for (y bottom to top) yield Future {
for (x left to right) {
val p = Pixel(x,y)
setPixel(p, col(p))
}
}
Future.sequence(futures) map { _ () }
}
/**
* Sets all pixels of the screen to a computed color using parallel
* operations.
*
* The returned value can be used if further operations need to be informed
* when this operation is done (e.g., by setting a callback). The future is
* completed when either all pixels are set or some exception occurs. Note
* that in case of an exception some pixel settings might still continue
* afterwards.
*
* @param col computing function that maps pixel coordinates to colors
* @return a future with no specific value indicating the completion of this
* operation
*/
def setPixelsParallel (col: Pixel Color): Unit =
def setPixelsParallel (col: Pixel Color): Future[Unit] =
setPixelsParallel(Pixel(0,0), Pixel(width-1,height-1))(col)
/**
......
......@@ -17,7 +17,7 @@
*/
package de.h2b.scala.lib.simgraf.demo
import scala.util.Random
import scala.util.{ Failure, Random, Success }
import de.h2b.scala.lib.simgraf.{ Color, Pixel, Screen }
......@@ -72,7 +72,11 @@ object ScreenDemo extends App {
{
n +=1
val screen = Screen(Pixel(500,400), w, h, s"Screen Demo $n")
screen.setPixelsParallel(_ => Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)))
val future = screen.setPixelsParallel(_ => Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)))
future.onComplete {
case Success(_) println("screen.setPixelsParallel succeeded.")
case Failure(f) println("screen.setPixelsParallel failed with: " + f.getMessage)
} (scala.concurrent.ExecutionContext.global)
}
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment