src/Pure/item_net.ML
author wenzelm
Fri Oct 07 21:16:48 2016 +0200 (2016-10-07)
changeset 64092 95469c544b82
parent 62165 b10046b14dd8
child 68126 5da8b97d9183
permissions -rw-r--r--
accept obscure timezone used in 2011;
     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 length: 'a T -> int
    14   val retrieve: 'a T -> term -> 'a list
    15   val retrieve_matching: 'a T -> term -> 'a list
    16   val member: 'a T -> 'a -> bool
    17   val lookup: 'a T -> 'a -> 'a list
    18   val merge: 'a T * 'a T -> 'a T
    19   val remove: 'a -> 'a T -> 'a T
    20   val update: 'a -> 'a T -> 'a T
    21   val filter: ('a -> bool) -> 'a T -> 'a T
    22 end;
    23 
    24 structure Item_Net: ITEM_NET =
    25 struct
    26 
    27 (* datatype *)
    28 
    29 datatype 'a T =
    30   Items of {
    31     eq: 'a * 'a -> bool,
    32     index: 'a -> term list,
    33     content: 'a list,
    34     next: int,
    35     net: (int * 'a) Net.net};
    36 
    37 fun mk_items eq index content next net =
    38   Items {eq = eq, index = index, content = content, next = next, net = net};
    39 
    40 fun init eq index = mk_items eq index [] ~1 Net.empty;
    41 
    42 fun content (Items {content, ...}) = content;
    43 fun length items = List.length (content items);
    44 fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
    45 fun retrieve_matching (Items {net, ...}) = order_list o Net.match_term net;
    46 
    47 
    48 (* standard operations *)
    49 
    50 fun member (Items {eq, index, content, net, ...}) x =
    51   (case index x of
    52     [] => Library.member eq content x
    53   | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
    54 
    55 fun lookup (Items {eq, index, content, net, ...}) x =
    56   (case index x of
    57     [] => content
    58   | t :: _ => map #2 (Net.unify_term net t))
    59   |> filter (fn y => eq (x, y));
    60 
    61 fun cons x (Items {eq, index, content, next, net}) =
    62   mk_items eq index (x :: content) (next - 1)
    63     (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
    64 
    65 fun merge
    66     (items1 as Items {net = net1, ...},
    67      items2 as Items {net = net2, content = content2, ...}) =
    68   if pointer_eq (net1, net2) then items1
    69   else if Net.is_empty net1 then items2
    70   else fold_rev (fn y => if member items1 y then I else cons y) content2 items1;
    71 
    72 fun remove x (items as Items {eq, index, content, next, net}) =
    73   if member items x then
    74     mk_items eq index (Library.remove eq x content) next
    75       (fold (fn t => Net.delete_term_safe (eq o apply2 #2) (t, (0, x))) (index x) net)
    76   else items;
    77 
    78 fun update x items = cons x (remove x items);
    79 
    80 fun filter pred items =
    81   fold (fn x => not (pred x) ? remove x) (content items) items;
    82 
    83 end;