src/Pure/item_net.ML
author wenzelm
Sun Nov 11 20:31:46 2012 +0100 (2012-11-11)
changeset 50081 9b92ee8dec98
parent 42810 2425068fe13a
child 53941 54874871aa06
permissions -rw-r--r--
tuned;
wenzelm@30559
     1
(*  Title:      Pure/item_net.ML
wenzelm@8298
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8298
     3
wenzelm@30559
     4
Efficient storage of items indexed by terms; preserves order and
wenzelm@30559
     5
prefers later entries.
wenzelm@8298
     6
*)
wenzelm@8298
     7
wenzelm@30559
     8
signature ITEM_NET =
wenzelm@8298
     9
sig
wenzelm@8298
    10
  type 'a T
wenzelm@33372
    11
  val init: ('a * 'a -> bool) -> ('a -> term list) -> 'a T
wenzelm@30559
    12
  val content: 'a T -> 'a list
wenzelm@42810
    13
  val length: 'a T -> int
wenzelm@11776
    14
  val retrieve: 'a T -> term -> 'a list
wenzelm@36296
    15
  val member: 'a T -> 'a -> bool
wenzelm@8298
    16
  val merge: 'a T * 'a T -> 'a T
wenzelm@33372
    17
  val remove: 'a -> 'a T -> 'a T
wenzelm@33372
    18
  val update: 'a -> 'a T -> 'a T
wenzelm@8298
    19
end;
wenzelm@8298
    20
wenzelm@30559
    21
structure Item_Net: ITEM_NET =
wenzelm@8298
    22
struct
wenzelm@8298
    23
wenzelm@30559
    24
(* datatype *)
wenzelm@8298
    25
wenzelm@8298
    26
datatype 'a T =
wenzelm@30559
    27
  Items of {
wenzelm@8298
    28
    eq: 'a * 'a -> bool,
wenzelm@33372
    29
    index: 'a -> term list,
wenzelm@30559
    30
    content: 'a list,
wenzelm@8298
    31
    next: int,
wenzelm@12385
    32
    net: (int * 'a) Net.net};
wenzelm@8298
    33
wenzelm@30559
    34
fun mk_items eq index content next net =
wenzelm@30559
    35
  Items {eq = eq, index = index, content = content, next = next, net = net};
wenzelm@8298
    36
wenzelm@33372
    37
fun init eq index = mk_items eq index [] ~1 Net.empty;
wenzelm@33372
    38
wenzelm@30559
    39
fun content (Items {content, ...}) = content;
wenzelm@42810
    40
fun length items = List.length (content items);
wenzelm@30559
    41
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
wenzelm@8298
    42
wenzelm@8298
    43
wenzelm@33372
    44
(* standard operations *)
wenzelm@8298
    45
wenzelm@33721
    46
fun member (Items {eq, index, content, net, ...}) x =
wenzelm@33721
    47
  (case index x of
wenzelm@33721
    48
    [] => Library.member eq content x
wenzelm@33721
    49
  | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
wenzelm@8298
    50
wenzelm@33372
    51
fun cons x (Items {eq, index, content, next, net}) =
wenzelm@30559
    52
  mk_items eq index (x :: content) (next - 1)
wenzelm@33721
    53
    (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
wenzelm@33372
    54
wenzelm@33372
    55
fun merge (items1, Items {content = content2, ...}) =
wenzelm@33372
    56
  fold_rev (fn y => if member items1 y then I else cons y) content2 items1;
wenzelm@8298
    57
wenzelm@33372
    58
fun remove x (items as Items {eq, index, content, next, net}) =
wenzelm@33372
    59
  if member items x then
wenzelm@33372
    60
    mk_items eq index (Library.remove eq x content) next
wenzelm@33372
    61
      (fold (fn t => Net.delete_term_safe (eq o pairself #2) (t, (0, x))) (index x) net)
wenzelm@33372
    62
  else items;
wenzelm@8298
    63
wenzelm@33372
    64
fun update x items = cons x (remove x items);
wenzelm@8298
    65
wenzelm@8298
    66
end;