Same/ChunkedSource.pm

filter: Statementstmt 97.0% 97 / 100 Branchbran 90.0% 18 / 20 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 92.3% 144 / 156 CC 13 SCAR 9.5
CC
13
Cov
94%
_get_map_key_from_options
6.3
_make_chunk_maps
3.0
new
2.0
CRAP 2.6
« Same/Cache.pm ↑ Summary Same/FileChunkedSource.pm »
1 =head1 NAME
2  
3 Text::Same::ChunkedSource
4  
5 =head1 DESCRIPTION
6  
7 Objects of this class represent a source of chunks (generally lines)
8 in a source (generally a file). The "chunks" could potentially be
9 paragraphs or sentences.
10  
11 =head1 SYNOPSIS
12  
13 my $source = new Text::Same::ChunkedSource(chunks->\@chunks)
14  
15 =head1 METHODS
16  
17 See below. Methods private to this module are prefixed by an
18 underscore.
19  
20 =cut
21  
22 package Text::Same::ChunkedSource;
23  
24 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 $VERSION = '0.07';
30  
31 5 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 ▶use Text::Same::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head2 new
36  
37 Title : new
38 Usage : $source = new Text::Same::ChunkedSource(chunks->\@chunks)
39 Function: Creates a new ChunkedSource object from an array
40 Returns : A Text::Same::ChunkedSource object
41 Args : chunks - an array of strings
42  
43 =cut
44  
45 sub new
46 3 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
47 3 my $arg = shift;
48 3 ▶ my $class = ref($arg) || $arg;
Condition33%1/3
ref $arg || $arg
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $arg || $arg
ref $arg $arg
Truth table33%1/3
A || B
A= ref $argB= $arg
ABresult
1X1
011
000
49  
50 3 my $self = {};
51  
52 3 my %params = @_;
53  
54 3 $self->{name} = $params{name};
55 3 $self->{all_chunks} = $params{chunks};
56 3 $self->{all_chunks_count} = scalar(@{$params{chunks}});
57  
58 3 return bless $self, $class;
59 }
60  
61 =head2 name
62  
63 Title : name
64 Usage : my $name = $source->name();
65 Function: return the name of this source - generally the filename
66  
67 =cut
68  
69 sub name
70 0 ▶{
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
71 0 my $self = shift;
72 0 return $self->{name};
73 }
74  
75 =head2 get_all_chunks
76  
77 Title : get_all_chunks
78 Usage : $all_chunks = $source->get_all_chunks;
79 Function: return (in order) the chunks from this source
80  
81 =cut
82  
83 sub get_all_chunks
84 148 ▶{
Subroutine get_all_chunks: called CC=1 SCAR=0.0
Pod documented
85 148 my $self = shift;
86 148 return @{$self->{all_chunks}};
87 }
88  
89 =head2 get_chunk_by_indx
90  
91 Title : get_chunk_by_indx
92 Usage : $chunk = $source->get_chunk_by_indx($indx);
93 Function: return the chunk/line at the given index in this source
94  
95 =cut
96  
97 sub get_chunk_by_indx
98 6 ▶{
Subroutine get_chunk_by_indx: called CC=1 SCAR=0.0
Pod documented
99 6 my $self = shift;
100 6 my $chunk_indx = shift;
101  
102 6 return $self->{all_chunks}[$chunk_indx];
103 }
104  
105 =head2 get_all_chunks_count
106  
107 Title : get_all_chunks_count
108 Usage : $count = $source->get_all_chunks_count;
109 Function: return the number of chunks in this source
110  
111 =cut
112  
113 sub get_all_chunks_count
114 7 ▶{
Subroutine get_all_chunks_count: called CC=1 SCAR=0.0
Pod documented
115 7 my $self = shift;
116 7 return $self->{all_chunks_count};
117 }
118  
119 sub _make_chunk_maps
120 6 ▶{
Subroutine _make_chunk_maps: called CC=3 SCAR=11.0
121 6 my $self = shift;
122 6 my $options = shift;
123  
124 6 my @filtered_chunk_indexes = ();
125 6 my %filtered_hash = ();
126 6 my %real_index_to_filtered_index = ();
127 6 my %filtered_index_to_real_index = ();
128  
129 6 my $filtered_chunk_count = 0;
130  
131 6 ▶ for (my $i = 0; $i < scalar(@{$self->{all_chunks}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"all_chunks"};}) 90 6 96
132 90 my $chunk = $self->{all_chunks}[$i];
133 90 ▶ if (!is_ignorable($options, $chunk)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_ignorable($options, $chunk)) 80 10 90
134 80 push @filtered_chunk_indexes, $i;
135 80 $real_index_to_filtered_index{$i} = $filtered_chunk_count;
136 80 $filtered_index_to_real_index{$filtered_chunk_count} = $i;
137  
138 80 push @{$filtered_hash{Text::Same::Util::hash($options, $chunk)}}, $i;
139 80 $filtered_chunk_count++;
140 }
141 }
142  
143 6 return \@filtered_chunk_indexes, \%filtered_hash,
144 \%real_index_to_filtered_index, \%filtered_index_to_real_index;
145 }
146  
147  
148 sub _get_map_key_from_options
149 68 ▶{
Subroutine _get_map_key_from_options: called CC=6 SCAR=18.4
150 68 my $self = shift;
151 68 my $options = shift;
152  
153 68 ▶ if (!defined $options) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options) 0 68 68
154 0 carp("\$options is undefined");
155 }
156  
157 return
158 ("key_" .
159 ($options->{ignore_case} ? "w" : "W") . "_" .
160 ($options->{ignore_blanks} ? "b" : "B") . "_" .
161 ($options->{ignore_case} ? "i" : "I") . "_" .
162 68 ▶ ($options->{ignore_simple} ? $options->{ignore_simple} : "0"));
Branch: 4 branches
BranchTrueFalseTotal
$options->{'ignore_case'} ? : 8 60 68
$options->{'ignore_blanks'} ? : 8 60 68
$options->{'ignore_case'} ? : 8 60 68
$options->{'ignore_simple'} ? : 0 68 68
163 }
164  
165 sub _maybe_make_filtered_maps
166 34 ▶{
Subroutine _maybe_make_filtered_maps: called CC=2 SCAR=6.9
167 34 my $self = shift;
168 34 my $options = shift;
169  
170 34 my $key = $self->_get_map_key_from_options($options);
171  
172 34 ▶ if (!exists $self->{filtered_chunk_indexes}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"filtered_chunk_indexes"}{$key}) 6 28 34
173 ($self->{filtered_chunk_indexes}{$key},
174 $self->{filtered_hash}{$key},
175 $self->{real_to_filtered}{$key},
176 6 $self->{filtered_to_real}{$key}) = $self->_make_chunk_maps($options);
177  
178 }
179 }
180  
181 =head2 get_filtered_chunk_indexes
182  
183 Title : get_filtered_chunk_indexes
184 Usage : $filtered_chunk_indexes = $source->get_filtered_chunk_indexes($options);
185 Function: return (in order) the chunks from this source that match the given
186 options:
187 ignore_case=> (0 or 1) -- ignore case when comparing
188 ignore_blanks=> (0 or 1) -- ignore blank lines when comparing
189 ignore_space=> (0 or 1) -- ignore whitespace in chunks
190  
191 =cut
192  
193 sub get_filtered_chunk_indexes
194 16 ▶{
Subroutine get_filtered_chunk_indexes: called CC=1 SCAR=0.0
Pod documented
195 16 my $self = shift;
196 16 my $options = shift;
197  
198 16 my $key = $self->_get_map_key_from_options($options);
199  
200 16 $self->_maybe_make_filtered_maps($options);
201  
202 16 return $self->{filtered_chunk_indexes}{$key};
203 }
204  
205 =head2 get_matching_chunk_indexes
206  
207 Title : get_matching_chunk_indexes
208 Usage : $matches = $source->get_matching_chunk_indexes($options, $text);
209 Function: return (in order) the chunks from this source that match the given
210 text.
211 options:
212 ignore_case=> (0 or 1) -- ignore case when comparing
213 ignore_blanks=> (0 or 1) -- ignore blank lines when comparing
214 ignore_space=> (0 or 1) -- ignore whitespace in chunks
215  
216 =cut
217  
218 sub get_matching_chunk_indexes
219 2 ▶{
Subroutine get_matching_chunk_indexes: called CC=2 SCAR=7.0
Pod documented
220 2 my $self = shift;
221 2 my $options = shift;
222 2 my $text = shift;
223  
224 2 my $key = $self->_get_map_key_from_options($options);
225  
226 2 $self->_maybe_make_filtered_maps($options);
227  
228 2 my $chunk_hash = $self->{filtered_hash}{$key};
229  
230 2 ▶ return @{$chunk_hash->{hash($options, $text)} || []};
Condition50%1/2
$$chunk_hash{hash($options, $text)} || []
l!l
20
MC/DC0%0/1
$$chunk_hash{hash($options, $text)} || []
$$chunk_hash{hash($options, $text)}
Truth table50%1/2
A
A= $$chunk_hash{hash($options, $text)}
Aresult
11
00
231 }
232  
233 =head2 _get_filtered_indx_from_real
234  
235 Title : _get_filtered_indx_from_real
236 Usage : $indx = $source->_get_filtered_indx_from_real($options, $real_indx);
237 Function: for the given index (eg. line number) in this source, return the
238 corresponding index in the list of chunks generated by applying the
239 $options. For example if $options->{ignore_blanks} is true the
240 filtered chunks will contain no blank lines.
241  
242 eg. input lines:
243  
244 some text on line 0
245 <blank line>
246 <blank line>
247 some text on line 3
248  
249 the real index of "some text on line 3" is 3, but the filtered index is 1 if
250 ignore_blanks is set because the filtered lines are:
251 some text on line 0
252 some text on line 3
253  
254 =cut
255  
256 sub _get_filtered_indx_from_real
257 16 ▶{
Subroutine _get_filtered_indx_from_real: called CC=1 SCAR=0.0
258 16 my $self = shift;
259 16 my $options = shift;
260 16 my $real_indx = shift;
261  
262 16 my $key = $self->_get_map_key_from_options($options);
263  
264 16 $self->_maybe_make_filtered_maps($options);
265  
266 16 return $self->{real_to_filtered}{$key}{$real_indx};
267 }
268  
269 =head2 get_previous_chunk_indx
270  
271 Title : get_previous_chunk_indx
272 Usage : $prev_chunk_indx =
273 $source->get_previous_chunk_indx($options, $chunk_indx);
274 Function: return the previous chunk index from the list of filtered chunk
275 indexes (for the given $options). See discussion above.
276  
277 =cut
278  
279 sub get_previous_chunk_indx
280 12 ▶{
Subroutine get_previous_chunk_indx: called CC=2 SCAR=6.9
Pod documented
281 12 my $self = shift;
282 12 my $options = shift;
283 12 my $chunk_indx = shift;
284  
285 12 my $prev_filtered_indx =
286 $self->_get_filtered_indx_from_real($options, $chunk_indx) - 1;
287  
288 12 ▶ if ($prev_filtered_indx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_filtered_indx < 0) 6 6 12
289 6 return undef;
290 }
291  
292 6 return ($self->get_filtered_chunk_indexes($options))->[$prev_filtered_indx];
293 }
294  
295 =head2 get_next_chunk_indx
296  
297 Title : get_next_chunk_indx
298 Usage : $next_chunk_indx =
299 $source->get_next_chunk_indx($options, $chunk_indx);
300 Function: return the next chunk index from the list of filtered chunk
301 indexes (for the given $options). See discussion above.
302  
303 =cut
304  
305 sub get_next_chunk_indx
306 4 ▶{
Subroutine get_next_chunk_indx: called CC=2 SCAR=6.9
Pod documented
307 4 my $self = shift;
308 4 my $options = shift;
309 4 my $chunk_indx = shift;
310  
311 4 my $next_filtered_indx =
312 $self->_get_filtered_indx_from_real($options, $chunk_indx) + 1;
313  
314 4 ▶ if ($next_filtered_indx >= $self->get_all_chunks_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_filtered_indx >= $self->get_all_chunks_count) 3 1 4
315 3 return undef;
316 }
317  
318 1 return ($self->get_filtered_chunk_indexes($options))->[$next_filtered_indx];
319 }
320  
321 =head1 AUTHOR
322  
323 Kim Rutherford <kmr+same@xenu.org.uk>
324  
325 =head1 COPYRIGHT & LICENSE
326  
327 Copyright 2005,2006 Kim Rutherford. All rights reserved.
328  
329 This program is free software; you can redistribute it and/or modify it
330 under the same terms as Perl itself.
331  
332 =head1 DISCLAIMER
333  
334 This module is provided "as is" without warranty of any kind. It
335 may redistributed under the same conditions as Perl itself.
336  
337 =cut
338  
339 5 1;
« Same/Cache.pm ↑ Summary Same/FileChunkedSource.pm »