All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kyo.stats.Histogram.scala Maven / Gradle / Ivy

There is a newer version: 0.8.5
Show newest version
package kyo.stats

import kyo._
import kyo.ios._

case class Histogram(unsafe: Histogram.Unsafe) extends AnyVal {

  def observe(v: Double): Unit > IOs =
    IOs(unsafe.observe(v))

  def observe(v: Double, b: Attributes): Unit > IOs =
    IOs(unsafe.observe(v, b))

  def attributes(b: Attributes): Histogram =
    Histogram(unsafe.attributes(b))
}

object Histogram {

  abstract class Unsafe {
    def observe(v: Double): Unit
    def observe(v: Double, b: Attributes): Unit
    def attributes(b: Attributes): Unsafe
  }

  val noop: Histogram =
    Histogram(
        new Unsafe {
          def observe(v: Double)                = ()
          def observe(v: Double, b: Attributes) = ()
          def attributes(b: Attributes)         = this
        }
    )

  def all(l: List[Histogram]): Histogram =
    l.filter(_ != noop) match {
      case Nil =>
        noop
      case h :: Nil =>
        h
      case l =>
        Histogram(
            new Unsafe {
              def observe(v: Double) = {
                var c = l
                while (c ne Nil) {
                  c.head.unsafe.observe(v)
                  c = c.tail
                }
              }
              def observe(v: Double, b: Attributes) = {
                var c = l
                while (c ne Nil) {
                  c.head.unsafe.observe(v, b)
                  c = c.tail
                }
              }
              def attributes(b: Attributes) =
                all(l.map(_.attributes(b))).unsafe
            }
        )
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy