src/Pure/General/space.scala
author wenzelm
Mon, 04 Nov 2024 20:55:01 +0100
changeset 81340 30f7eb65d679
parent 77708 f137bf5d3d94
permissions -rw-r--r--
clarified signature; clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
77109
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/space.scala
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     3
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     4
Storage space based on bytes.
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     5
*/
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     6
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     7
package isabelle
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     8
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
     9
77114
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    10
import java.util.Locale
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    11
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    12
import scala.annotation.tailrec
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    13
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    14
77109
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    15
object Space {
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    16
  def bytes(bs: Long): Space = new Space(bs)
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    17
  val zero: Space = bytes(0L)
77114
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    18
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    19
  private val units: List[String] = List("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB")
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    20
77116
00d1db8e496e clarified signature;
wenzelm
parents: 77114
diff changeset
    21
  def B(x: Double): Space = bytes(x.round)
00d1db8e496e clarified signature;
wenzelm
parents: 77114
diff changeset
    22
  def KiB(x: Double): Space = B(x * 1024)
77114
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    23
  def MiB(x: Double): Space = KiB(x * 1024)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    24
  def GiB(x: Double): Space = MiB(x * 1024)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    25
  def TiB(x: Double): Space = GiB(x * 1024)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    26
  def PiB(x: Double): Space = TiB(x * 1024)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    27
  def EiB(x: Double): Space = PiB(x * 1024)
77109
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    28
}
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    29
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    30
final class Space private(val bytes: Long) extends AnyVal {
77687
07e2cafcc97e more operations;
wenzelm
parents: 77122
diff changeset
    31
  def + (other: Space): Space = new Space(bytes + other.bytes)
07e2cafcc97e more operations;
wenzelm
parents: 77122
diff changeset
    32
  def - (other: Space): Space = new Space(bytes - other.bytes)
07e2cafcc97e more operations;
wenzelm
parents: 77122
diff changeset
    33
  def * (scalar: Double): Space = new Space((bytes * scalar).round)
77694
ea509b0bfc80 more operations;
wenzelm
parents: 77687
diff changeset
    34
  def / (other: Space): Double = B / other.B
77687
07e2cafcc97e more operations;
wenzelm
parents: 77122
diff changeset
    35
77120
8c14be9beb58 more operations;
wenzelm
parents: 77116
diff changeset
    36
  def is_proper: Boolean = bytes > 0
8c14be9beb58 more operations;
wenzelm
parents: 77116
diff changeset
    37
  def is_relevant: Boolean = MiB >= 1.0
8c14be9beb58 more operations;
wenzelm
parents: 77116
diff changeset
    38
77708
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 77694
diff changeset
    39
  def used(free: Space): Space = new Space((bytes - free.bytes) max 0)
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 77694
diff changeset
    40
  def used_fraction(free: Space): Double =
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 77694
diff changeset
    41
    if (is_proper && used(free).is_proper) used(free).B / B else 0.0
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 77694
diff changeset
    42
77116
00d1db8e496e clarified signature;
wenzelm
parents: 77114
diff changeset
    43
  def B: Double = bytes.toDouble
00d1db8e496e clarified signature;
wenzelm
parents: 77114
diff changeset
    44
  def KiB: Double = B / 1024
77114
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    45
  def MiB: Double = KiB / 1024
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    46
  def GiB: Double = MiB / 1024
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    47
  def TiB: Double = GiB / 1024
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    48
  def PiB: Double = TiB / 1024
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    49
  def EiB: Double = PiB / 1024
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    50
77122
wenzelm
parents: 77120
diff changeset
    51
  override def toString: String = Value.Long(bytes)
77114
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    52
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    53
  def print: String = {
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    54
    @tailrec def print_unit(x: Double, units: List[String]): String =
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    55
      if (x.abs < 1024 || units.tail.isEmpty) {
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    56
        val s = String.format(Locale.ROOT, "%.1f", x.asInstanceOf[AnyRef])
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    57
        Library.perhaps_unsuffix(".0", s) + " " + units.head
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    58
      }
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    59
      else print_unit(x / 1024, units.tail)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    60
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    61
    print_unit(bytes.toDouble, Space.units)
6608de52a3b5 support units, e.g. java.lang.Long.MAX_VALUE is 8 EiB;
wenzelm
parents: 77109
diff changeset
    62
  }
77120
8c14be9beb58 more operations;
wenzelm
parents: 77116
diff changeset
    63
8c14be9beb58 more operations;
wenzelm
parents: 77116
diff changeset
    64
  def print_relevant: Option[String] = if (is_relevant) Some(print) else None
77109
e3a2b3536030 prefer typed bytes count, but retain toString of original Long for robustness of Java/Scala string composition;
wenzelm
parents:
diff changeset
    65
}