src/Pure/item_net.ML
author blanchet
Mon May 19 23:43:53 2014 +0200 (2014-05-19)
changeset 57008 10f68b83b474
parent 55741 b969263fcf02
child 59058 a78612c67ec0
permissions -rw-r--r--
use E 1.8's auto scheduler option
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
lammich@53941
    15
  val retrieve_matching: 'a T -> term -> 'a list
wenzelm@36296
    16
  val member: 'a T -> 'a -> bool
wenzelm@8298
    17
  val merge: 'a T * 'a T -> 'a T
wenzelm@33372
    18
  val remove: 'a -> 'a T -> 'a T
wenzelm@33372
    19
  val update: 'a -> 'a T -> 'a T
wenzelm@8298
    20
end;
wenzelm@8298
    21
wenzelm@30559
    22
structure Item_Net: ITEM_NET =
wenzelm@8298
    23
struct
wenzelm@8298
    24
wenzelm@30559
    25
(* datatype *)
wenzelm@8298
    26
wenzelm@8298
    27
datatype 'a T =
wenzelm@30559
    28
  Items of {
wenzelm@8298
    29
    eq: 'a * 'a -> bool,
wenzelm@33372
    30
    index: 'a -> term list,
wenzelm@30559
    31
    content: 'a list,
wenzelm@8298
    32
    next: int,
wenzelm@12385
    33
    net: (int * 'a) Net.net};
wenzelm@8298
    34
wenzelm@30559
    35
fun mk_items eq index content next net =
wenzelm@30559
    36
  Items {eq = eq, index = index, content = content, next = next, net = net};
wenzelm@8298
    37
wenzelm@33372
    38
fun init eq index = mk_items eq index [] ~1 Net.empty;
wenzelm@33372
    39
wenzelm@30559
    40
fun content (Items {content, ...}) = content;
wenzelm@42810
    41
fun length items = List.length (content items);
wenzelm@30559
    42
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
lammich@53941
    43
fun retrieve_matching (Items {net, ...}) = order_list o Net.match_term net;
wenzelm@8298
    44
wenzelm@8298
    45
wenzelm@33372
    46
(* standard operations *)
wenzelm@8298
    47
wenzelm@33721
    48
fun member (Items {eq, index, content, net, ...}) x =
wenzelm@33721
    49
  (case index x of
wenzelm@33721
    50
    [] => Library.member eq content x
wenzelm@33721
    51
  | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
wenzelm@8298
    52
wenzelm@33372
    53
fun cons x (Items {eq, index, content, next, net}) =
wenzelm@30559
    54
  mk_items eq index (x :: content) (next - 1)
wenzelm@33721
    55
    (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
wenzelm@33372
    56
wenzelm@55741
    57
fun merge
wenzelm@55741
    58
    (items1 as Items {net = net1, ...},
wenzelm@55741
    59
     items2 as Items {net = net2, content = content2, ...}) =
wenzelm@55741
    60
  if pointer_eq (net1, net2) then items1
wenzelm@55741
    61
  else if Net.is_empty net1 then items2
wenzelm@55741
    62
  else fold_rev (fn y => if member items1 y then I else cons y) content2 items1;
wenzelm@8298
    63
wenzelm@33372
    64
fun remove x (items as Items {eq, index, content, next, net}) =
wenzelm@33372
    65
  if member items x then
wenzelm@33372
    66
    mk_items eq index (Library.remove eq x content) next
wenzelm@33372
    67
      (fold (fn t => Net.delete_term_safe (eq o pairself #2) (t, (0, x))) (index x) net)
wenzelm@33372
    68
  else items;
wenzelm@8298
    69
wenzelm@33372
    70
fun update x items = cons x (remove x items);
wenzelm@8298
    71
wenzelm@8298
    72
end;