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
open Printf
module L = BatList
module Log = Dolog.Log
type layer = int * ((PiEltHA.t * int) list)
type t = layer list
let counted_types_to_string (l: (PiEltHA.t * int) list): string =
let buff = Buffer.create 80 in
L.iteri (fun i (x, count) ->
bprintf buff (if i = 0 then "%s:%d" else ",%s:%d")
(PiEltHA.to_string x) count
) l;
Buffer.contents buff
let counted_types_of_string (s: string): (PiEltHA.t * int) list =
let strings = BatString.nsplit s ~by:"," in
L.map (fun str -> Scanf.sscanf str "%s:%d" Utls.make_pair) strings
let layer_to_string ((depth, counted_types): layer): string =
sprintf "%d_%s" depth (counted_types_to_string counted_types)
let layer_of_string (str: string): layer =
Scanf.sscanf str "%d_%s" (fun d s ->
(d, counted_types_of_string s)
)
let to_string (layers: t): string =
let buff = Buffer.create 80 in
L.iteri (fun i layer ->
bprintf buff (if i = 0 then "%s" else ";%s")
(layer_to_string layer)
) layers;
Buffer.contents buff
let of_string (s: string): t =
let layer_strings = BatString.nsplit s ~by:";" in
L.map layer_of_string layer_strings
let fn =
let , index_lines = Utls.maybe_extract_comment_header fn in
match header with
| None -> (-1, [])
| Some ->
let radius = Scanf.sscanf comment "#radius=%d" (fun r -> r) in
(radius, index_lines)
let fn =
let , mol_lines = Utls.maybe_extract_comment_header fn in
match header with
| None -> (-1, "/dev/null", mol_lines)
| Some ->
let radius, index_fn =
Scanf.sscanf comment "#radius=%d;index=%s"
(fun r fn -> (r, fn)) in
(radius, index_fn, mol_lines)
let input =
try
Scanf.sscanf (input_line input)
"#radius=%d;index=%s" (fun r fn -> (r, fn))
with
Scanf.Scan_failure _ -> (-1, "/dev/null")
let restore_mop2d_index fn =
let radius, index_lines = parse_index_comment fn in
let mop2d_envs = L.map of_string index_lines in
let res = Hashtbl.create 11 in
L.iteri (fun i env ->
assert(not (Hashtbl.mem res env));
Hashtbl.add res env i
) mop2d_envs;
Log.info "index size: %d" (Hashtbl.length res);
(radius, res)