line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::NestedSet;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
81781
|
use 5.008008;
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
11
|
use strict;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
5
|
2
|
|
|
2
|
|
11
|
use warnings;
|
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
72
|
|
6
|
2
|
|
|
2
|
|
11
|
use Carp;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1502
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION=1.03;
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new {
|
12
|
1
|
|
|
1
|
1
|
471
|
my ($class,$data,$depth_position)= @_;
|
13
|
|
|
|
|
|
|
|
14
|
1
|
50
|
|
|
|
9
|
croak 'An array ref must be supplied as the first argument. Seen '. ref($data) if(ref($data) ne 'ARRAY');
|
15
|
1
|
|
|
|
|
3
|
my $nb = scalar @{$data};
|
|
1
|
|
|
|
|
4
|
|
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
6
|
croak 'The number of items within the array ref must be >=1 . Seen '.$nb if($nb==0);
|
18
|
1
|
50
|
33
|
|
|
12
|
croak 'An integer must be supplied as the second argument. Seen '. $depth_position if(not defined $depth_position
|
19
|
|
|
|
|
|
|
|| $depth_position!~m/^[0-9]+/mx);
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
|
|
12
|
my $this = {
|
23
|
|
|
|
|
|
|
left_node => 1,
|
24
|
|
|
|
|
|
|
previous_right_node => [],
|
25
|
|
|
|
|
|
|
last_depth => 0,
|
26
|
|
|
|
|
|
|
Data_length => $nb,
|
27
|
|
|
|
|
|
|
Max_right_node => $nb*2,
|
28
|
|
|
|
|
|
|
depth_position => $depth_position,
|
29
|
|
|
|
|
|
|
data => $data,
|
30
|
|
|
|
|
|
|
};
|
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
4
|
$this->{Left} = scalar @{$data->[0]};
|
|
1
|
|
|
|
|
6
|
|
33
|
1
|
|
|
|
|
5
|
$this->{Right} = $this->{Left} + 1;
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#the first row is the root, we assign 1 to left and the maximum value possible to right
|
36
|
1
|
|
|
|
|
8
|
$this->{data}->[0]->[$this->{Left}] = $this->{left_node};
|
37
|
1
|
|
|
|
|
5
|
$this->{data}->[0]->[$this->{Right}] = $this->{Max_right_node};
|
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
5
|
bless $this, $class;
|
40
|
1
|
|
|
|
|
7
|
return $this;
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub create_nodes {
|
45
|
1
|
|
|
1
|
1
|
4
|
my $this = shift;
|
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
16
|
for(my $i=1; $i<$this->{Data_length}; $i++) {
|
48
|
|
|
|
|
|
|
|
49
|
6
|
|
|
|
|
24
|
my $orientation = $this->{data}->[$i][$this->{depth_position}] - $this->{last_depth};
|
50
|
|
|
|
|
|
|
|
51
|
6
|
100
|
|
|
|
24
|
if($orientation==1) { #go down in depth
|
|
|
50
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
17
|
$this->set_left_node($i);
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
}
|
56
|
|
|
|
|
|
|
elsif($orientation==0) { # same depth level
|
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
$this->set_previous_right_node();
|
59
|
0
|
|
|
|
|
0
|
$this->set_left_node($i);
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
}
|
62
|
|
|
|
|
|
|
else { # go back up in depth
|
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
9
|
$this->set_previous_right_node();
|
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
5
|
my $depth = abs $orientation;
|
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
9
|
for(0..$depth-1) {
|
69
|
3
|
|
|
|
|
12
|
$this->set_previous_right_node();
|
70
|
|
|
|
|
|
|
}
|
71
|
2
|
|
|
|
|
9
|
$this->set_left_node($i);
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
|
74
|
6
|
|
|
|
|
14
|
push @{$this->{previous_right_node}},$i;
|
|
6
|
|
|
|
|
17
|
|
75
|
6
|
|
|
|
|
42
|
$this->{last_depth}=$this->{data}->[$i]->[$this->{depth_position}];
|
76
|
|
|
|
|
|
|
}
|
77
|
1
|
|
|
|
|
5
|
$this->set_right_nodes();
|
78
|
1
|
|
|
|
|
5
|
return $this->{data};
|
79
|
|
|
|
|
|
|
}
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set_previous_right_node {
|
82
|
5
|
|
|
5
|
0
|
11
|
my $this=shift;
|
83
|
5
|
|
|
|
|
10
|
$this->{left_node}++;
|
84
|
5
|
|
|
|
|
24
|
$this->{data}->[pop @{$this->{previous_right_node}}]->[$this->{Right}]=$this->{left_node};
|
|
5
|
|
|
|
|
25
|
|
85
|
|
|
|
|
|
|
}
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub set_left_node {
|
88
|
6
|
|
|
6
|
0
|
15
|
my $this=shift;
|
89
|
6
|
|
|
|
|
14
|
$this->{left_node}++;
|
90
|
6
|
|
|
|
|
30
|
$this->{data}->[shift]->[$this->{Left}]=$this->{left_node};
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub set_right_nodes {
|
94
|
1
|
|
|
1
|
0
|
3
|
my $this=shift;
|
95
|
1
|
|
|
|
|
4
|
my $last_node = $this->{data}->[$this->{Data_length}-1][$this->{Left}];
|
96
|
1
|
|
|
|
|
3
|
for(my $i=scalar(@{$this->{previous_right_node}}) -1; $i >= 0; $i--) {
|
|
1
|
|
|
|
|
9
|
|
97
|
1
|
|
|
|
|
3
|
$last_node++;
|
98
|
1
|
|
|
|
|
9
|
$this->{data}->[$this->{previous_right_node}->[$i]]->[$this->{Right}]=$last_node;
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1;
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__
|