This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

FixedPoint.scala 593 Bytes
Newer Older
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
package de.bbisping.coupledsim.util

import scala.collection.mutable.Builder

class FixedPoint[A](step: (A) => A, goodEnough: (A,A) => Boolean) {
  def apply(initial: A): A = {
    val newValue = step(initial)
    if (goodEnough(initial, newValue)) {
      newValue
    } else {
      apply(newValue)
    }
  }
  
  def applyIfNecessary(initial: A): A = {
    if (goodEnough(initial, initial)) {
      initial
    } else {
      apply(initial)
    }
  }
}

object FixedPoint {
  def apply[A](step: (A) => A, goodEnough: (A,A) => Boolean): FixedPoint[A] =
    new FixedPoint(step, goodEnough)
}