File Coverage

blib/lib/Algorithm/Annotate.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 0 4 0.0
total 36 41 87.8


line stmt bran cond sub pod time code
1             package Algorithm::Annotate;
2             $VERSION = '0.10';
3 1     1   12 use strict;
  1         9  
  1         13  
4 1     1   90 use Algorithm::Diff qw(traverse_balanced);
  1         11  
  1         17  
5              
6             =head1 NAME
7            
8             Algorithm::Annotate - represent a series of changes in annotate form
9            
10             =head1 SYNOPSIS
11            
12             use Algorithm::Annotate;
13            
14             my $ann = Algorithm::Annotate->new ();
15            
16             $ann->add ($info1, \@seq1);
17            
18             $ann->add ($info2, \@seq2);
19             $ann->add ($info3, \@seq3);
20            
21             $result = $ann->result;
22            
23             =head1 DESCRIPTION
24            
25             Algorithm::Annotate generates a list that is useful for generating
26             output simliar to C<cvs annotate>.
27            
28             =head1 TODO
29            
30             Might parse diff output and accumulate them for generating the annotate list.
31            
32             =cut
33              
34             sub new {
35 1     1 0 11     my $class = shift;
36 1         12     my $self = bless {}, $class;
37 1         12     return $self;
38             }
39              
40             sub init {
41 1     1 0 11     my ($self, $info, $seq) = @_;
42 1         11     $self->{lastseq} = $seq;
43 1         9     $self->{annotate} = [map {$info} @$seq];
  11         102  
44             }
45              
46             sub add {
47 2     2 0 22     my ($self, $info, $seq) = @_;
48              
49 2 50       24     return $self->init ($info, $seq) unless $self->{lastseq};
50              
51                 traverse_balanced( $self->{lastseq}, $seq,
52 20     20   1650 { MATCH => sub {},
53             DISCARD_A =>
54             sub {
55 2     2   78 splice (@{$self->{annotate}}, $_[1], 1);
  2         37  
56             },
57             DISCARD_B =>
58             sub {
59 6     6   136 splice(@{$self->{annotate}}, $_[1], 0, $info);
  6         170  
60             },
61             CHANGE =>
62             sub {
63 2     2   1187 $self->{annotate}[$_[1]] = $info;
64             },
65 2         68 } );
66              
67 2         63     $self->{lastseq} = $seq;
68             }
69              
70             sub result {
71 2     2 0 20     my $self = shift;
72 2         41     return $self->{annotate};
73             }
74              
75             1;
76              
77             =head1 AUTHORS
78            
79             Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
80            
81             =head1 COPYRIGHT
82            
83             Copyright 2003 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.
84            
85             This program is free software; you can redistribute it and/or modify it
86             under the same terms as Perl itself.
87            
88             See L<http://www.perl.com/perl/misc/Artistic.html>
89            
90             =cut
91