-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbag.ml
227 lines (175 loc) · 5.69 KB
/
bag.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(**************************************************************************)
(* *)
(* Copyright (C) Jean-Christophe Filliatre *)
(* *)
(* This software is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU Library General Public *)
(* License version 2.1, with the special exception on linking *)
(* described in file LICENSE. *)
(* *)
(* This software is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)
(* *)
(**************************************************************************)
module Make(X: sig
type t
val compare: t -> t -> int
end) = struct
module M = Map.Make(X)
type elt = X.t
type t = int M.t
(** invariant: multiplicities are all > 0 *)
let empty =
M.empty
let is_empty =
M.is_empty
let mem =
M.mem
let occ x b =
try M.find x b with Not_found -> 0
let add x ?(mult=1) b =
if mult < 0 then invalid_arg "add";
if mult = 0 then b else
try let m = M.find x b in M.add x (m + mult) b
with Not_found -> M.add x mult b
let update x f b =
let f o =
let m = f (match o with None -> 0 | Some m -> m) in
if m < 0 then invalid_arg "update";
if m = 0 then None else Some m in
M.update x f b
let singleton x =
M.add x 1 M.empty
let remove x ?(mult=1) b =
if mult < 0 then invalid_arg "remove";
if mult = 0 then b else
M.update x
(function | None | Some 1 -> None
| Some m when m <= mult -> None
| Some m -> Some (m - mult)) b
let remove_all =
M.remove
let merge f b1 b2 =
let f x o1 o2 =
let m1 = match o1 with None -> 0 | Some m -> m in
let m2 = match o2 with None -> 0 | Some m -> m in
let m = f x m1 m2 in
if m < 0 then invalid_arg "merge";
if m = 0 then None else Some m in
M.merge f b1 b2
let cardinal b =
M.fold (fun _ m c -> m + c) b 0
let elements =
M.bindings
let min_elt =
M.min_binding
let min_elt_opt =
M.min_binding_opt
let max_elt =
M.max_binding
let max_elt_opt =
M.max_binding_opt
let choose =
M.choose
let choose_opt =
M.choose_opt
let union b1 b2 =
M.merge (fun _ o1 o2 -> match o1, o2 with
| None, None -> None
| None, Some m | Some m, None -> Some m
| Some m1, Some m2 -> Some (max m1 m2)) b1 b2
let sum b1 b2 =
M.merge (fun _ o1 o2 -> match o1, o2 with
| None, None -> None
| None, Some m | Some m, None -> Some m
| Some m1, Some m2 -> Some (m1 + m2)) b1 b2
let inter b1 b2 =
M.merge (fun _ o1 o2 -> match o1, o2 with
| None, None
| None, Some _ | Some _, None -> None
| Some m1, Some m2 -> Some (min m1 m2)) b1 b2
let diff b1 b2 =
M.merge (fun _ o1 o2 -> match o1, o2 with
| None, _ -> None
| Some m, None -> Some m
| Some m1, Some m2 when m1 <= m2 -> None
| Some m1, Some m2 -> Some (m1 - m2)) b1 b2
let disjoint b1 b2 =
M.for_all (fun x1 _ -> not (mem x1 b2)) b1
let included b1 b2 =
M.for_all (fun x1 m1 -> m1 <= occ x1 b2) b1
let iter =
M.iter
let fold =
M.fold
let for_all =
M.for_all
let exists =
M.exists
let filter =
M.filter
let partition =
M.partition
let split x b =
let l, m, r = M.split x b in
l, (match m with None -> 0 | Some m -> m), r
let find_first =
M.find_first
let find_first_opt =
M.find_first_opt
let find_last =
M.find_last
let find_last_opt =
M.find_last_opt
let map f =
let f m = let m = f m in if m <= 0 then invalid_arg "map"; m in
M.map f
let mapi f =
let f x m = let m = f x m in if m <= 0 then invalid_arg "mapi"; m in
M.mapi f
let mul b n =
if n < 0 then invalid_arg "mul";
if n = 0 then empty else map (fun m -> m * n) b
let div b1 b2 =
if is_empty b2 then 0, b1 else
try
let update x m1 q =
let m2 = occ x b2 in
if m2 = 0 || m2 > m1 then raise Exit;
min q (m1 / m2) in
let q = fold update b1 max_int in
assert (q > 0);
let remainder x m1 r =
let mult = m1 - q * occ x b2 in
add ~mult x r in
let r = fold remainder b1 empty in
q, r
with Exit ->
0, b1
let divi b n =
if n <= 0 then invalid_arg "divi";
let update x m (q, r) = add ~mult:(m / n) x q, add ~mult:(m mod n) x r in
fold update b (empty, empty)
let compare =
M.compare Stdlib.compare
let equal =
M.equal (==)
let to_seq =
M.to_seq
let to_seq_from =
M.to_seq_from
let add_seq s b =
Seq.fold_left (fun b (x, mult) -> add x ~mult b) b s
let of_seq s =
add_seq s empty
let print print_elt fmt b =
Format.fprintf fmt "{@[";
let first = ref true in
iter (fun x m ->
if not !first then Format.fprintf fmt ",";
first := false;
Format.fprintf fmt "@ %a:%d" print_elt x m) b;
if not !first then Format.fprintf fmt " ";
Format.fprintf fmt "@]}"
end