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