Infinite/Arithmetic.pm

filter: Statementstmt 35.5% 58 / 163 Branchbran 6.6% 2 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 38.3% 18 / 47 Podpod n/a totaltotal 31.2% 78 / 250 CC 18 SCAR 27.0
CC
18
Cov
30%
__ANON__
72.0
__ANON__
42.0
__ANON__
12.0
CRAP 14.9
↑ Summary Infinite.pm »
1 package Set::Infinite::Arithmetic;
2 # Copyright (c) 2001 Flavio Soibelmann Glock. All rights reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the same terms as Perl itself.
5  
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # use warnings;
8 11 require Exporter;
9 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use vars qw( @EXPORT @EXPORT_OK $inf );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 @EXPORT = qw();
16 11 @EXPORT_OK = qw();
17 # @EXPORT_OK = qw( %subs_offset2 %Offset_to_value %Value_to_offset %Init_quantizer );
18  
19 11 $inf = 100**100**100; # $Set::Infinite::inf; doesn't work! (why?)
20  
21 =head2 NAME
22  
23 Set::Infinite::Arithmetic - Scalar operations used by quantize() and offset()
24  
25 =head2 AUTHOR
26  
27 Flavio Soibelmann Glock - fglock@pucrs.br
28  
29 =cut
30  
31 11 ▶use vars qw( $day_size $hour_size $minute_size $second_size );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 $day_size = timegm(0,0,0,2,3,2001) - timegm(0,0,0,1,3,2001);
33 11 $hour_size = $day_size / 24;
34 11 $minute_size = $hour_size / 60;
35 11 $second_size = $minute_size / 60;
36  
37 11 ▶use vars qw( %_MODE %subs_offset2 %Offset_to_value @week_start %Init_quantizer %Value_to_offset %Offset_to_value );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head2 %_MODE hash of subs
40  
41 $a->offset ( value => [1,2], mode => 'offset', unit => 'days' );
42  
43 $a->offset ( value => [1,2, -5,-4], mode => 'offset', unit => 'days' );
44  
45 note: if mode = circle, then "-5" counts from end (like a Perl negative array index).
46  
47 $a->offset ( value => [1,2], mode => 'offset', unit => 'days', strict => $a );
48  
49 option 'strict' will return intersection($a,offset). Default: none.
50  
51 =cut
52  
53 # return value = ($this, $next, $cmp)
54 %_MODE = (
55 4 ▶ circle => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
56 4 ▶ if ($_[3] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3] >= 0) { } 2 2 4
57 2 &{ $_[0] } ($_[1], $_[3], $_[4] )
58 }
59 else {
60 2 &{ $_[0] } ($_[2], $_[3], $_[4] )
61 }
62 },
63 2367 ▶ begin => sub { &{ $_[0] } ($_[1], $_[3], $_[4] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 6 ▶ end => sub { &{ $_[0] } ($_[2], $_[3], $_[4] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 17 ▶ offset => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 17 my ($this, undef) = &{ $_[0] } ($_[1], $_[3], $_[4] );
67 17 my (undef, $next) = &{ $_[0] } ($_[2], $_[3], $_[4] );
68 17 ($this, $next);
69 }
70 11 );
71  
72  
73 =head2 %subs_offset2($object, $offset1, $offset2)
74  
75 &{ $subs_offset2{$unit} } ($object, $offset1, $offset2);
76  
77 A hash of functions that return:
78  
79 ($object+$offset1, $object+$offset2)
80  
81 in $unit context.
82  
83 Returned $object+$offset1, $object+$offset2 may be scalars or objects.
84  
85 =cut
86  
87 %subs_offset2 = (
88 0 ▶ weekdays => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
89 # offsets to week-day specified
90 # 0 = first sunday from today (or today if today is sunday)
91 # 1 = first monday from today (or today if today is monday)
92 # 6 = first friday from today (or today if today is friday)
93 # 13 = second friday from today
94 # -1 = last saturday from today (not today, even if today were saturday)
95 # -2 = last friday
96 0 my ($self, $index1, $index2) = @_;
97 0 ▶ return ($self, $self) if $self == $inf;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $Set::Infinite::Arithmetic::inf 0 0 0
98 # my $class = ref($self);
99 0 my @date = gmtime( $self );
100 0 my $wday = $date[6];
101 0 my ($tmp1, $tmp2);
102  
103 0 $tmp1 = $index1 - $wday;
104 0 ▶ if ($index1 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index1 >= 0) { } 0 0 0
105 0 ▶ $tmp1 += 7 if $tmp1 < 0; # it will only happen next week
Branch: 1 branch
BranchTrueFalseTotal
if $tmp1 < 0 0 0 0
106 }
107 else {
108 0 ▶ $tmp1 += 7 if $tmp1 < -7; # if will happen this week
Branch: 1 branch
BranchTrueFalseTotal
if $tmp1 < -7 0 0 0
109 }
110  
111 0 $tmp2 = $index2 - $wday;
112 0 ▶ if ($index2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index2 >= 0) { } 0 0 0
113 0 ▶ $tmp2 += 7 if $tmp2 < 0; # it will only happen next week
Branch: 1 branch
BranchTrueFalseTotal
if $tmp2 < 0 0 0 0
114 }
115 else {
116 0 ▶ $tmp2 += 7 if $tmp2 < -7; # if will happen this week
Branch: 1 branch
BranchTrueFalseTotal
if $tmp2 < -7 0 0 0
117 }
118  
119 # print " [ OFS:weekday $self $tmp1 $tmp2 ] \n";
120 # $date[3] += $tmp1;
121 0 $tmp1 = $self + $tmp1 * $day_size;
122 # $date[3] += $tmp2 - $tmp1;
123 0 $tmp2 = $self + $tmp2 * $day_size;
124  
125 0 ($tmp1, $tmp2);
126 },
127 0 ▶ years => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
128 0 my ($self, $index, $index2) = @_;
129 0 ▶ return ($self, $self) if $self == $inf;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $Set::Infinite::Arithmetic::inf 0 0 0
130 # my $class = ref($self);
131 # print " [ofs:year:$self -- $index]\n";
132 0 my @date = gmtime( $self );
133 0 $date[5] += 1900 + $index;
134 0 my $tmp = timegm(@date);
135  
136 0 $date[5] += $index2 - $index;
137 0 my $tmp2 = timegm(@date);
138  
139 0 ($tmp, $tmp2);
140 },
141 0 ▶ months => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
142 0 my ($self, $index, $index2) = @_;
143 # carp " [ofs:month:$self -- $index -- $inf]";
144 0 ▶ return ($self, $self) if $self == $inf;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $Set::Infinite::Arithmetic::inf 0 0 0
145 # my $class = ref($self);
146 0 my @date = gmtime( $self );
147  
148 0 my $mon = $date[4] + $index;
149 0 my $year = $date[5] + 1900;
150 # print " [OFS: month: from $year$mon ]\n";
151 0 ▶ if (($mon > 11) or ($mon < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon > 11 or $mon < 0) 0 0 0
Condition0%0/3
$mon > 11 or $mon < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$mon > 11 or $mon < 0
$mon > 11 $mon < 0
Truth table0%0/3
A or B
A= $mon > 11B= $mon < 0
ABresult
1X1
011
000
152 0 my $addyear = floor($mon / 12);
153 0 $mon = $mon - 12 * $addyear;
154 0 $year += $addyear;
155 }
156  
157 0 my $mon2 = $date[4] + $index2;
158 0 my $year2 = $date[5] + 1900;
159 0 ▶ if (($mon2 > 11) or ($mon2 < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon2 > 11 or $mon2 < 0) 0 0 0
Condition0%0/3
$mon2 > 11 or $mon2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$mon2 > 11 or $mon2 < 0
$mon2 > 11 $mon2 < 0
Truth table0%0/3
A or B
A= $mon2 > 11B= $mon2 < 0
ABresult
1X1
011
000
160 0 my $addyear2 = floor($mon2 / 12);
161 0 $mon2 = $mon2 - 12 * $addyear2;
162 0 $year2 += $addyear2;
163 }
164  
165 # print " [OFS: month: to $year $mon ]\n";
166  
167 0 $date[4] = $mon;
168 0 $date[5] = $year;
169 0 my $tmp = timegm(@date);
170  
171 0 $date[4] = $mon2;
172 0 $date[5] = $year2;
173 0 my $tmp2 = timegm(@date);
174  
175 0 ($tmp, $tmp2);
176 },
177 0 ▶ days => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 ( $_[0] + $_[1] * $day_size,
179 $_[0] + $_[2] * $day_size,
180 )
181 },
182 0 ▶ weeks => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 ( $_[0] + $_[1] * (7 * $day_size),
184 $_[0] + $_[2] * (7 * $day_size),
185 )
186 },
187 0 ▶ hours => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 # carp " [ $_[0]+$_[1] hour = ".( $_[0] + $_[1] * $hour_size )." mode=".($_[0]->{mode})." ]";
189 0 ( $_[0] + $_[1] * $hour_size,
190 $_[0] + $_[2] * $hour_size,
191 )
192 },
193 0 ▶ minutes => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 ( $_[0] + $_[1] * $minute_size,
195 $_[0] + $_[2] * $minute_size,
196 )
197 },
198 0 ▶ seconds => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 ( $_[0] + $_[1] * $second_size,
200 $_[0] + $_[2] * $second_size,
201 )
202 },
203 2411 ▶ one => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
204 2411 ( $_[0] + $_[1],
205 $_[0] + $_[2],
206 )
207 },
208 11 );
209  
210  
211 11 @week_start = ( 0, -1, -2, -3, 3, 2, 1, 0, -1, -2, -3, 3, 2, 1, 0 );
212  
213 =head2 %Offset_to_value($object, $offset)
214  
215 =head2 %Init_quantizer($object)
216  
217 $Offset_to_value{$unit} ($object, $offset);
218  
219 $Init_quantizer{$unit} ($object);
220  
221 Maps an 'offset value' to a 'value'
222  
223 A hash of functions that return ( int($object) + $offset ) in $unit context.
224  
225 Init_quantizer subroutines must be called before using subs_offset1 functions.
226  
227 int(object)+offset is a scalar.
228  
229 Offset_to_value is optimized for calling it multiple times on the same object,
230 with different offsets. That's why there is a separate initialization
231 subroutine.
232  
233 $self->{offset} is created on initialization. It is an index used
234 by the memoization cache.
235  
236 =cut
237  
238 %Offset_to_value = (
239 0 ▶ weekyears => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 0 my ($self, $index) = @_;
241 my $epoch = timegm( 0,0,0,
242 0 1,0,$self->{offset} + $self->{quant} * $index);
243 0 my @time = gmtime($epoch);
244 # print " [QT_D:weekyears:$self->{offset} + $self->{quant} * $index]\n";
245 # year modulo week
246 # print " [QT:weekyears: time = ",join(";", @time )," ]\n";
247 0 $epoch += ( $week_start[$time[6] + 7 - $self->{wkst}] ) * $day_size;
248 # print " [QT:weekyears: week=",join(";", gmtime($epoch) )," wkst=$self->{wkst} tbl[",$time[6] + 7 - $self->{wkst},"]=",$week_start[$time[6] + 7 - $self->{wkst}]," ]\n\n";
249  
250 my $epoch2 = timegm( 0,0,0,
251 0 1,0,$self->{offset} + $self->{quant} * (1 + $index) );
252 0 @time = gmtime($epoch2);
253 0 $epoch2 += ( $week_start[$time[6] + 7 - $self->{wkst}] ) * $day_size;
254 0 ( $epoch, $epoch2 );
255 },
256 0 ▶ years => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 0 my $index = $_[0]->{offset} + $_[0]->{quant} * $_[1];
258 ( timegm( 0,0,0, 1, 0, $index),
259 0 timegm( 0,0,0, 1, 0, $index + $_[0]->{quant}) )
260 },
261 0 ▶ months => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 my $mon = $_[0]->{offset} + $_[0]->{quant} * $_[1];
263 0 my $year = int($mon / 12);
264 0 $mon -= $year * 12;
265 0 my $tmp = timegm( 0,0,0, 1, $mon, $year);
266  
267 0 $mon += $year * 12 + $_[0]->{quant};
268 0 $year = int($mon / 12);
269 0 $mon -= $year * 12;
270 0 ( $tmp, timegm( 0,0,0, 1, $mon, $year) );
271 },
272 0 ▶ weeks => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
273 0 my $tmp = 3 * $day_size + $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
274 ($tmp, $tmp + $_[0]->{quant})
275 0 },
276 0 ▶ days => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 my $tmp = $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
278 ($tmp, $tmp + $_[0]->{quant})
279 0 },
280 8 ▶ hours => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
281 8 my $tmp = $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
282 ($tmp, $tmp + $_[0]->{quant})
283 8 },
284 0 ▶ minutes => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 my $tmp = $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
286 ($tmp, $tmp + $_[0]->{quant})
287 0 },
288 0 ▶ seconds => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 my $tmp = $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
290 ($tmp, $tmp + $_[0]->{quant})
291 0 },
292 3316 ▶ one => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
293 3316 my $tmp = $_[0]->{quant} * ($_[0]->{offset} + $_[1]);
294 ($tmp, $tmp + $_[0]->{quant})
295 3316 },
296 11 );
297  
298  
299 # Maps an 'offset value' to a 'value'
300  
301 %Value_to_offset = (
302 2628 ▶ one => sub { floor( $_[1] / $_[0]{quant} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
303 0 ▶ seconds => sub { floor( $_[1] / $_[0]{quant} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
304 0 ▶ minutes => sub { floor( $_[1] / $_[0]{quant} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 8 ▶ hours => sub { floor( $_[1] / $_[0]{quant} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
306 0 ▶ days => sub { floor( $_[1] / $_[0]{quant} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
307 0 ▶ weeks => sub { floor( ($_[1] - 3 * $day_size) / $_[0]{quant} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
308 0 ▶ months => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
309 0 my @date = gmtime( 0 + $_[1] );
310 0 my $tmp = $date[4] + 12 * (1900 + $date[5]);
311 0 floor( $tmp / $_[0]{quant} );
312 },
313 0 ▶ years => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 0 my @date = gmtime( 0 + $_[1] );
315 0 my $tmp = $date[5] + 1900;
316 0 floor( $tmp / $_[0]{quant} );
317 },
318 0 ▶ weekyears => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
319  
320 0 my ($self, $value) = @_;
321 0 my @date;
322  
323 # find out YEAR number
324 0 @date = gmtime( 0 + $value );
325 0 my $year = floor( $date[5] + 1900 / $self->{quant} );
326  
327 # what is the EPOCH for this week-year's begin ?
328 0 my $begin_epoch = timegm( 0,0,0, 1,0,$year);
329 0 @date = gmtime($begin_epoch);
330 0 $begin_epoch += ( $week_start[$date[6] + 7 - $self->{wkst}] ) * $day_size;
331  
332 # what is the EPOCH for this week-year's end ?
333 0 my $end_epoch = timegm( 0,0,0, 1,0,$year+1);
334 0 @date = gmtime($end_epoch);
335 0 $end_epoch += ( $week_start[$date[6] + 7 - $self->{wkst}] ) * $day_size;
336  
337 0 ▶ $year-- if $value < $begin_epoch;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $begin_epoch 0 0 0
338 0 ▶ $year++ if $value >= $end_epoch;
Branch: 1 branch
BranchTrueFalseTotal
if $value >= $end_epoch 0 0 0
339  
340 # carp " value=$value offset=$year this_epoch=".$begin_epoch;
341 # carp " next_epoch=".$end_epoch;
342  
343 0 $year;
344 },
345 11 );
346  
347 # Initialize quantizer
348  
349 %Init_quantizer = (
350 1314 ▶ one => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
351 0 ▶ seconds => sub { $_[0]->{quant} *= $second_size },
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 0 ▶ minutes => sub { $_[0]->{quant} *= $minute_size },
Subroutine __ANON__: not called CC=1 SCAR=6.9
353 4 ▶ hours => sub { $_[0]->{quant} *= $hour_size },
Subroutine __ANON__: called CC=1 SCAR=0.0
354 0 ▶ days => sub { $_[0]->{quant} *= $day_size },
Subroutine __ANON__: not called CC=1 SCAR=6.9
355 0 ▶ weeks => sub { $_[0]->{quant} *= 7 * $day_size },
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 ▶ months => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
357 0 ▶ years => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
358 0 ▶ weekyears => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
359 0 ▶ $_[0]->{wkst} = 1 unless defined $_[0]->{wkst};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"wkst"} 0 0 0
360 # select which 'cache' to use
361 # $_[0]->{memo} .= $_[0]->{wkst};
362 },
363 11 );
364  
365  
366 11 1;
367  
↑ Summary Infinite.pm »