src/Pure/General/cache.scala
author wenzelm
Wed, 11 Nov 2020 21:04:22 +0100
changeset 72575 c7ab83a0c564
parent 71382 6316debd3a9f
child 73023 e15621aa8c72
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68396
7433ee1ed7e3 tuned header;
wenzelm
parents: 68266
diff changeset
     1
/*  Title:      Pure/General/cache.scala
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     3
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     4
cache for partial sharing (weak table).
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     5
*/
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     6
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     7
package isabelle
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     8
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     9
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    10
import java.util.{Collections, WeakHashMap}
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    11
import java.lang.ref.WeakReference
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    12
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    13
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    14
class Cache(initial_size: Int = 131071, max_string: Int = 100)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    15
{
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    16
  private val table =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    17
    Collections.synchronizedMap(new WeakHashMap[Any, WeakReference[Any]](initial_size))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    18
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    19
  def size: Int = table.size
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    20
68266
f13bb379c573 tuned output;
wenzelm
parents: 68265
diff changeset
    21
  override def toString: String = "Cache(" + size + ")"
f13bb379c573 tuned output;
wenzelm
parents: 68265
diff changeset
    22
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    23
  protected def lookup[A](x: A): Option[A] =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    24
  {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    25
    val ref = table.get(x)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    26
    if (ref == null) None
71382
wenzelm
parents: 70536
diff changeset
    27
    else Option(ref.asInstanceOf[WeakReference[A]].get)
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    28
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    29
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    30
  protected def store[A](x: A): A =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    31
  {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    32
    table.put(x, new WeakReference[Any](x))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    33
    x
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    34
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    35
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    36
  protected def cache_string(x: String): String =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    37
  {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    38
    if (x == "") ""
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    39
    else if (x == "true") "true"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    40
    else if (x == "false") "false"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    41
    else if (x == "0.0") "0.0"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    42
    else if (Library.is_small_int(x)) Library.signed_string_of_int(Integer.parseInt(x))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    43
    else {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    44
      lookup(x) match {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    45
        case Some(y) => y
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    46
        case None =>
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    47
          val z = Library.isolate_substring(x)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    48
          if (z.length > max_string) z else store(z)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    49
      }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    50
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    51
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    52
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    53
  // main methods
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    54
  def string(x: String): String = synchronized { cache_string(x) }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    55
}