16464
|
1 |
(* Title: Pure/General/ord_list.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Ordered lists without duplicates -- a light-weight representation of
|
16497
|
6 |
finite sets, all operations take linear time and economize heap usage.
|
16464
|
7 |
*)
|
|
8 |
|
|
9 |
signature ORD_LIST =
|
|
10 |
sig
|
|
11 |
val member: ('b * 'a -> order) -> 'a list -> 'b -> bool
|
|
12 |
val insert: ('a * 'a -> order) -> 'a -> 'a list -> 'a list
|
|
13 |
val remove: ('b * 'a -> order) -> 'b -> 'a list -> 'a list
|
16511
|
14 |
val subset: ('b * 'a -> order) -> 'b list * 'a list -> bool
|
|
15 |
val eq_set: ('b * 'a -> order) -> 'b list * 'a list -> bool
|
16464
|
16 |
val union: ('a * 'a -> order) -> 'a list -> 'a list -> 'a list
|
16497
|
17 |
val inter: ('b * 'a -> order) -> 'b list -> 'a list -> 'a list
|
|
18 |
val subtract: ('b * 'a -> order) -> 'b list -> 'a list -> 'a list
|
16464
|
19 |
end;
|
|
20 |
|
|
21 |
structure OrdList: ORD_LIST =
|
|
22 |
struct
|
|
23 |
|
16511
|
24 |
|
|
25 |
(* single elements *)
|
|
26 |
|
|
27 |
fun find_index ord list x =
|
|
28 |
let
|
16811
|
29 |
fun find i [] = ~ i
|
16511
|
30 |
| find i (y :: ys) =
|
|
31 |
(case ord (x, y) of
|
16811
|
32 |
LESS => ~ i
|
16511
|
33 |
| EQUAL => i
|
|
34 |
| GREATER => find (i + 1) ys);
|
16811
|
35 |
in find 1 list end;
|
16497
|
36 |
|
16811
|
37 |
fun member ord list x = find_index ord list x > 0;
|
16464
|
38 |
|
|
39 |
fun insert ord x list =
|
|
40 |
let
|
16811
|
41 |
fun insrt 1 ys = x :: ys
|
16511
|
42 |
| insrt i (y :: ys) = y :: insrt (i - 1) ys;
|
16811
|
43 |
val idx = find_index ord list x;
|
|
44 |
in if idx > 0 then list else insrt (~ idx) list end;
|
16464
|
45 |
|
|
46 |
fun remove ord x list =
|
|
47 |
let
|
16811
|
48 |
fun rmove 1 (_ :: ys) = ys
|
16511
|
49 |
| rmove i (y :: ys) = y :: rmove (i - 1) ys;
|
16811
|
50 |
val idx = find_index ord list x;
|
|
51 |
in if idx > 0 then rmove idx list else list end;
|
16511
|
52 |
|
|
53 |
|
|
54 |
(* lists as sets *)
|
|
55 |
|
|
56 |
nonfix subset;
|
|
57 |
fun subset ord (list1, list2) =
|
|
58 |
let
|
|
59 |
fun sub [] _ = true
|
|
60 |
| sub _ [] = false
|
|
61 |
| sub (lst1 as x :: xs) (y :: ys) =
|
16464
|
62 |
(case ord (x, y) of
|
16511
|
63 |
LESS => false
|
|
64 |
| EQUAL => sub xs ys
|
|
65 |
| GREATER => sub lst1 ys);
|
|
66 |
in sub list1 list2 end;
|
|
67 |
|
16534
|
68 |
fun eq_set ord lists = (list_ord ord lists = EQUAL);
|
16511
|
69 |
|
|
70 |
|
|
71 |
(* algebraic operations *)
|
|
72 |
|
|
73 |
exception SAME;
|
|
74 |
fun handle_same f x = f x handle SAME => x;
|
16464
|
75 |
|
16497
|
76 |
(*union: insert elements of first list into second list*)
|
16464
|
77 |
nonfix union;
|
16497
|
78 |
fun union ord list1 list2 =
|
|
79 |
let
|
|
80 |
fun unio [] _ = raise SAME
|
|
81 |
| unio xs [] = xs
|
|
82 |
| unio (lst1 as x :: xs) (lst2 as y :: ys) =
|
|
83 |
(case ord (x, y) of
|
|
84 |
LESS => x :: handle_same (unio xs) lst2
|
|
85 |
| EQUAL => y :: unio xs ys
|
|
86 |
| GREATER => y :: unio lst1 ys);
|
16886
|
87 |
in handle_same (unio list1) list2 end;
|
16464
|
88 |
|
16497
|
89 |
(*intersection: filter second list for elements present in first list*)
|
16464
|
90 |
nonfix inter;
|
16497
|
91 |
fun inter ord list1 list2 =
|
|
92 |
let
|
|
93 |
fun intr _ [] = raise SAME
|
|
94 |
| intr [] _ = []
|
|
95 |
| intr (lst1 as x :: xs) (lst2 as y :: ys) =
|
|
96 |
(case ord (x, y) of
|
|
97 |
LESS => intr xs lst2
|
|
98 |
| EQUAL => y :: intr xs ys
|
|
99 |
| GREATER => handle_same (intr lst1) ys);
|
|
100 |
in handle_same (intr list1) list2 end;
|
|
101 |
|
|
102 |
(*subtraction: filter second list for elements NOT present in first list*)
|
|
103 |
fun subtract ord list1 list2 =
|
|
104 |
let
|
|
105 |
fun subtr [] _ = raise SAME
|
|
106 |
| subtr _ [] = raise SAME
|
|
107 |
| subtr (lst1 as x :: xs) (lst2 as y :: ys) =
|
|
108 |
(case ord (x, y) of
|
|
109 |
LESS => subtr xs lst2
|
|
110 |
| EQUAL => handle_same (subtr xs) ys
|
|
111 |
| GREATER => y :: subtr lst1 ys);
|
|
112 |
in handle_same (subtr list1) list2 end;
|
16464
|
113 |
|
|
114 |
end;
|