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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
(** return a unique identifier *)
let uuid () =
let rand =
let s = Random.State.make_self_init () in
fun () -> Random.State.bits s
in
Digest.to_hex (Digest.string (string_of_int (rand ())))
let list_unique l =
let seen = Hashtbl.create (List.length l) in
let rec add acc = function
| hd :: tl when not (Hashtbl.mem seen hd) ->
Hashtbl.add seen hd () ;
add (hd :: acc) tl
| _ :: tl -> add acc tl
| [] -> acc
in
add [] l
let memo f =
let h = Hashtbl.create 1023 in
fun i ->
try Hashtbl.find h i
with Not_found ->
let r = f i in
Hashtbl.add h i r ;
r
let timestamp () =
let tm = Unix.localtime (Unix.time ()) in
Printf.sprintf
"%04d-%02d-%02d %02d:%02d:%02d"
(tm.Unix.tm_year + 1900)
(tm.Unix.tm_mon + 1)
tm.Unix.tm_mday
tm.Unix.tm_hour
tm.Unix.tm_min
tm.Unix.tm_sec
let max32int =
if Int32.to_int Int32.max_int < 0 then max_int else Int32.to_int Int32.max_int
type label = string
module type Messages = sig
type t
val create : ?enabled:bool -> label -> t
val eprintf : ?raw:bool -> t -> ('a, unit, string, unit) format4 -> 'a
val enable : label -> unit
val disable : label -> unit
val all_disabled : unit -> unit
val all_enabled : unit -> unit
val avalaible : unit -> label list
val is_enabled : label -> bool
end
(** Debug messages are printed immediately on stderr.
* They can enabled or disabled (default) *)
module MakeMessages (X : sig
val label : string
end) =
struct
type t = { label : string; mutable enabled : bool }
let messages = Hashtbl.create 10
let clean label =
try
let s = Filename.chop_extension (Filename.basename label) in
String.capitalize_ascii s
with Invalid_argument _ -> label
let create ?(enabled = false) label =
if label = "" then (
Printf.eprintf "Logging Label Empty\n" ;
Stdlib.exit 64)
else if not (Hashtbl.mem messages label) then (
let t = { label = clean label; enabled } in
Hashtbl.add messages (clean label) t ;
t)
else (
Printf.eprintf "The label (%s) %s already exists\n" X.label label ;
Stdlib.exit 64)
let eprintf ?(raw = false) t fmt =
Printf.ksprintf
(if t.enabled then fun s ->
if raw then Printf.eprintf "(%s)%s: %s" X.label t.label s
else Printf.eprintf "(%s)%s: %s\n%!" X.label t.label s
else ignore)
fmt
let onoff s b =
try
let t = Hashtbl.find messages s in
t.enabled <- b
with Not_found -> Printf.eprintf "Warning: debug label %s not found\n" s
let all_enabled () = Hashtbl.iter (fun _ t -> t.enabled <- true) messages
let all_disabled () = Hashtbl.iter (fun _ t -> t.enabled <- false) messages
let enable s = onoff s true
let disable s = onoff s false
let avalaible () = Hashtbl.fold (fun k _ acc -> k :: acc) messages []
let is_enabled s =
try
let t = Hashtbl.find messages s in
t.enabled
with Not_found ->
Printf.eprintf "Warning: debug label %s not found\n" s ;
false
end
module Info = MakeMessages (struct
let label = "I"
end)
module Notice = MakeMessages (struct
let label = "N"
end)
module Warning = MakeMessages (struct
let label = "W"
end)
module Debug = MakeMessages (struct
let label = "D"
end)
module Logging (X : sig
val label : string
end) =
struct
let it = Info.create X.label
let info fmt = Info.eprintf it fmt
let nt = Notice.create X.label
let notice fmt = Notice.eprintf nt fmt
let wt = Warning.create ~enabled:true X.label
let warning fmt = Warning.eprintf wt fmt
let dt = Debug.create X.label
let debug fmt = Debug.eprintf dt fmt
let fatal fmt =
let l = Printf.sprintf "Fatal error in module %s: " X.label in
Printf.kprintf
(fun s ->
Printf.eprintf "%s\n %s\n%!" l s ;
Stdlib.exit 64)
fmt
end
include Logging (struct
let label = "dose_common.util"
end)
(** Printf bars are printed immediately on stderr.
* they can be enabled or disabled (default) *)
module Progress = struct
type t =
{ name : string;
buffer : Buffer.t;
mutable total : int;
mutable perc : int;
mutable rotation : int;
mutable enabled : bool;
mutable unbounded : bool
}
let columns = 75
let full = " %100.0\n"
let rotate = "|/-\\"
let bars = Hashtbl.create 10
let create ?(enabled = false) ?(total = 0) ?(unbounded = false) s =
let c =
{ name = s;
buffer = Buffer.create columns;
total = (if unbounded then 100 else total);
perc = 0;
rotation = 0;
enabled;
unbounded
}
in
Hashtbl.add bars s c ;
c
let enable s =
try
let t = Hashtbl.find bars s in
t.enabled <- true
with Not_found -> warning "Progress Bar %s not found" s
let disable s =
try
let t = Hashtbl.find bars s in
t.enabled <- false
with Not_found -> warning "Progress Bar %s not found" s
let available () = Hashtbl.fold (fun k _ acc -> k :: acc) bars []
let set_total c total =
if not c.unbounded then c.total <- total
else warning "%s is an unbounded progress bar. Cannot set total" c.name
let reset c =
Buffer.clear c.buffer ;
c.perc <- 0 ;
c.rotation <- 0
let progress ?(i = 1) c =
if c.enabled then (
c.perc <- c.perc + i ;
Buffer.clear c.buffer ;
Buffer.add_char c.buffer '\r' ;
Buffer.add_string c.buffer c.name ;
let f = floor (1000.0 *. float c.perc /. float c.total) in
let f = f /. 10.0 in
if f = 100.0 && not c.unbounded then Buffer.add_string c.buffer full
else (
c.rotation <- (1 + c.rotation) land 3 ;
Printf.bprintf c.buffer "%c %%%4.1f" rotate.[c.rotation] f) ;
Printf.eprintf "%s%!" (Buffer.contents c.buffer))
end
(** Timers are printed all together when the function dump is called.
* they can be enabled or disabled (default) *)
module Timer = struct
type t =
{ name : string;
mutable total : float;
mutable last : float;
mutable is_in : bool;
mutable enabled : bool
}
let timers = Hashtbl.create 10
let gettimeofday = ref (fun _ -> 0.)
let () = gettimeofday := Unix.gettimeofday
let pp_timer fmt c =
if c.total = 0. then Format.fprintf fmt "Timer %s: n/a@." c.name
else Format.fprintf fmt "Timer %s: %f.@." c.name c.total
let dump fmt () =
Hashtbl.iter (fun _ c -> if c.enabled then pp_timer fmt c) timers
let create ?(enabled = false) s =
let c = { name = s; total = 0.; last = 0.; is_in = false; enabled } in
Hashtbl.add timers s c ;
c
let enable s =
try
let t = Hashtbl.find timers s in
t.enabled <- true
with Not_found -> warning "Timer %s not found" s
let available () = Hashtbl.fold (fun k _ acc -> k :: acc) timers []
let start c =
c.is_in <- true ;
c.last <- !gettimeofday ()
let stop c x =
assert c.is_in ;
c.is_in <- false ;
c.total <- c.total +. (!gettimeofday () -. c.last) ;
x
end
module StringHashtbl = Hashtbl.Make (struct
type t = string
let equal (a : string) (b : string) = a = b
let hash s = Hashtbl.hash s
end)
let hits = ref 0
let miss = ref 0
let hashcons table x =
try
let s = StringHashtbl.find table x in
incr hits ;
s
with Not_found ->
incr miss ;
StringHashtbl.add table x x ;
x
module StringPairHashtbl = Hashtbl.Make (struct
type t = string * string
let equal (a : string * string) (b : string * string) = a = b
let hash s = Hashtbl.hash s
end)
module IntHashtbl = Hashtbl.Make (struct
type t = int
let equal (a : int) (b : int) = a = b
let hash i = Hashtbl.hash i
end)
module IntPairHashtbl = Hashtbl.Make (struct
type t = int * int
let equal (a : int * int) (b : int * int) = a = b
let hash i = Hashtbl.hash i
end)
let range i j =
let rec aux acc n = if n < i then acc else aux (n :: acc) (n - 1) in
aux [] j
let string_of_list ?(delim = ("", "")) ?(sep = ",") string_of_item l =
let buf = Buffer.create 1023 in
let rec aux = function
| [] -> assert false
| [last] ->
Buffer.add_string buf (string_of_item last)
| item :: tl ->
Buffer.add_string buf (string_of_item item) ;
Buffer.add_string buf sep ;
aux tl
in
Buffer.add_string buf (fst delim) ;
(match l with
| [] -> ()
| [sole] -> Buffer.add_string buf (string_of_item sole)
| _ -> aux l) ;
Buffer.add_string buf (snd delim) ;
Buffer.contents buf
class type projection =
object
method add : int -> unit
method inttovar : int -> int
method vartoint : int -> int
method size : int
end
(** associate a sat solver variable to a package id *)
class intprojection size =
object
val vartoint = IntHashtbl.create (2 * size)
val inttovar = Array.make size 0
val size = size
val mutable counter = 0
(** add a package id to the map *)
method add v =
if size = 0 then assert false ;
if counter > size - 1 then assert false ;
IntHashtbl.add vartoint v counter ;
inttovar.(counter) <- v ;
counter <- counter + 1
(** given a package id return a sat solver variable
raise Not_found if the package id is not known *)
method vartoint v = IntHashtbl.find vartoint v
method inttovar i =
if i >= size then fatal "out of boundary i = %d size = %d" i size ;
inttovar.(i)
method size = size
end
class identity =
object
method add (_ : int) =
warning "There is no point of adding element to the identity projection" ;
()
method vartoint (v : int) = v
method inttovar (v : int) = v
method size =
warning "This is the identity projetion. size unknown" ;
0
end