blib/lib/Set/Integer/Gapfillers.pm

filter: Statementstmt 100% 150 / 150 Branchbran 96.1% 75 / 78 Conditioncond 80.0% 12 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 96.6% 258 / 267 CC 50 SCAR 27.2
CC
50
Cov
98%
_calculate
24.0
_args_check
16.0
segments_needed
5.0
CRAP 15.2
↑ Summary
1 package Set::Integer::Gapfillers;
2 8 $VERSION = '0.08';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 45 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
7 45 my $class = shift;
8 45 my $initref = _args_check(@_);
9 33 return bless $initref, $class;
10 }
11  
12 35 ▶sub all_segments {
Subroutine all_segments: called CC=1 SCAR=0.0
Pod documented
13 35 my $self = shift;
14 35 my %params = _check_extra_args(@_);
15 34 _expand_upon_request($self->{segments}, \%params);
16 }
17  
18 29 ▶sub gapfillers {
Subroutine gapfillers: called CC=3 SCAR=11.0
Pod documented
19 29 my $self = shift;
20 29 my %params = _check_extra_args(@_);
21 28 my @segments = @{$self->{segments}};
22 28 my @gaps;
23 28 ▶ for (my $n = 0; $n <= $#segments; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $#segments) 133 28 161
24 133 ▶ push @gaps, $segments[$n] unless $self->{statuses}->[$n];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"statuses"}[$n] 65 68 133
25 }
26 28 _expand_upon_request( [ @gaps ], \%params);
27 }
28  
29 7 ▶sub segments_needed {
Subroutine segments_needed: called CC=5 SCAR=16.2
Pod documented
30 7 my $self = shift;
31 7 my %params = _check_extra_args(@_);
32 6 my @segments_needed = @{ $self->{segments} };
33 # If the lower bound to the requested range fell in a provided segment,
34 # then the first segment returned may have "unneeded" elements on its
35 # lower side.
36 # Analogously, if the upper bound fell in a provided segment,
37 # then the last segment returned may have "unneeded" elements on its
38 # upper side.
39 # We need to snip these unneeded elements off.
40 6 ▶ if ($self->{statuses}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"statuses"}[0]) 4 2 6
41 4 ▶ if ($segments_needed[0]->[0] < $self->{lower}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments_needed[0][0] < $self->{"lower"}) 4 0 4
42 4 $segments_needed[0]->[0] = $self->{lower};
43 }
44 }
45 6 ▶ if ($self->{statuses}->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"statuses"}[-1]) 4 2 6
46 4 ▶ if ($segments_needed[-1]->[1] > $self->{upper}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments_needed[-1][1] > $self->{"upper"}) 4 0 4
47 4 $segments_needed[-1]->[1] = $self->{upper};
48 }
49 }
50 6 _expand_upon_request([ @segments_needed ], \%params);
51 }
52  
53 45 ▶sub _args_check {
Subroutine _args_check: called CC=16 SCAR=27.7
54 45 my %args = @_;
55 45 ▶ croak "Need lower bound: $!" unless defined $args{lower};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"lower"} 1 44 45
56 44 ▶ croak "Need upper bound: $!" unless defined $args{upper};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"upper"} 1 43 44
57 43 ▶ croak "Lower bound must be numeric: $!" unless $args{lower} =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"lower"} =~ /^-?\d+$/ 1 42 43
58 42 ▶ croak "Upper bound must be numeric: $!" unless $args{upper} =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"upper"} =~ /^-?\d+$/ 1 41 42
59 croak "Upper bound must be >= lower bound: $!"
60 41 ▶ unless $args{upper} >= $args{lower};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"upper"} >= $args{"lower"} 1 40 41
61 40 ▶ croak "Need 'sets' argument: $!" unless defined $args{sets};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"sets"} 1 39 40
62 croak "'sets' must be array reference: $!"
63 39 ▶ unless ref($args{sets}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"sets"} eq "ARRAY" 1 38 39
64 38 foreach my $pairref (@{$args{sets}}) {
65 croak "Elements of 'sets' must be 2-element array references: $!"
66 124 ▶ unless scalar(@{$pairref}) == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$pairref;} == 2 1 123 124
67 123 foreach my $n (@{$pairref}) {
68 245 ▶ croak "Elements of sets must be numeric: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /^-?\d+$/ 1 244 245
69 unless $n =~ /^-?\d+$/;
70 }
71 122 ▶ croak "First element of each array must be <= second element\n$pairref->[0] has problem: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $pairref->[0] <= $pairref->[1] 2 120 122
72 unless $pairref->[0] <= $pairref->[1];
73 }
74 34 my @sets = sort { $a->[0] <=> $b->[0] } @{$args{sets}};
75 34 my (@firsts, @seconds);
76 34 ▶ for (my $i=0; $i<=$#sets; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#sets) 119 33 152
77 119 ▶ if ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) { } 85 34 119
78 85 ▶ croak "First element of each array must be > second element of previous array: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $sets[$i][0] > $sets[$i - 1][1] 1 84 85
79 unless ($sets[$i]->[0] > $sets[$i-1]->[1]);
80 } else {
81 34 1;
82 }
83 118 $firsts[$i] = $sets[$i]->[0];
84 118 $seconds[$i] = $sets[$i]->[1];
85 }
86 33 my %intermediate;
87 33 $intermediate{sets} = \@sets;
88 33 $intermediate{firsts} = \@firsts;
89 33 $intermediate{seconds} = \@seconds;
90 33 $intermediate{lower} = $args{lower};
91 33 $intermediate{upper} = $args{upper};
92 33 my ($segmentsref, $statusref) = _calculate(%intermediate);
93 my %init = (
94 segments => $segmentsref,
95 statuses => $statusref,
96 lower => $intermediate{lower},
97 upper => $intermediate{upper},
98 33 );
99 33 return \%init;
100 }
101  
102 33 ▶sub _calculate {
Subroutine _calculate: called CC=24 SCAR=31.8
103 33 my %args = @_;
104 33 my @all_segments;
105 my @statuses;
106 33 my %status;
107 # Inspect for either of two oddball but easy-to-compute cases.
108 # Cases of $args{lower} in gap after last provided segment
109 # and $args{upper} in gap before first provided segment
110 33 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"firsts"}[0] > $args{"upper"} or $args{"seconds"}[-1] < $args{"lower"}) 2 31 33
Condition100%3/3
$args{"firsts"}[0] > $args{"upper"} or $args{"seconds"}[-1] < $args{"lower"}
l!l&&r!l&&!r
1131
MC/DC100%2/2
$args{"firsts"}[0] > $args{"upper"} or $args{"seconds"}[-1] < $args{"lower"}
$args{"firsts"}[0] > $args{"upper"} $args{"seconds"}[-1] < $args{"lower"}
Truth table100%3/3
A or B
A= $args{"firsts"}[0] > $args{"upper"}B= $args{"seconds"}[-1] < $args{"lower"}
ABresult
1X1
011
000
111 $args{firsts}->[0] > $args{upper}
112 or
113 $args{seconds}->[-1] < $args{lower}
114 ) {
115 2 push @all_segments, [ $args{lower}, $args{upper} ];
116 2 push @statuses, 0;
117 2 return (\@all_segments, \@statuses);
118 }
119 31 my $i = 0;
120 31 my $j = scalar(@{$args{seconds}}) - 1;
121 31 ▶ $i++ until ( $args{lower} < $args{firsts}->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
$args{"lower"} < $args{"firsts"}[$i] or do { ++$i } 31 24 55
122 31 ▶ $j-- while ( $args{upper} < $args{firsts}->[$j] );
Branch: 1 branch
BranchTrueFalseTotal
$args{"upper"} < $args{"firsts"}[$j] and do { --$j } 22 31 53
123 # $status{xxx} true: starting (ending) in provided segment
124 # $status{xxx} false: starting (ending) in subsequent gap
125 31 ▶ $status{lower} = ($args{lower} <= $args{seconds}->[$i-1]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'lower'} <= $args{'seconds'}[$i - 1] ? : 22 9 31
126 31 ▶ $status{upper} = ($args{upper} <= $args{seconds}->[$j] ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'upper'} <= $args{'seconds'}[$j] ? : 12 19 31
127  
128 # Case of $args{lower} in gap below the first provided segment.
129 # I have to handle this separately because its $i value would be -1 ...
130 # which would created problems if used as an array subscript
131 31 ▶ if ($args{lower} < $args{firsts}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"lower"} < $args{"firsts"}[0]) 8 23 31
132 8 push @all_segments, [ $args{lower}, $args{firsts}->[0] - 1 ];
133 8 push @statuses, 0;
134 8 ▶ if ($j == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) { } 2 6 8
135 2 push @all_segments, $args{sets}[0];
136 2 push @statuses, 1;
137 2 ▶ if (! $status{upper}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status{"upper"}) 1 1 2
138 1 push @all_segments, [ $args{seconds}->[0] + 1, $args{upper} ];
139 1 push @statuses, 0;
140 }
141 } else {
142 6 for my $p (0..$j-1) {
143 12 push @all_segments, $args{sets}[$p];
144 12 push @statuses, 1;
145 12 ▶ unless ($args{seconds}->[$p] + 1 == $args{firsts}->[$p+1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"seconds"}[$p] + 1 == $args{"firsts"}[$p + 1]) 10 2 12
146 push @all_segments,
147 10 [ $args{seconds}->[$p] + 1, $args{firsts}->[$p+1] - 1 ];
148 10 push @statuses, 0;
149 }
150 }
151 6 push @all_segments, $args{sets}[$j];
152 6 push @statuses, 1;
153 6 ▶ if (! $status{upper}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status{"upper"}) 2 4 6
154 push @all_segments,
155 2 [ $args{seconds}->[$j] + 1, $args{upper} ];
156 2 push @statuses, 0;
157 }
158 }
159 8 return (\@all_segments, \@statuses);
160 }
161  
162 # Cases where $args{lower} and $args{upper} occur within same interior
163 # provided segment/following gap pair
164 # 3 sub-cases:
165 # both in segment
166 # lower in segment; upper in gap
167 # both in gap
168 # I want to handle these here so that subsequently I can proceed on
169 # assumption that lower and upper are in different pairs
170 23 my $h = $i - 1;
171 23 ▶ if ($h == $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h == $j) 5 18 23
172 5 ▶ if ($status{lower} and $status{upper}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($status{'lower'} and $status{'upper'}) { } 1 4 5
elsif ($status{'lower'} and not $status{'upper'}) { } 1 3 4
elsif (not $status{'lower'} || $status{'upper'}) { } 3 0 3
Condition100%3/3
$status{"lower"} and $status{"upper"}
!ll&&!rl&&r
311
MC/DC100%2/2
$status{"lower"} and $status{"upper"}
$status{"lower"} $status{"upper"}
Truth table100%3/3
A and B
A= $status{"lower"}B= $status{"upper"}
ABresult
0X0
100
111
173 1 push @all_segments, $args{sets}[$h];
174 1 push @statuses, 1;
175 ▶ } elsif ($status{lower} and ! $status{upper}) {
Condition66%2/3
$status{"lower"} and not $status{"upper"}
!ll&&!rl&&r
301
MC/DC50%1/2
$status{"lower"} and not $status{"upper"}
$status{"lower"} not $status{"upper"}
Truth table66%2/3
A and not(B)
A= $status{"lower"}B= not $status{"upper"}
ABresult
0X0
100
111
176 1 push @all_segments, $args{sets}[$h];
177 1 push @statuses, 1;
178 push @all_segments,
179 1 [ $args{seconds}->[$h] + 1, $args{upper} ];
180 1 push @statuses, 0;
181 ▶ } elsif (! $status{lower} and ! $status{upper}) {
Condition33%1/3
$status{'lower'} || $status{'upper'}
l!l&&r!l&&!r
003
MC/DC0%0/2
$status{'lower'} || $status{'upper'}
$status{'lower'} $status{'upper'}
Truth table33%1/3
A || B
A= $status{'lower'}B= $status{'upper'}
ABresult
1X1
011
000
182 3 push @all_segments, [ $args{lower}, $args{upper} ];
183 3 push @statuses, 0;
184 }
185 5 return (\@all_segments, \@statuses);
186 }
187 # So now I'm ready to handle the remaining -- and most likely to occur --
188 # cases: Starting in one segment-gap pair (other than the first) and
189 # ending in a different segment-gap pair.
190 # First handle the location of the lower bound:
191 18 ▶ if ($status{lower}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status{'lower'}) { } 12 6 18
192 12 push @all_segments, $args{sets}[$h];
193 12 push @statuses, 1;
194 push @all_segments,
195 12 [ $args{seconds}->[$h] + 1, $args{firsts}->[$i] - 1 ];
196 12 push @statuses, 0;
197 } else {
198 push @all_segments,
199 6 [ $args{lower}, $args{firsts}->[$i] - 1 ];
200 6 push @statuses, 0;
201 }
202 # Next handle all other segment-gap pairs except the last:
203 18 for my $p ($i..$j-1) {
204 26 push @all_segments, $args{sets}[$p];
205 26 push @statuses, 1;
206 26 ▶ unless ($args{seconds}->[$p] + 1 == $args{firsts}->[$p+1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"seconds"}[$p] + 1 == $args{"firsts"}[$p + 1]) 19 7 26
207 push @all_segments,
208 19 [ $args{seconds}->[$p] + 1, $args{firsts}->[$p+1] - 1 ];
209 19 push @statuses, 0;
210 }
211 }
212 # Finally, handle the final segment and possible gap:
213 18 push @all_segments, $args{sets}[$j];
214 18 push @statuses, 1;
215 18 ▶ if (! $status{upper}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status{"upper"}) 12 6 18
216 push @all_segments,
217 12 [ $args{seconds}->[$j] + 1, $args{upper} ];
218 12 push @statuses, 0;
219 }
220 18 return (\@all_segments, \@statuses);
221 }
222  
223 68 ▶sub _expand_upon_request {
Subroutine _expand_upon_request: called CC=4 SCAR=13.9
224 68 my $compressed_ref = shift;
225 68 my $paramsref = shift;
226 68 ▶ if (defined $paramsref->{expand} and $paramsref->{expand}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paramsref->{'expand'} and $paramsref->{'expand'}) { } 9 59 68
Condition100%3/3
defined $paramsref->{"expand"} and $paramsref->{"expand"}
!ll&&!rl&&r
5819
MC/DC100%2/2
defined $paramsref->{"expand"} and $paramsref->{"expand"}
defined $paramsref->{"expand"} $paramsref->{"expand"}
Truth table100%3/3
A and B
A= defined $paramsref->{"expand"}B= $paramsref->{"expand"}
ABresult
0X0
100
111
227 9 my @expanded;
228 9 foreach my $pairref (@{$compressed_ref}) {
229 47 push @expanded, [ $pairref->[0] .. $pairref->[1] ];
230 }
231 9 return [ @expanded ];
232 } else {
233 59 return $compressed_ref;
234 }
235 }
236  
237 71 ▶sub _check_extra_args {
Subroutine _check_extra_args: called CC=3 SCAR=11.0
238 71 my @args = @_;
239 71 my %params;
240 71 ▶ if ( scalar(@args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 13 58 71
241 13 ▶ unless ( scalar(@args) % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2) { } 3 10 13
242 10 %params = @args;
243 } else {
244 3 croak "Need even number of arguments: $!";
245 }
246 }
247 68 return %params;
248 }
249  
250 8 1;
251  
252 #################### DOCUMENTATION ####################
253  
254 =head1 NAME
255  
256 Set::Integer::Gapfillers - Fill in the gaps between integer ranges
257  
258 =head1 SYNOPSIS
259  
260 use Set::Integer::Gapfillers;
261 $gf = Set::Integer::Gapfillers->new(
262 lower => -12,
263 upper => 62,
264 sets => [
265 [ 1, 17 ], # Note: Use comma, not
266 [ 25, 42 ], # range operator (..)
267 [ 44, 50 ],
268 ],
269 );
270  
271 $segments_needed_ref = $gf->segments_needed();
272  
273 $gapfillers_ref = $gf->gapfillers();
274  
275 $all_segments_ref = $gf->all_segments();
276  
277 Any of the three preceding output methods can also be called with an C<expand>
278 option:
279  
280 $segments_needed_ref = $gf->segments_needed( expand => 1 );
281  
282 =head1 DESCRIPTION
283  
284 This Perl extension provides methods which may be useful in manipulating sets
285 whose elements are consecutive integers. Suppose that you are provided with
286 the following non-intersecting, non-overlapping sets of consecutive integers:
287  
288 { 1 .. 17 }
289 { 25 .. 42 }
290 { 44 .. 50 }
291  
292 Suppose further that you are provided with the following lower and upper
293 bounds to a range of consecutive integers:
294  
295 lower: 12
296 upper: 62
297  
298 Provide a set of sets which:
299  
300 =over 4
301  
302 =item *
303  
304 when joined together, would form a set of consecutive integers from the
305 lower to the upper bound, inclusive; and
306  
307 =item *
308  
309 are derived from:
310  
311 =over 4
312  
313 =item *
314  
315 the sets provided;
316  
317 =item *
318  
319 proper subsets thereof; or
320  
321 =item *
322  
323 newly generated sets which fill in the gaps below, in between or above the
324 provided sets.
325  
326 =back
327  
328 =back
329  
330 Once a Set::Integer::Gapfillers object has been constructed, its C<segments_needed()>
331 method can be used to provide these results:
332  
333 { 12 .. 17 } # subset of 1st set provided
334 { 18 .. 24 } # gap-filler set
335 { 25 .. 42 } # 2nd set provided
336 { 43 .. 43 } # gap-filler set
337 # (which happens to consist of a single element)
338 { 44 .. 50 } # 3rd set provided
339 { 51 .. 62 } # gap-filler set for range above highest provided set
340  
341 Alternatively, you may only wish to examine the gap-filler sets. The
342 C<gapfillers()> method provides this set of sets.
343  
344 { 18 .. 24 } # gap-filler set
345 { 43 .. 43 } # gap-filler set
346 { 51 .. 62 } # gap-filler set
347  
348 And, as an additional alternative, you may wish to have your set of sets begin
349 or end with I<all> the values of a given provided set, rather than a proper
350 subset thereof containing only those values needed to populate the desired
351 range. In that case, use the C<all_segments()> method.
352  
353 { 1 .. 17 } # 1st set provided
354 { 18 .. 24 } # gap-filler set
355 { 25 .. 42 } # 2nd set provided
356 { 43 .. 43 } # gap-filler set
357 # (which happens to consist of a single element)
358 { 44 .. 50 } # 3rd set provided
359 { 51 .. 62 } # gap-filler set for range above highest provided set
360  
361 The results returned by the C<all_segments()> method differ from those
362 returned by the C<segments_needed()> method only at the lower or upper ends.
363 If, as in the above example, the lower bound of the target range of integers
364 falls inside a provided segment, the first set returned by
365 C<all_segments()> will be the I<entire> first set provided; the first set
366 returned by C<segments_needed()> will be a I<proper subset> of the first set
367 provided, starting with the requested lower bound.
368  
369 =head1 USAGE
370  
371 =head2 Publicly Callable Methods
372  
373 =head3 C<new()>
374  
375 $gf = Set::Integer::Gapfillers->new(
376 lower => -12,
377 upper => 62,
378 sets => [
379 [ 1, 17 ], # Note: Use comma, not
380 [ 25, 42 ], # range operator (..)
381 [ 44, 50 ],
382 ],
383 );
384  
385 B<Purpose:> Constructor of a Set::Integer::Gapfillers object.
386  
387 B<Arguments:> List of key-value pairs. C<lower> and C<upper> take integers
388 denoting the lower and upper bounds of the range of integers desired as the
389 result. C<sets> takes a reference to an anonymous array whose elements are,
390 in turn, references to anonymous arrays whose B<two> elements are the lowest
391 and highest numbers in a range of consecutive integers.
392  
393 I<Note:> The sets of consecutive integers supplied must be non-overlapping.
394 Set::Integer::Gapfillers will C<croak> if supplied with arguments such as these:
395  
396 $gf = Set::Integer::Gapfillers->new(
397 lower => -12, upper => 62,
398 sets => [
399 [ 1, 30 ], # no good: overlaps with next set
400 [ 25, 48 ], # no good: overlaps with previous and next sets
401 [ 44, 50 ], # no good: overlaps with previous set
402 ],
403 );
404  
405 I<Note:> Only two elements should be supplied in the anonymous arrays
406 supplied as elements to the array reference which is the value of C<sets>:
407 the lowest and highest (or, first and last) elements in each array. You
408 should B<not> use Perl's range operator (I<e.g.,> C<[ 25 .. 48 ]>) in this
409 instance.
410  
411 B<Returns:> A Set::Integer::Gapfillers object.
412  
413 =head3 C<segments_needed()>
414  
415 $segments_needed_ref = $gf->segments_needed();
416  
417 B<Purpose:> Generate a set of sets which (a) when joined together, would
418 form a set of consecutive integers from the lower to the upper bound,
419 inclusive; and (b) are derived from (i) the sets provided; (ii) proper
420 subsets thereof; or (iii) newly generated sets which fill in the gaps below,
421 in between or above the provided sets.
422  
423 B<Arguments:> None required. C<expand => 1> is optional (see FAQ).
424  
425 B<Returns:> A reference to an anonymous array whose elements are, in turn,
426 anonymous arrays of two elements: the lowest and highest integers in a
427 particular subset. But when the C<expand> option is set, the return value is
428 a reference to an anonymous array whose elements are, in turn, references to
429 arrays each of which holds I<the entire range> of each set needed -- not just
430 the beginning and end points.
431  
432  
433 =head3 C<gapfillers()>
434  
435 $gapfillers_ref = $gf->gapfillers();
436  
437 B<Purpose:> Generate a set of the newly generated sets needed to fill in the
438 gaps below, in between or above the sets provided to the constructor. The
439 sets, like those returned by C<segments_needed()>, are denoted by their lower
440 and upper bounds rather than by their entire contents.
441  
442 B<Arguments:> None required. C<expand => 1> is optional (see FAQ).
443  
444 B<Returns:> A reference to an anonymous array whose elements are, in turn,
445 anonymous arrays holding two elements: the lower and upper bounds of the
446 integer ranges needed to provide gap-filling as described in 'Purpose'. When
447 the C<expand> option is set, the contents of those inner sets are expanded to
448 include the full range of integers needed, not just the beginning and end
449 points.
450  
451 =head3 C<all_segments()>
452  
453 $all_segments_ref = $gf->all_segments();
454  
455 B<Purpose:> Generate a set of all sets needed in order to populate a set of
456 consecutive integers from the lower to the upper bound, inclusive. The sets
457 generated are derived from (a) the sets provided or (b) newly generated sets
458 which fill in the gaps below, in between or above the provided sets.
459  
460 B<Arguments:> None required. C<expand => 1> is optional (see FAQ).
461  
462 B<Returns:> A reference to an anonymous array whose elements are, in turn,
463 anonymous arrays holding the sets described in 'Purpose'. When
464 the C<expand> option is set, the contents of those inner sets are expanded to
465 include the full range of integers needed, not just the beginning and end
466 points.
467  
468 =head1 FAQ
469  
470 =over 4
471  
472 =item 1. How do the sets returned by the three non-constructor methods differ from one another?
473  
474 With C<segments_needed()>, the objective is: I<Show me whether the integers
475 I need to fill the desired range will come from sets already provided or from
476 newly created gap-filling sets.>
477  
478 With C<gapfillers()>, the objective is: I<Show me the sets of integers I
479 will need to create to fill the gaps between the sets already provided.>
480  
481 With C<all_segments()>, the objective is: I<Show me all the sets of integers
482 -- those already provided and those I will have to create -- from which I will
483 pull integers to populate the desired range.>
484  
485 Here are two examples:
486  
487 =over 4
488  
489 =item *
490  
491 $gf = Set::Integer::Gapfillers->new(
492 lower => 10,
493 upper => 22,
494 sets => [
495 [ 9, 11 ],
496 [ 15, 18 ],
497 [ 20, 24 ],
498 ],
499 );
500  
501 The three non-constructor methods return sets as follows:
502  
503 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
504 segments_needed: A A B B B C C C C D E E E
505 gapfillers: B B B D
506 all_segments: A A A B B B C C C C D E E E E E
507  
508 ... where C<A>, C<C> and C<E> are elements coming from provided sets and C<B>
509 and C<D> are coming from newly-created gap-filling sets.
510  
511 =item *
512  
513 $gf = Set::Integer::Gapfillers->new(
514 lower => 10,
515 upper => 22,
516 sets => [
517 [ 9, 11 ],
518 [ 15, 18 ],
519 [ 20, 20 ],
520 ],
521 );
522  
523 The three non-constructor methods return sets as follows:
524  
525 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
526 segments_needed: A A B B B C C C C D E F F
527 gapfillers: B B B D F F
528 all_segments: A A A B B B C C C C D E F F
529  
530 ... where C<A>, C<C> and C<E> are elements coming from provided sets and C<B>,
531 C<D> and C<F> are coming from newly-created gap-filling sets.
532  
533 =back
534  
535 =item 2. Why do the output methods, by default, return references to two-element arrays rather than the full range of integers needed?
536  
537 Memory and speed.
538  
539 In an earlier implementation, F<Set::Integer::Gapfillers> calculated its return values
540 by supplying the constructor's C<sets> argument with a list of references to
541 arrays of consecutive integers -- C<[ 12 .. 22 ]> -- rather than a list of
542 references to two-element arrays of the lower and upper bounds of the integer
543 ranges desired -- C<[ 12, 22 ]>. All internal calculations were made by
544 comparing the lower and upper bounds supplied with the arrays supplied. This
545 proved to be a memory hog and slow.
546  
547 F<Set::Integer::Gapfillers> was then revised to
548 require the user to supply only the beginning and end points of the provided
549 segments. Although this complicated the logic of the internal calculations
550 for the module author, it led to a vastly reduced memory footprint and vast
551 speedup in producing results. It was therefore decided to make the output
552 methods return values in the same manner, I<i.e.,> beginning and end points of
553 ranges, rather than the entire ranges.
554  
555 However, what an end-user of F<Set::Integer::Gapfillers> might really be after is those
556 entire ranges. Hence, the C<expand => 1> option is provided so that the
557 results look like this:
558  
559 $gf = Set::Integer::Gapfillers->new(
560 lower => -12,
561 upper => 62,
562 sets => [
563 [ 1, 17 ],
564 [ 25, 42 ],
565 [ 44, 50 ],
566 ],
567 );
568  
569 $segments_needed_ref = $gf->( expand => 1);
570 __END__
571 $segments_needed_ref: [
572 [-12 .. 0 ], # without 'expand': [ -12, 0 ]
573 [ 1 .. 17 ],
574 [ 18 .. 24 ],
575 [ 25 .. 42 ],
576 [ 43 .. 43 ],
577 [ 44 .. 50 ],
578 [ 51 .. 62 ],
579 ]
580  
581 =back
582  
583 =head1 BUGS
584  
585 None reported so far.
586  
587 =head1 SUPPORT
588  
589 Via e-mail to author at address below.
590  
591 =head1 AUTHOR
592  
593 James E Keenan
594 CPAN ID: JKEENAN
595 jkeenan@cpan.org
596 http://search.cpan.org/~jkeenan/
597  
598 =head1 ACKNOWLEDGEMENTS
599  
600 This Perl extension has its origin in a question I posed on Perlmonks
601 (L<http://perlmonks.org/?node_id=539350>). BrowserUK's response
602 (L<http://perlmonks.org/?node_id=539357>) was ingenious and terse and led me
603 to think that the solution could be modularized. However, when I realized
604 that my original question had not fully specified my objective, I found I
605 could no longer use BrowserUK's algorithm and had to work my own out -- so any
606 bugs are my fault, not his!
607  
608 =head1 COPYRIGHT
609  
610 Copyright 2006. James E. Keenan. United States.
611  
612 This program is free software; you can redistribute
613 it and/or modify it under the same terms as Perl itself.
614  
615 The full text of the license can be found in the
616 LICENSE file included with this module.
617  
618 =head1 SEE ALSO
619  
620 perl(1). During the Perlmonks thread mentioned in ACKNOWLEDGMENTS, reference
621 was made to CPAN module Set::Infinite (L<http://search.cpan.org/dist/Set-Infinite/>), and specifically to
622 C<Set::Infinite::minus()> as possibly providing another solution to the
623 gap-filling problem. Set::Array and Set::Scalar should also be consulted if
624 you need a wider arrange of methods to perform set operations.
625  
626 =cut
↑ Summary