-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload2graph
75 lines (63 loc) · 1.67 KB
/
load2graph
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
use strict;
use Data::Dumper;
use YAML;
my @loads = (
[ A => "B" ],
[ A => "C" ],
[ C => "D" ],
[ D => "E" ],
[ C => "F" ],
);
my @load_graph;
my @stack = ( \@load_graph );
my @callers_stack;
my $prev_caller = "";
my $prev_callee = "";
for my $load (@loads) {
my $caller = $load->[0];
my $callee = $load->[1];
print STDERR "* $caller -> $callee -- (@callers_stack)\n";
if ($caller eq $prev_caller) {
push @{ $stack[-1] }, [ $callee ];
}
elsif ($caller eq $prev_callee) {
push @{ $stack[-1][-1] }, [ [ $callee ] ];
push @stack, $stack[-1][-1][-1];
push @callers_stack, $caller;
}
else {
while (@callers_stack and $callers_stack[-1] ne $caller) {
print STDERR "--- (@callers_stack)\n";
pop @callers_stack;
pop @stack;
}
if ($callers_stack[-1] eq $caller) {
push @{ $stack[-1] }, [ $callee ];
}
else {
push @callers_stack, $caller;
push @stack, [ [ $callee ] ];
push @load_graph, [ $caller => $stack[-1] ];
}
}
print Dumper(\@load_graph), $/;
$prev_caller = $caller;
$prev_callee = $callee;
}
my @expected = (
[ "A" => [
[ "B" ],
[ "C" => [
[ "D" => [
[ "E" ]
]
],
[ "F" ],
]
],
],
]
);
use Test::More;
plan tests => 1;
is_deeply(\@load_graph, \@expected);