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

geotrellis.statistics.MapHistogram.scala Maven / Gradle / Ivy

The newest version!
package geotrellis.statistics

import geotrellis._
import scala.collection.mutable.Map
import scala.math.{max, min}
import scala.util.Sorting

object MapHistogram {
  def apply() = new MapHistogram(Map.empty[Int, Int], 0)

  def apply(counts:Map[Int, Int], total:Int) = new MapHistogram(counts, total)
}

/**
  * Data object representing a histogram that uses a Map (as in hashtable map/dictionary) for internal storage.
  *
  */
class MapHistogram(counts:Map[Int, Int], var total:Int) extends MutableHistogram {
  def getTotalCount = this.total

  def mutable() = MapHistogram(this.counts.clone, this.total)

  def getValues() = {
    var keys = rawValues()
    Sorting.quickSort(keys)
    keys
  }

  def rawValues() = counts.keys.toArray

  def foreachValue(f:Int => Unit) {
    counts.keys.foreach(f)
  }

  def uncountItem(i:Int) {
    if (isData(i)) {
      val opt = this.counts.remove(i)
      if (opt.isDefined) {
        this.total -= opt.get
      }
    }
  }

  def countItem(i:Int, count:Int=1) {
    if (isData(i)) {
      if (this.counts.contains(i)) {
        this.counts(i) += count
      } else {
        this.counts(i) = count
      }
      this.total += count
    }
  }

  def setItem(i:Int, count:Int) {
    if (isData(i)) {
      if (this.counts.contains(i)) {
        this.total = this.total + count - this.counts(i)
        this.counts(i) = count
      } else {
        this.counts(i) = count
        this.total += count
      }
    }
  }

  def getItemCount(i:Int) = this.counts.getOrElse(i, 0)

  def getMinValue = getMinMaxValues._1
  def getMaxValue = getMinMaxValues._2

  override def getMinMaxValues = {
    val it = counts.keys.toIterator
    if (it.isEmpty) {
      (Int.MaxValue, Int.MinValue)
    } else {
      var zmin = it.next()
      var zmax = zmin
      for (z <- it) {
        if (z < zmin) {
          zmin = z
        } else if (z > zmax) {
          zmax = z
        }
      }
      (zmin, zmax)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy