/usr/share/perl5/Set/Infinite/_recurrence.pm is in libdatetime-set-perl 0.31-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | # Copyright (c) 2003 Flavio Soibelmann Glock. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package Set::Infinite::_recurrence;
use strict;
use constant INFINITY => 100 ** 100 ** 100 ;
use constant NEG_INFINITY => -1 * (100 ** 100 ** 100);
use vars qw( @ISA $PRETTY_PRINT $max_iterate );
@ISA = qw( Set::Infinite );
use Set::Infinite 0.5502;
BEGIN {
$PRETTY_PRINT = 1; # enable Set::Infinite debug
$max_iterate = 20;
# TODO: inherit %Set::Infinite::_first / _last
# in a more "object oriented" way
$Set::Infinite::_first{_recurrence} =
sub {
my $self = $_[0];
my ($callback_next, $callback_previous) = @{ $self->{param} };
my ($min, $min_open) = $self->{parent}->min_a;
# my ($max, $max_open) = $self->{parent}->max_a;
my ( $min1, $min2 );
$min1 = $callback_next->( $min );
if ( ! $min_open )
{
$min2 = $callback_previous->( $min1 );
$min1 = $min2 if defined $min2 && $min == $min2;
}
my $start = $callback_next->( $min1 );
my $end = $self->{parent}->max;
#print STDERR "set ";
#print STDERR $start->datetime
# unless $start == INFINITY;
#print STDERR " - " ;
#print STDERR $end->datetime
# unless $end == INFINITY;
#print STDERR "\n";
return ( $self->new( $min1 ), undef )
if $start > $end;
return ( $self->new( $min1 ),
$self->new( $start, $end )->
# $self->new( {a => $start, b => $end, open_end => $max_open} )->
_function( '_recurrence', @{ $self->{param} } ) );
};
$Set::Infinite::_last{_recurrence} =
sub {
my $self = $_[0];
my ($callback_next, $callback_previous) = @{ $self->{param} };
my ($max, $max_open) = $self->{parent}->max_a;
my ( $max1, $max2 );
$max1 = $callback_previous->( $max );
if ( ! $max_open )
{
$max2 = $callback_next->( $max1 );
$max1 = $max2 if $max == $max2;
}
return ( $self->new( $max1 ),
$self->new( $self->{parent}->min,
$callback_previous->( $max1 ) )->
_function( '_recurrence', @{ $self->{param} } ) );
};
}
# $si->_recurrence(
# \&callback_next, \&callback_previous )
#
# Generates "recurrences" from a callback.
# These recurrences are simple lists of dates.
#
# The recurrence generation is based on an idea from Dave Rolsky.
#
# use Data::Dumper;
# use Carp qw(cluck);
sub _recurrence {
my $set = shift;
my ( $callback_next, $callback_previous, $delta ) = @_;
$delta->{count} = 0 unless defined $delta->{delta};
# warn "reusing delta: ". $delta->{count} if defined $delta->{delta};
# warn Dumper( $delta );
if ( $#{ $set->{list} } != 0 || $set->is_too_complex )
{
return $set->iterate(
sub {
$_[0]->_recurrence(
$callback_next, $callback_previous, $delta )
} );
}
# $set is a span
my $result;
if ($set->min != NEG_INFINITY && $set->max != INFINITY)
{
# print STDERR " finite set\n";
my ($min, $min_open) = $set->min_a;
my ($max, $max_open) = $set->max_a;
my ( $min1, $min2 );
$min1 = $callback_next->( $min );
if ( ! $min_open )
{
$min2 = $callback_previous->( $min1 );
$min1 = $min2 if defined $min2 && $min == $min2;
}
$result = $set->new();
# get "delta" - abort if this will take too much time.
unless ( defined $delta->{max_delta} )
{
for ( $delta->{count} .. 10 )
{
if ( $max_open )
{
return $result if $min1 >= $max;
}
else
{
return $result if $min1 > $max;
}
push @{ $result->{list} },
{ a => $min1, b => $min1, open_begin => 0, open_end => 0 };
$min2 = $callback_next->( $min1 );
if ( $delta->{delta} )
{
$delta->{delta} += $min2 - $min1;
}
else
{
$delta->{delta} = $min2 - $min1;
}
$delta->{count}++;
$min1 = $min2;
}
$delta->{max_delta} = $delta->{delta} * 40;
}
if ( $max < $min + $delta->{max_delta} )
{
for ( 1 .. 200 )
{
if ( $max_open )
{
return $result if $min1 >= $max;
}
else
{
return $result if $min1 > $max;
}
push @{ $result->{list} },
{ a => $min1, b => $min1, open_begin => 0, open_end => 0 };
$min1 = $callback_next->( $min1 );
}
}
# cluck "give up";
}
# return a "_function", such that we can backtrack later.
my $func = $set->_function( '_recurrence', $callback_next, $callback_previous, $delta );
# removed - returning $result doesn't help on speed
## return $func->_function2( 'union', $result ) if $result;
return $func;
}
sub is_forever
{
$#{ $_[0]->{list} } == 0 &&
$_[0]->max == INFINITY &&
$_[0]->min == NEG_INFINITY
}
sub _is_recurrence
{
exists $_[0]->{method} &&
$_[0]->{method} eq '_recurrence' &&
$_[0]->{parent}->is_forever
}
sub intersection
{
my ($s1, $s2) = (shift,shift);
if ( exists $s1->{method} && $s1->{method} eq '_recurrence' )
{
# optimize: recurrence && span
return $s1->{parent}->
intersection( $s2, @_ )->
_recurrence( @{ $s1->{param} } )
unless ref($s2) && exists $s2->{method};
# optimize: recurrence && recurrence
if ( $s1->{parent}->is_forever &&
ref($s2) && _is_recurrence( $s2 ) )
{
my ( $next1, $previous1 ) = @{ $s1->{param} };
my ( $next2, $previous2 ) = @{ $s2->{param} };
return $s1->{parent}->_function( '_recurrence',
sub {
# intersection of parent 'next' callbacks
my ($n1, $n2);
my $iterate = 0;
$n2 = $next2->( $_[0] );
while(1) {
$n1 = $next1->( $previous1->( $n2 ) );
return $n1 if $n1 == $n2;
$n2 = $next2->( $previous2->( $n1 ) );
return if $iterate++ == $max_iterate;
}
},
sub {
# intersection of parent 'previous' callbacks
my ($p1, $p2);
my $iterate = 0;
$p2 = $previous2->( $_[0] );
while(1) {
$p1 = $previous1->( $next1->( $p2 ) );
return $p1 if $p1 == $p2;
$p2 = $previous2->( $next2->( $p1 ) );
return if $iterate++ == $max_iterate;
}
},
);
}
}
return $s1->SUPER::intersection( $s2, @_ );
}
sub union
{
my ($s1, $s2) = (shift,shift);
if ( $s1->_is_recurrence &&
ref($s2) && _is_recurrence( $s2 ) )
{
# optimize: recurrence || recurrence
my ( $next1, $previous1 ) = @{ $s1->{param} };
my ( $next2, $previous2 ) = @{ $s2->{param} };
return $s1->{parent}->_function( '_recurrence',
sub { # next
my $n1 = $next1->( $_[0] );
my $n2 = $next2->( $_[0] );
return $n1 < $n2 ? $n1 : $n2;
},
sub { # previous
my $p1 = $previous1->( $_[0] );
my $p2 = $previous2->( $_[0] );
return $p1 > $p2 ? $p1 : $p2;
},
);
}
return $s1->SUPER::union( $s2, @_ );
}
=head1 NAME
Set::Infinite::_recurrence - Extends Set::Infinite with recurrence functions
=head1 SYNOPSIS
$recurrence = $base_set->_recurrence ( \&next, \&previous );
=head1 DESCRIPTION
This is an internal class used by the DateTime::Set module.
The API is subject to change.
It provides all functionality provided by Set::Infinite, plus the ability
to define recurrences with arbitrary objects, such as dates.
=head1 METHODS
=over 4
=item * _recurrence ( \&next, \&previous )
Creates a recurrence set. The set is defined inside a 'base set'.
$recurrence = $base_set->_recurrence ( \&next, \&previous );
The recurrence functions take one argument, and return the 'next' or
the 'previous' occurence.
Example: defines the set of all 'integer numbers':
use strict;
use Set::Infinite::_recurrence;
use POSIX qw(floor);
# define the recurrence span
my $forever = Set::Infinite::_recurrence->new(
Set::Infinite::_recurrence::NEG_INFINITY,
Set::Infinite::_recurrence::INFINITY
);
my $recurrence = $forever->_recurrence(
sub { # next
floor( $_[0] + 1 )
},
sub { # previous
my $tmp = floor( $_[0] );
$tmp < $_[0] ? $tmp : $_[0] - 1
},
);
print "sample recurrence ",
$recurrence->intersection( -5, 5 ), "\n";
# sample recurrence -5,-4,-3,-2,-1,0,1,2,3,4,5
{
my $x = 234.567;
print "next occurence after $x = ",
$recurrence->{param}[0]->( $x ), "\n"; # 235
print "previous occurence before $x = ",
$recurrence->{param}[2]->( $x ), "\n"; # 234
}
{
my $x = 234;
print "next occurence after $x = ",
$recurrence->{param}[0]->( $x ), "\n"; # 235
print "previous occurence before $x = ",
$recurrence->{param}[2]->( $x ), "\n"; # 233
}
=item * is_forever
Returns true if the set is a single span,
ranging from -Infinity to Infinity.
=item * _is_recurrence
Returns true if the set is an unbounded recurrence,
ranging from -Infinity to Infinity.
=back
=head1 CONSTANTS
=over 4
=item * INFINITY
The C<Infinity> value.
=item * NEG_INFINITY
The C<-Infinity> value.
=back
=head1 SUPPORT
Support is offered through the C<datetime@perl.org> mailing list.
Please report bugs using rt.cpan.org
=head1 AUTHOR
Flavio Soibelmann Glock <fglock@gmail.com>
The recurrence generation algorithm is based on an idea from Dave Rolsky.
=head1 COPYRIGHT
Copyright (c) 2003 Flavio Soibelmann Glock. All rights reserved.
This program is free software; you can distribute it and/or
modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file
included with this module.
=head1 SEE ALSO
Set::Infinite
DateTime::Set
For details on the Perl DateTime Suite project please see
L<http://datetime.perl.org>.
=cut
|