Algorithm-Merge

 view release on metacpan or  search on metacpan

Merge.pm  view on Meta::CPAN

        @diffs{(BC_B, BC_C)} = ([], []);  
        $left = BC_B; $right = BC_C; 
        Algorithm::Diff::traverse_sequences( $bdoc, $cdoc, $ts_callbacks, $keyGen, @_);

        if(join(",", @{$diffs{&CB_B}}) ne join(",", @{$diffs{&BC_B}}) ||
           join(",", @{$diffs{&CB_C}}) ne join(",", @{$diffs{&BC_C}}))
        {
            @bdoc_save = splice @{$bdoc}, $target_len;
            @cdoc_save = splice @{$cdoc}, $target_len;
            
            carp "Algorithm::Diff::diff is not symmetric for second and third sequences - results might not be correct";
        }

        @diffs{(BC_B, BC_C)} = ([], []);
        $left = BC_B; $right = BC_C;
        Algorithm::Diff::traverse_sequences( $bdoc, $cdoc, $ts_callbacks, $keyGen, @_);

        if(scalar(@bdoc_save) || scalar(@cdoc_save)) {
            push @{$diffs{&BC_B}}, ($target_len .. $b_len) if $target_len < $b_len;
            push @{$diffs{&BC_C}}, ($target_len .. $c_len) if $target_len < $c_len;
        

Merge.pm  view on Meta::CPAN

           }, $key_generation_function);


=head1 USAGE

This module complements L<Algorithm::Diff|Algorithm::Diff> by 
providing three-way merge and diff functions.

In this documentation, the first list to C<diff3>, C<merge>, and 
C<traverse_sequences3> is 
called the `original' list.  The second list is the `left' list.  The 
third list is the `right' list.

The optional key generation arguments are the same as in 
L<Algorithm::Diff|Algorithm::Diff>.  See L<Algorithm::Diff> for more 
information.

=head2 diff3

Given references to three lists of items, C<diff3> performs a 
three-way difference.

Merge.pm  view on Meta::CPAN


=head2 merge

Given references to three lists of items, C<merge> performs a three-way 
merge.  The C<merge> function uses the C<diff3> function to do most of 
the work.

The only callback currently used is C<CONFLICT> which should be a 
reference to a subroutine that accepts two array references.  The 
first array reference is to a list of elements from the left list.  
The second array reference is to a list of elements from the right list.
This callback should return a list of elements to place in the merged 
list in place of the conflict.

The default C<CONFLICT> callback returns the following:

 q{<!-- ------ START CONFLICT ------ -->},
 (@left),
 q{<!-- ---------------------------- -->},
 (@right),
 q{<!-- ------  END  CONFLICT ------ -->},

Merge.pm  view on Meta::CPAN

=item A_DIFF

This is called if the first sequence is different than the other two 
sequences at the current position.
This callback will be called with one, two, or three arguments.

If one argument, then only the element at the given position from the 
first sequence is not in either of the other two sequences.

If two arguments, then there is no element in the first sequence that 
corresponds to the elements at the given positions in the second and 
third sequences.

If three arguments, then the element at the given position in the first 
sequence is different than the corresponding element in the other two 
sequences, but the other two sequences have corresponding elements.

=item B_DIFF

This is called if the second sequence is different than the other two 
sequences at the current position.
This callback will be called with one, two, or three arguments.   

If one argument, then only the element at the given position from the 
second sequence is not in either of the other two sequences.

If two arguments, then there is no element in the second sequence that 
corresponds to the elements at the given positions in the first and 
third sequences.

If three arguments, then the element at the given position in the second 
sequence is different than the corresponding element in the other two 
sequences, but the other two sequences have corresponding elements.

=item C_DIFF

This is called if the third sequence is different than the other two 
sequences at the current position.
This callback will be called with one, two, or three arguments.   

If one argument, then only the element at the given position from the 
third sequence is not in either of the other two sequences.

If two arguments, then there is no element in the third sequence that 
corresponds to the elements at the given positions in the first and 
second sequences.

If three arguments, then the element at the given position in the third 
sequence is different than the corresponding element in the other two 
sequences, but the other two sequences have corresponding elements.

=item CONFLICT

This is called if all three sequences have different elements at the 
current position.  The three arguments are the current positions within 
each sequence.

Merge.pm  view on Meta::CPAN

=back 4

=head1 BUGS

Most assuredly there are bugs.  If a pattern similar to the above 
example does not work, send it to <jsmith@cpan.org> or report it on 
<http://rt.cpan.org/>, the CPAN bug tracker.

L<Algorithm::Diff|Algorithm::Diff>'s implementation of 
C<traverse_sequences> may not be symmetric with respect to the input 
sequences if the second and third sequence are of different lengths.  
Because of this, C<traverse_sequences3> will calculate the diffs of 
the second and third sequences as passed and swapped.  If the differences 
are not the same, it will issue an `Algorithm::Diff::diff is not symmetric 
for second and third sequences...' warning.  It will try to handle 
this, but there may be some cases where it can't.

=head1 SEE ALSO

L<Algorithm::Diff>.

=head1 AUTHOR

James G. Smith, <jsmith@cpan.org>

README  view on Meta::CPAN


     $trav   = traverse_sequences3(\@ancestor, \@a, \@b, { 
                   # callbacks
               }, $key_generation_function);

USAGE
    This module complements Algorithm::Diff by providing three-way merge and
    diff functions.

    In this documentation, the first list to "diff3", "merge", and
    "traverse_sequences3" is called the `original' list. The second list is
    the `left' list. The third list is the `right' list.

    The optional key generation arguments are the same as in
    Algorithm::Diff. See Algorithm::Diff for more information.

  diff3
    Given references to three lists of items, "diff3" performs a three-way
    difference.

    This function returns an array of operations describing how the left and

README  view on Meta::CPAN

    arrays respectively that the row refers to (in the synopsis, these are
    @ancestor, @a, and @b, respectively).

  merge
    Given references to three lists of items, "merge" performs a three-way
    merge. The "merge" function uses the "diff3" function to do most of the
    work.

    The only callback currently used is "CONFLICT" which should be a
    reference to a subroutine that accepts two array references. The first
    array reference is to a list of elements from the left list. The second
    array reference is to a list of elements from the right list. This
    callback should return a list of elements to place in the merged list in
    place of the conflict.

    The default "CONFLICT" callback returns the following:

     q{<!-- ------ START CONFLICT ------ -->},
     (@left),
     q{<!-- ---------------------------- -->},
     (@right),

README  view on Meta::CPAN

    A_DIFF
        This is called if the first sequence is different than the other two
        sequences at the current position. This callback will be called with
        one, two, or three arguments.

        If one argument, then only the element at the given position from
        the first sequence is not in either of the other two sequences.

        If two arguments, then there is no element in the first sequence
        that corresponds to the elements at the given positions in the
        second and third sequences.

        If three arguments, then the element at the given position in the
        first sequence is different than the corresponding element in the
        other two sequences, but the other two sequences have corresponding
        elements.

    B_DIFF
        This is called if the second sequence is different than the other
        two sequences at the current position. This callback will be called
        with one, two, or three arguments.

        If one argument, then only the element at the given position from
        the second sequence is not in either of the other two sequences.

        If two arguments, then there is no element in the second sequence
        that corresponds to the elements at the given positions in the first
        and third sequences.

        If three arguments, then the element at the given position in the
        second sequence is different than the corresponding element in the
        other two sequences, but the other two sequences have corresponding
        elements.

    C_DIFF
        This is called if the third sequence is different than the other two
        sequences at the current position. This callback will be called with
        one, two, or three arguments.

        If one argument, then only the element at the given position from
        the third sequence is not in either of the other two sequences.

        If two arguments, then there is no element in the third sequence
        that corresponds to the elements at the given positions in the first
        and second sequences.

        If three arguments, then the element at the given position in the
        third sequence is different than the corresponding element in the
        other two sequences, but the other two sequences have corresponding
        elements.

    CONFLICT
        This is called if all three sequences have different elements at the
        current position. The three arguments are the current positions
        within each sequence.

BUGS
    Most assuredly there are bugs. If a pattern similar to the above example
    does not work, send it to <jsmith@cpan.org> or report it on
    <http://rt.cpan.org/>, the CPAN bug tracker.

    Algorithm::Diff's implementation of "traverse_sequences" may not be
    symmetric with respect to the input sequences if the second and third
    sequence are of different lengths. Because of this,
    "traverse_sequences3" will calculate the diffs of the second and third
    sequences as passed and swapped. If the differences are not the same, it
    will issue an `Algorithm::Diff::diff is not symmetric for second and
    third sequences...' warning. It will try to handle this, but there may
    be some cases where it can't.

SEE ALSO
    Algorithm::Diff.

AUTHOR
    James G. Smith, <jsmith@cpan.org>

COPYRIGHT

t/10merge.t  view on Meta::CPAN

# `use' statements are after test definition

my $error_message = 'Algorithm::Diff::diff is not symmetric for second and third sequences';

my(@tests, $tests);

BEGIN {

# test deletion of last member in ancestor
push @tests, [
    [qw(a b c)], # ancestor
    [qw(a b)],   # left
    [qw(a b c)], # right

t/10merge.t  view on Meta::CPAN

    [qw(          r s   b c b     d b e)],
    [qw(  < l m | r s > b c b     d b e)],
    [qw(  < r s | l m > b c b     d b e)],
];

push @tests, [
    [qw(a         b c         b f b d)],
    [qw(  l       b   d       b     d)],
    [qw(      r   b       e   b     d b e)],
    [qw(< l | r > b < d | e > b     d b e)],
    [qw(< r | l > b < e | d > b     d b e)], # Algorithm::Diff::diff should fail (see BUG section of man page) on this one
];

push @tests, [
    [qw(a         b c         b f b c d)],
    [qw(  l       b   d       b       d)], 
    [qw(      r   b       e   b       d b e)],
    [qw(< l | r > b < d | e > b       d b e)],
    [qw(< r | l > b < e | d > b       d b e)], # Algorithm::Diff::diff should fail (see BUG section of man page) on this one
];

push @tests, [  # test conflict at end of sequences
    [qw(a b c)],
    [qw(a b d)],
    [qw(a b e)],
    [qw(a b < d | e >)],
    [qw(a b < e | d >)],
];

t/20diff.t  view on Meta::CPAN

# `use' statements are after test definition

my $error_message = 'Algorithm::Diff::diff is not symmetric for second and third sequences';

my(@tests, $tests);

BEGIN {

# check sequences of equal length (1 element each)
my @results = (
    [ 'u', '', '', '' ],
    [ 'r', '', '', 'c' ],
    [ 'l', '', 'b', '' ],



( run in 0.758 second using v1.01-cache-2.11-cpan-39bf76dae61 )