SegmentTree/Builder.pm

filter: Statementstmt 94.4% 118 / 125 Branchbran 65.3% 17 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 33.3% 4 / 12 totaltotal 87.0% 161 / 185 SCAR 27.3
CC
15
Cov
89%
build_tree
5.9
place_intervals
4.0
new
2.1
CRAP 15.3
« SegmentTree/ValueLookup.pm ↑ Summary SegmentTree.pm »
1 package Set::SegmentTree::Builder;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.05';
7  
8 1 ▶use Carp qw/croak confess carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes qw/gettimeofday/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Map qw/map_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util qw/reduce/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Set::SegmentTree::ValueLookup;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 Readonly our $INTERVAL_ID => 0;
17 1 Readonly our $INTERVAL_MIN => 1;
18 1 Readonly our $INTERVAL_MAX => 2;
19 1 Readonly our $ELEMENTARY_MIN => 0;
20 1 Readonly our $ELEMENTARY_MAX => 1;
21 1 Readonly our $TRUE => 1;
22 1 Readonly our $MS_IN_NS => 1000;
23 1 Readonly our $INTERVALS_PER_NODE => 2;
24  
25 #########################
26 1 my $cc = 0;
27 1 my $icc = 0;
28 #########################
29  
30 sub new_instance {
31 1 ▶ my ( $class, $options ) = @_;
Subroutine new_instance: called CC=2 SCAR=7.1
Pod undocumented
32 1 ▶ return bless { locked => 0, segment_list => [], %{ $options || {} } },
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 0 1 1
33 $class;
34 }
35  
36 sub build {
37 1 ▶ my ($self) = @_;
Subroutine build: called CC=1 SCAR=0.0
Pod documented
38 1 $self->build_tree( @{ $self->{segment_list} } );
39 1 return Set::SegmentTree->deserialize( $self->serialize );
40 }
41  
42 sub new {
43 1 ▶ my ( $class, @list ) = @_;
Subroutine new: called CC=2 SCAR=7.4
Pod documented
44 1 my $options = {};
45 1 ▶ if ( 'HASH' eq ref @list ) { $options = pop @list; }
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref @list) 0 1 1
46 1 return $class->new_instance($options)->insert(@list);
47 }
48  
49 sub insert {
50 1 ▶ my ( $self, @list ) = @_;
Subroutine insert: called CC=2 SCAR=7.0
Pod documented
51 1 ▶ confess 'This tree already built. Make a new one' if $self->{locked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"locked"} 0 1 1
52 1 push @{ $self->{segment_list} }, @list;
53 1 return $self;
54 }
55  
56 sub serialize {
57 2 ▶ my ($self) = @_;
Subroutine serialize: called CC=2 SCAR=7.0
Pod undocumented
58 2 ▶ confess 'Cannot serialized unlocked tree' if !$self->{locked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"locked"} 0 2 2
59  
60 my $t = Set::SegmentTree::ValueLookup->new(
61 root => $self->{tree},
62 nodes => $self->{nodelist},
63 2 created => time
64 );
65 2 return $t->serialize;
66 }
67  
68 sub to_file {
69 1 ▶ my ( $self, $outfile ) = @_;
Subroutine to_file: called CC=2 SCAR=7.1
Pod documented
70 1 ▶ if ( !$self->{locked} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"locked"}) 0 1 1
71 0 carp 'you asked for to_file without building first. '
72 . 'Building now. This is expensive.'
73 . $self->build;
74 }
75 1 my $out = IO::File->new( $outfile, '>:raw' );
76 1 $out->print( $self->serialize );
77 1 undef $out;
78 1 return -s $outfile;
79 }
80  
81 sub endpoint {
82 58 ▶ my ( $self, $offset, $which ) = @_;
Subroutine endpoint: called CC=1 SCAR=0.0
Pod undocumented
83 58 return $self->{elist}->[$offset]->[$which];
84 }
85  
86 sub endpoints {
87 1 ▶ my ( $self, @endpoints ) = @_;
Subroutine endpoints: called CC=1 SCAR=0.0
Pod undocumented
88 14 my @list = sort { $a <=> $b }
89 1 map { ( $_->[$INTERVAL_MIN], $_->[$INTERVAL_MAX] ) } @endpoints;
90 1 return @list;
91 }
92  
93 sub place_intervals {
94 1 ▶ my ( $self, @intervals ) = @_;
Subroutine place_intervals: called CC=4 SCAR=13.9
Pod undocumented
95 1 foreach my $node ( @{ $self->{nodelist} } ) {
96 29 ▶ next if exists $node->{low};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node->{"low"} 14 15 29
97 $node->{segments} = [
98 24 map { $_->[$INTERVAL_ID] } grep {
99 15 $node->{min} >= $_->[$INTERVAL_MIN]
100 60 ▶ && $node->{max} <= $_->[$INTERVAL_MAX];
Branch: 1 branch
BranchTrueFalseTotal
$node->{"min"} >= $_->[$Set::SegmentTree::Builder::INTERVAL_MIN] and $node->{"max"} <= $_->[$Set::SegmentTree::Builder::INTERVAL_MAX] 42 18 60
101 } @intervals
102 ];
103 }
104 1 return;
105 }
106  
107 sub build_elementary_list {
108 1 ▶ my ( $self, @segment_list ) = @_;
Subroutine build_elementary_list: called CC=1 SCAR=0.0
Pod undocumented
109 my ($elementary) = reduce {
110 7 ▶ my ( $d, $c ) = ( $a, $a );
Subroutine __ANON__: called CC=2 SCAR=6.9
111 7 ▶ if ( 'ARRAY' ne ref $a ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $a) 1 6 7
112 1 $d = [ [ $c, $c ], $c ];
113 }
114 7 $c = pop @{$d};
115 7 [ @{$d}, [ $c, $b ], [ $b, $b ], $b ];
116 }
117 1 $self->endpoints(@segment_list);
118 1 pop @{$elementary}; # extra bit
119 1 $self->{elist} = $elementary;
120 1 return $elementary;
121 }
122  
123 sub build_tree {
124 1 ▶ my ( $self, @segment_list ) = @_;
Subroutine build_tree: called CC=5 SCAR=17.8
Pod undocumented
125 1 ▶ if ( $self->{locked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locked"}) 0 1 1
126 0 croak 'This tree is immutable. Build a new one.';
127 }
128 1 $self->{locked} = 1;
129 1 my $elementary = $self->build_elementary_list(@segment_list);
130  
131 1 ▶ if ( $self->{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 1 1
132 0 warn "Building binary tree\n";
133 }
134 1 my $st = gettimeofday;
135 1 $self->{tree} = $self->build_binary( 0, $#{$elementary} );
136 1 ▶ if ( $self->{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 1 1
137 0 my $et = gettimeofday;
138 0 warn "took $cc calls "
139 . sprintf( '%0.3f', ( ( $et - $st ) * $MS_IN_NS ) / $cc )
140 . ' ms per ('
141 . ( $et - $st )
142 . " elap)\n";
143 0 warn "placing intervals...\n";
144 }
145 1 my $ist = gettimeofday;
146 1 $self->place_intervals(@segment_list);
147 1 my $iet = gettimeofday;
148 warn "took $icc segment placements "
149 . sprintf( '%0.3f', ( ( $iet - $ist ) * $MS_IN_NS ) / $icc )
150 . ' ms per ('
151 . ( $iet - $ist )
152 . " elapsed)\n"
153 1 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
154 1 return $self;
155 }
156  
157 # from being offset into elementary list
158 # to being offset into elementary list
159 sub build_binary {
160 29 ▶ my ( $self, $from, $to ) = @_;
Subroutine build_binary: called CC=2 SCAR=6.9
Pod undocumented
161 29 $cc++;
162 29 my $mid = int( ( $to - $from ) / $INTERVALS_PER_NODE ) + $from;
163 29 my $node = {
164 min => $self->endpoint( $from, $ELEMENTARY_MIN ),
165 max => $self->endpoint( $to, $ELEMENTARY_MAX ),
166 };
167  
168 29 ▶ if ( $from != $to ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from != $to) 14 15 29
169 14 $node->{low} = $self->build_binary( $from, $mid );
170 14 $node->{high} = $self->build_binary( $mid + 1, $to );
171 }
172 29 push @{ $self->{nodelist} }, $node;
173 29 return $#{ $self->{nodelist} };
174 }
175  
176 1 1;
177 __END__
« SegmentTree/ValueLookup.pm ↑ Summary SegmentTree.pm »