Server IP : 85.214.239.14 / Your IP : 18.217.89.130 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/2/cwd/proc/2/root/proc/2/task/2/root/usr/lib/x86_64-linux-gnu/perl5/5.36/Algorithm/Diff/ |
Upload File : |
package Algorithm::Diff::XS; use 5.006; use strict; use warnings; use vars '$VERSION'; use Algorithm::Diff; BEGIN { $VERSION = '0.04'; require XSLoader; XSLoader::load( __PACKAGE__, $VERSION ); my $code = do { open my $fh, '<', $INC{'Algorithm/Diff.pm'} or die "Cannot read $INC{'Algorithm/Diff.pm'}: $!"; local $/; <$fh>; }; { no warnings; local $@; $code =~ s/Algorithm::Diff/Algorithm::Diff::XS/g; $code =~ s/sub LCSidx/sub LCSidx_old/g; $code = "#line 1 " . __FILE__ . "\n$code"; eval $code; die $@ if $@; } no warnings 'redefine'; sub LCSidx { my $lcs = Algorithm::Diff::XS->_CREATE_; my ( @l, @r ); for my $chunk ( $lcs->_LCS_(@_) ) { push @l, $chunk->[0]; push @r, $chunk->[1]; } return ( \@l, \@r ); } } sub _line_map_ { my $ctx = shift; my %lines; push @{ $lines{ $_[$_] } }, $_ for 0 .. $#_; # values MUST be SvIOK \%lines; } sub _LCS_ { my ( $ctx, $a, $b ) = @_; my ( $amin, $amax, $bmin, $bmax ) = ( 0, $#$a, 0, $#$b ); while ( $amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin] ) { $amin++; $bmin++; } while ( $amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax] ) { $amax--; $bmax--; } my $h = $ctx->_line_map_( @$b[ $bmin .. $bmax ] ); # line numbers are off by $bmin return $amin + _core_loop_( $ctx, $a, $amin, $amax, $h ) + ( $#$a - $amax ) unless wantarray; my @lcs = _core_loop_( $ctx, $a, $amin, $amax, $h ); if ( $bmin > 0 ) { $_->[1] += $bmin for @lcs; # correct line numbers } map( [ $_ => $_ ], 0 .. ( $amin - 1 ) ), @lcs, map( [ $_ => ++$bmax ], ( $amax + 1 ) .. $#$a ); } 1; __END__ =head1 NAME Algorithm::Diff::XS - Algorithm::Diff with XS core loop =head1 SYNOPSIS # Drop-in replacement to Algorithm::Diff, but "compact_diff" # and C<LCSidx> will run much faster for large data sets. use Algorithm::Diff::XS qw( compact_diff LCSidx ); =head1 DESCRIPTION This module is a simple re-packaging of Joe Schaefer's excellent but not very well-known L<Algorithm::LCS> with a drop-in interface that simply re-uses the installed version of the L<Algorithm::Diff> module. Note that only the C<LCSidx> function is optimized in XS at the moment, which means only C<compact_diff> will get significantly faster for large data sets, while C<diff> and C<sdiff> will run in identical speed as C<Algorithm::Diff>. =head1 BENCHMARK Rate Algorithm::Diff Algorithm::Diff::XS Algorithm::Diff 14.7/s -- -98% Algorithm::Diff::XS 806/s 5402% -- The benchmarking script is as below: my @data = ([qw/a b d/ x 50], [qw/b a d c/ x 50]); cmpthese( 500, { 'Algorithm::Diff' => sub { Algorithm::Diff::compact_diff(@data) }, 'Algorithm::Diff::XS' => sub { Algorithm::Diff::XS::compact_diff(@data) }, }); =head1 SEE ALSO L<Algorithm::Diff>, L<Algorithm::LCS>. =head1 AUTHORS Audrey Tang E<lt>cpan@audreyt.orgE<gt> =head1 COPYRIGHT Copyright 2008 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>. Contains derived code copyrighted 2003 by Joe Schaefer, E<lt>joe+cpan@sunstarsys.comE<gt>. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut