-
Notifications
You must be signed in to change notification settings - Fork 0
/
invert.ml
49 lines (46 loc) · 1.4 KB
/
invert.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
open Batteries_uni
open Graph
module H=Hashtbl
let invert1 : graph -> graph = fun g ->
let res = H.create (H.length g) in
H.iter
(fun from days ->
H.iter
(fun day reps ->
H.iter
(fun to' num ->
let to_days = match H.find_option res to' with
| Some x -> x
| _ -> let x: adjlist = H.create 10 in H.add res to' x; x in
let to_reps = match H.find_option to_days day with
| Some x -> x
| _ -> let x: reps = H.create 10 in H.add to_days day x; x in
H.add to_reps from num) reps) days) g;
res
(* @kaustuv: find_option is implemented with the exception anyways
and makes one more extra allocation *)
let invert2 g =
let res = H.create (H.length g) in
H.iter begin fun f days ->
H.iter begin fun day reps ->
H.iter begin fun t num ->
let t_days = begin
try H.find res t
with Not_found -> let x = H.create 10 in H.add res t x ; x
end in
let t_reps = begin
try H.find t_days day
with Not_found -> let x = H.create 10 in H.add t_days day x ; x
end in
H.add t_reps f num
end reps
end days
end g ;
res
(*
let invert2 : graph -> graph = fun g ->
let res = H.enum g in
Enum.map (fun (from, days) -> let days' = H.enum days in
Enum.map (fun (day,reps) -> let reps' = H.enum reps in
Enum.map (fun (to',num) -> (to',(day,from,num))
...*)