Same/FileChunkedSource.pm

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