List::Compare::Functional - Compare elements of two or more lists


List-Compare documentation Contained in the List-Compare distribution.

Index


Code Index:

NAME

Top

List::Compare::Functional - Compare elements of two or more lists

VERSION

Top

This document refers to version 0.37 of List::Compare::Functional. This version was released June 07, 2008. The first released version of List::Compare::Functional was v0.21. Its version numbers are set to be consistent with the other parts of the List::Compare distribution.

Notice of Interface Changes

Certain significant changes to the interface to List::Compare::Functional were made with the introduction of Version 0.25 in April 2004. The documentation immediately below reflects those changes, so if you are first using this module with that or a later version, simply read and follow the documentation below. If, however, you used List::Compare::Functional prior to that version, see the discussion of interface changes farther below: April 2004 Change of Interface.

SYNOPSIS

Top

Getting Started

List::Compare::Functional exports no subroutines by default.

    use List::Compare::Functional qw(:originals :aliases);

will import all publicly available subroutines from List::Compare::Functional. The model for importing just one subroutine from List::Compare::Functional is:

    use List::Compare::Functional qw( get_intersection );

It will probably be most convenient for the user to import functions by using one of the two following export tags:

    use List::Compare::Functional qw(:main :mainrefs);

The assignment of the various comparison functions to export tags is discussed below.

For clarity, we shall begin by discussing comparisons of just two lists at a time. Farther below, we shall discuss comparisons among three or more lists at a time.

Comparing Two Lists Held in Arrays

Comparing Three or More Lists Held in Arrays

Given five lists:

    @Al     = qw(abel abel baker camera delta edward fargo golfer);
    @Bob    = qw(baker camera delta delta edward fargo golfer hilton);
    @Carmen = qw(fargo golfer hilton icon icon jerky kappa);
    @Don    = qw(fargo icon jerky);
    @Ed     = qw(fargo icon icon jerky);

Comparing Lists Held in Seen-Hashes

What is a seen-hash? A seen-hash is a typical Perl implementation of a look-up table: a hash where the value for a given element represents the number of times the element's key is observed in a list. For the purposes of List::Compare::Functional, what is crucial is whether an item is observed in a list or not; how many times the item occurs in a list is, with one exception, irrelevant. (That exception is the get_bag() function and its fraternal twin get_bag_ref(). In this case only, the key in each element of the seen-hash is placed in the bag the number of times indicated by the value of that element.) The value of an element in a List::Compare seen-hash must be a positive integer, but whether that integer is 1 or 1,000,001 is immaterial for all List::Compare::Functional functions except forming a bag.

The two lists compared above were represented by arrays; references to those arrays were passed to the various List::Compare::Functional functions. They could, however, have been represented by seen-hashes such as the following and passed in exactly the same manner to the various functions.

    %Llist = (
        abel   => 2,
        baker  => 1, 
        camera => 1,
        delta  => 1,
        edward => 1,
        fargo  => 1,
        golfer => 1,
    );
    %Rlist = (
        baker  => 1,
        camera => 1,
        delta  => 2,
        edward => 1,
        fargo  => 1,
        golfer => 1,
        hilton => 1,
    );

    @intersection = get_intersection( [ \%Llist, \%Rlist ] );
    @union        = get_union(        [ \%Llist, \%Rlist ] );
    @complement   = get_complement(   [ \%Llist, \%Rlist ] );

and so forth.

To compare three or more lists simultaneously, provide the appropriate List::Compare::Functional function with a first array reference holding a list of three or more references to seen-hashes. Thus,

    @union = get_intersection( [ \%Alpha, \%Beta, \%Gamma ] );

The 'single hashref' format for List::Compare::Functional functions is also available when passing seen-hashes as arguments. Examples:

    @intersection = get_intersection( {
        lists => [ \%Alpha, \%Beta, \%Gamma ],
    } );

    @Ronly = get_complement( {
        lists => [ \%Alpha, \%Beta, \%Gamma ],
        item  => 3,
    } );

    $LR = is_LsubsetR( {
        lists => [ \%Alpha, \%Beta, \%Gamma ],
        pair  => [ 4, 2 ],
    } );

    $memb_hash_ref = are_members_any( {
        lists => [ \%Alpha, \%Beta, \%Gamma ],
        items => [ qw| abel baker fargo hilton zebra | ], 
    } );

Faster Results with the Unsorted Option

By default, List::Compare::Function functions return lists sorted in Perl's default ASCII-betical mode. Sorting entails a performance cost, and if you do not need a sorted list and do not wish to pay this performance cost, you may call the following List::Compare::Function functions with the 'unsorted' option:

    @intersection = get_intersection(        '-u',  [ \@Llist, \@Rlist ] );
    @union        = get_union(               '-u',  [ \@Llist, \@Rlist ] );
    @Lonly        = get_unique(              '-u',  [ \@Llist, \@Rlist ] );
    @Ronly        = get_complement(          '-u',  [ \@Llist, \@Rlist ] );
    @LorRonly     = get_symmetric_difference('-u',  [ \@Llist, \@Rlist ] );
    @bag          = get_bag(                 '-u',  [ \@Llist, \@Rlist ] );

For greater readability, the option may be spelled out:

    @intersection = get_intersection('--unsorted',  [ \@Llist, \@Rlist ] );

or

    @intersection = get_intersection( {
        unsorted => 1,
        lists    => [ \@Llist, \@Rlist ],
    } );

Should you need a reference to an unsorted list as the return value, you may call the unsorted option as follows:

    $intersection_ref = get_intersection_ref(
                            '-u',         [ \@Llist, \@Rlist ] );
    $intersection_ref = get_intersection_ref(
                            '--unsorted', [ \@Llist, \@Rlist ] );

DISCUSSION

Top

General Comments

List::Compare::Functional is a non-object-oriented implementation of very common Perl code used to determine interesting relationships between two or more lists at a time. List::Compare::Functional is based on the same author's List::Compare module found in the same CPAN distribution. List::Compare::Functional is closely modeled on the ''Accelerated'' mode in List::Compare.

For a discussion of the antecedents of this module, see the discussion of the history and development of this module in the documentation to List::Compare.

List::Compare::Functional's Export Tag Groups

By default, List::Compare::Functional exports no functions. You may import individual functions into your main package but may find it more convenient to import via export tag groups. Four such groups are currently defined:

    use List::Compare::Functional qw(:main)
    use List::Compare::Functional qw(:mainrefs)
    use List::Compare::Functional qw(:originals)
    use List::Compare::Functional qw(:aliases)

April 2004 Change of Interface

Note: You can skip this section unless you used List::Compare::Functional prior to the release of Version 0.25 in April 2004.

Version 0.25 initiated a significant change in the interface to this module's various functions. In order to be able to accommodate comparisons among more than two lists, it was necessary to change the type of arguments passed to the various functions. Whereas previously a typical List::Compare::Functional function would be called like this:

    @intersection = get_intersection( \@Llist, \@Rlist ); # SUPERSEDED

... now the references to the lists being compared must now be placed within a wrapper array (anonymous or named), a reference to which is now passed to the function, like so:

    @intersection = get_intersection( [ \@Llist, \@Rlist ] );

... or, alternatively:

    @to_be_compared = (\@Llist, \@Rlist);
    @intersection = get_intersection( \@to_be_compared );

In a similar manner, List::Compare::Functional functions could previously take arguments in the form of references to 'seen-hashes' instead of references to arrays:

    @intersection = get_intersection( \%h0, \%h1 );

(See above for discussion of seen-hashes.) Now, those references to seen-hashes must be placed within a wrapper array (anonymous or named), a reference to which is passed to the function, like so:

    @intersection = get_intersection( [ \%h0, \%h1 ] );

Also, in a similar manner, some List::Compare::Functional functions previously took arguments in addition to the lists being compared. These arguments were simply passed as scalars, like this:

    @memb_arr = is_member_which(\@Llist, \@Rlist, 'abel');

Now these arguments must also be placed within a wrapper array (anonymous or named), a reference to which is now passed to the function, like so:

    @memb_arr = is_member_which( [ \@Llist, \@Rlist ], [ 'abel' ] );

... or, alternatively:

    @to_be_compared = (\@Llist, \@Rlist);
    @opts = ( 'abel' );
    @memb_arr = is_member_which( \@to_be_compared, \@opts );

As in previous versions, for a speed boost the user may provide the '-u' or '--unsorted' option as the first argument to some List::Compare::Functional functions. Using this option, the get_intersection() function above would appear as:

    @intersection = get_intersection( '-u', [ \@Llist, \@Rlist ] );

... or, alternatively:

    @intersection = get_intersection( '--unsorted', [ \@Llist, \@Rlist ] );

The arguments to any List::Compare::Functional function will therefore consist possibly of the unsorted option, and then of either one or two references to arrays, the first of which is a reference to an array of arrays or an array of seen-hashes.

AUTHOR

Top

James E. Keenan (jkeenan@cpan.org). When sending correspondence, please include 'List::Compare::Functional' or 'List-Compare-Functional' in your subject line.

Creation date: May 20, 2002. Last modification date: June 07, 2008. Copyright (c) 2002-08 James E. Keenan. United States. All rights reserved. This is free software and may be distributed under the same terms as Perl itself.


List-Compare documentation Contained in the List-Compare distribution.

package List::Compare::Functional;
#$Id: Functional.pm 1329 2008-06-07 23:49:51Z jimk $
$VERSION = 0.37;
@ISA = qw(Exporter);
@EXPORT_OK = qw|
    get_intersection
    get_intersection_ref
    get_union
    get_union_ref
    get_unique
    get_unique_ref
    get_unique_all
    get_complement
    get_complement_ref
    get_complement_all
    get_symmetric_difference
    get_symmetric_difference_ref
    is_LsubsetR
    is_RsubsetL
    is_LequivalentR
    is_LdisjointR
    is_member_which
    is_member_which_ref
    are_members_which
    is_member_any
    are_members_any
    print_subset_chart
    print_equivalence_chart
    get_shared
    get_shared_ref
    get_nonintersection
    get_nonintersection_ref
    get_symdiff
    get_symdiff_ref
    is_LeqvlntR
    get_bag
    get_bag_ref
    get_version
|;
%EXPORT_TAGS = (
    main => [ qw(
        get_intersection
        get_union
        get_unique
        get_complement
        get_symmetric_difference
        is_LsubsetR
    ) ],
    mainrefs => [ qw(
        get_intersection_ref
        get_union_ref
        get_unique_ref
        get_complement_ref
        get_symmetric_difference_ref
    ) ],
    originals => [ qw(
        get_intersection
        get_intersection_ref
        get_union
        get_union_ref
        get_unique
        get_unique_ref
        get_unique_all
        get_complement
        get_complement_ref
        get_complement_all
        get_symmetric_difference
        get_symmetric_difference_ref
        get_shared
        get_shared_ref
        get_nonintersection
        get_nonintersection_ref
        is_LsubsetR
        is_RsubsetL
        is_LequivalentR
        is_LdisjointR
        is_member_which
        is_member_which_ref
        are_members_which
        is_member_any
        are_members_any
        print_subset_chart
        print_equivalence_chart
        get_bag
        get_bag_ref
        get_version
    ) ],
    aliases => [ qw(
        get_symdiff
        get_symdiff_ref
        is_LeqvlntR
    ) ],
);
use strict;
local $^W = 1;
use Carp;
use List::Compare::Base::_Auxiliary qw(
    _subset_subengine
    _chart_engine_multiple
    _equivalent_subengine
    _calc_seen1
);
use List::Compare::Base::_Auxiliary qw(:calculate :checker :tester);
use List::Compare::Base::_Engine qw( 
    _unique_all_engine 
    _complement_all_engine
);


sub get_union {
    return @{ get_union_ref(@_) };
}

sub get_union_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _union_engine(_argument_checker($argref))
        : return [ sort @{_union_engine(_argument_checker($argref))} ];
}

sub _union_engine {
    my $seenrefsref = _calc_seen1(@_);
    my $unionhashref = _calculate_union_only($seenrefsref);
    return [ keys %{$unionhashref} ];
}

sub get_intersection {
    return @{ get_intersection_ref(@_) };
}

sub get_intersection_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _intersection_engine(_argument_checker($argref))
        : return [ sort @{_intersection_engine(_argument_checker($argref))} ];
}

sub _intersection_engine {
    my $seenrefsref = _calc_seen1(@_);
    my $xintersectionref = _calculate_xintersection_only($seenrefsref);
    my $intersectionref = _calculate_hash_intersection($xintersectionref);
    return [ keys %{$intersectionref} ];
}

sub get_unique {
    return @{ get_unique_ref(@_) };
}

sub get_unique_ref {
    my ($argref, $unsorted) = _alt_construct_tester_3(@_);
    $unsorted
        ? return          _unique_engine(_argument_checker_3($argref))
        : return [ sort @{_unique_engine(_argument_checker_3($argref))} ];
}

sub get_unique_all {
    my ($argref, $unsorted) = _alt_construct_tester_3(@_);
    # currently it doesn't appear that &_unique_all_engine can make use of
    # $unsorted
    return _unique_all_engine(_argument_checker_3a($argref));
}

sub _unique_engine {
    my $tested = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my ($seenref, $xintersectionref) = 
        _calculate_seen_xintersection_only($seenrefsref);
    my %seen = %{$seenref};
    my %xintersection = %{$xintersectionref};

    # Calculate %xunique
    my (%xunique);
    for (my $i = 0; $i <= $#{$seenrefsref}; $i++) {
        my %seenthis = %{$seen{$i}};
        my (@uniquethis, %deductions, %alldeductions);
        # Get those elements of %xintersection which we'll need 
        # to subtract from %seenthis
        foreach (keys %xintersection) {
            my ($left, $right) = split /_/, $_;
            if ($left == $i || $right == $i) {
                $deductions{$_} = $xintersection{$_};
            }
        }
        foreach my $ded (keys %deductions) {
            foreach (keys %{$deductions{$ded}}) {
                $alldeductions{$_}++;
            }
        }
        foreach (keys %seenthis) {
            push(@uniquethis, $_) unless ($alldeductions{$_});
        }
        $xunique{$i} = \@uniquethis;
    }
    return [ @{$xunique{$tested}} ];
}

sub get_complement {
    return @{ get_complement_ref(@_) };
}

sub get_complement_ref {
    my ($argref, $unsorted) = _alt_construct_tester_3(@_);
    $unsorted
        ? return          _complement_engine(_argument_checker_3($argref))
        : return [ sort @{_complement_engine(_argument_checker_3($argref))} ];
}

sub get_complement_all {
    my ($argref, $unsorted) = _alt_construct_tester_3(@_);
    return _complement_all_engine(_argument_checker_3a($argref), $unsorted);
}

sub _complement_engine {
    my $tested = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my ($unionref, $seenref) = _calculate_union_seen_only($seenrefsref);
    my %seen = %{$seenref};
    my @union = keys %{$unionref};

    # Calculate %xcomplement
    # Inputs:  $seenrefsref @union %seen
    my (%xcomplement);
    for (my $i = 0; $i <= $#{$seenrefsref}; $i++) {
        my %seenthis = %{$seen{$i}};
        my @complementthis = ();
        foreach (@union) {
            push(@complementthis, $_) unless (exists $seenthis{$_});
        }
        $xcomplement{$i} = \@complementthis;
    }
    return [ @{$xcomplement{$tested}} ];
}

sub get_symmetric_difference {
    return @{ get_symmetric_difference_ref(@_) };
}

sub get_symmetric_difference_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _symmetric_difference_engine(_argument_checker($argref))
        : return [ sort @{_symmetric_difference_engine(_argument_checker($argref))} ];
}

sub _symmetric_difference_engine {
    my $seenrefsref = _calc_seen1(@_);
    my ($unionref, $xintersectionref) = 
        _calculate_union_xintersection_only($seenrefsref);
    my @union = keys %{$unionref};

    my $sharedref = _calculate_hash_shared($xintersectionref);
    my (@symmetric_difference);
    foreach (@union) {
        push(@symmetric_difference, $_) unless exists ${$sharedref}{$_};
    }
    return \@symmetric_difference;
}

*get_symdiff  = \&get_symmetric_difference;
*get_symdiff_ref  = \&get_symmetric_difference_ref;

sub get_shared {
    return @{ get_shared_ref(@_) };
}

sub get_shared_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _shared_engine(_argument_checker($argref))
        : return [ sort @{_shared_engine(_argument_checker($argref))} ];
}

sub _shared_engine {
    my $seenrefsref = _calc_seen1(@_);
    # Calculate @shared
    # Inputs:  %xintersection
    my $xintersectionref = _calculate_xintersection_only($seenrefsref);
    my $sharedref = _calculate_hash_shared($xintersectionref);
    my @shared = keys %{$sharedref};
    return \@shared;
}

sub get_nonintersection {
    return @{ get_nonintersection_ref(@_) };
}

sub get_nonintersection_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _nonintersection_engine(_argument_checker($argref))
        : return [ sort @{_nonintersection_engine(_argument_checker($argref))} ];
}

sub _nonintersection_engine {
    my $seenrefsref = _calc_seen1(@_);
    my ($unionref, $xintersectionref) = 
        _calculate_union_xintersection_only($seenrefsref);
    my @union = keys %{$unionref};
    my $intersectionref = _calculate_hash_intersection($xintersectionref);
    # Calculate nonintersection
    # Inputs:  @union    %intersection
    my (@nonintersection);
    foreach (@union) {
        push(@nonintersection, $_) unless exists ${$intersectionref}{$_};
    }
    return \@nonintersection;
}

sub is_LsubsetR {
    my $argref = _alt_construct_tester_4(@_);
    return _is_LsubsetR_engine(_argument_checker_4($argref));
}

sub _is_LsubsetR_engine {
    my $testedref = pop(@_);
    my $xsubsetref = _subset_engine(@_);
    return ${$xsubsetref}[${$testedref}[0]][${$testedref}[1]];
}

sub is_RsubsetL {
    my $argref = _alt_construct_tester_4(@_);
    return _is_RsubsetL_engine(_argument_checker_4($argref));
}

sub _is_RsubsetL_engine {
    my $testedref = pop(@_);
    my $xsubsetref = _subset_engine(@_);
    return ${$xsubsetref}[${$testedref}[1]][${$testedref}[0]];
}

sub _subset_engine {
    my $seenrefsref = _calc_seen1(@_);
    my $xsubsetref = _subset_subengine($seenrefsref);
    return $xsubsetref;
}

sub is_LequivalentR {
    my $argref = _alt_construct_tester_4(@_);
    return _is_LequivalentR_engine(_argument_checker_4($argref));
}

*is_LeqvlntR = \&is_LequivalentR;

sub _is_LequivalentR_engine {
    my $testedref = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my $xequivalentref = _equivalent_subengine($seenrefsref);
    return ${$xequivalentref}[${$testedref}[1]][${$testedref}[0]];
}

sub is_LdisjointR {
    my $argref = _alt_construct_tester_4(@_);
    return _is_LdisjointR_engine(_argument_checker_4($argref));
}

sub _is_LdisjointR_engine {
    my $testedref = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my $xintersectionref = _calculate_xintersection_only($seenrefsref);
    my (@xdisjoint);
    for (my $i = 0; $i <= @{$seenrefsref}; $i++) {
        foreach (keys %{$xintersectionref}) {
            my ($left, $right) = split /_/, $_;
            $xdisjoint[$left][$right] = $xdisjoint[$right][$left] = 
                ! scalar(keys %{${$xintersectionref}{$_}}) ? 1 : 0;
        }
        $xdisjoint[$i][$i] = 0; 
    }
    my $disjoint_status = $xdisjoint[${$testedref}[1]][${$testedref}[0]];
    return $disjoint_status;
}

sub print_subset_chart {
    my $argref = _alt_construct_tester_5(@_);
    _print_subset_chart_engine(_argument_checker($argref));
}

sub _print_subset_chart_engine {
    my $seenrefsref = _calc_seen1(@_);
    my $xsubsetref = _subset_subengine($seenrefsref);
    my $title = 'Subset';
    _chart_engine_multiple($xsubsetref, $title);
}

sub print_equivalence_chart {
    my $argref = _alt_construct_tester_5(@_);
    _print_equivalence_chart_engine(_argument_checker($argref));
}

sub _print_equivalence_chart_engine {
    my $seenrefsref = _calc_seen1(@_);
    my $xequivalentref = _equivalent_subengine($seenrefsref);
    my $title = 'Equivalence';
    _chart_engine_multiple($xequivalentref, $title);
}    

sub is_member_which {
    return @{ is_member_which_ref(@_) };
}    

sub is_member_which_ref {
    my $argref = _alt_construct_tester_1(@_);
    return _is_member_which_engine(_argument_checker_1($argref));
}    

sub _is_member_which_engine {
    my $arg = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my $seenref = _calculate_seen_only($seenrefsref);
    my (@found);
    foreach (sort keys %{$seenref}) {
        push @found, $_ if (exists ${$seenref}{$_}{$arg});
    }
    return \@found;
}

sub is_member_any {
    my $argref = _alt_construct_tester_1(@_);
    return _is_member_any_engine(_argument_checker_1($argref));
}    

sub _is_member_any_engine {
    my $tested = pop(@_);
    my $seenrefsref = _calc_seen1(@_);
    my $seenref = _calculate_seen_only($seenrefsref);
    my ($k);
    while ( $k = each %{$seenref} ) {
        return 1 if (defined ${$seenref}{$k}{$tested});
    }
    return 0;
}

sub are_members_which {
    my $argref = _alt_construct_tester_2(@_);
    return _are_members_which_engine(_argument_checker_2($argref));
}

sub _are_members_which_engine {
    my $testedref = pop(@_);
    my @tested = @{$testedref};
    my $seenrefsref = _calc_seen1(@_);
    my $seenref = _calculate_seen_only($seenrefsref);
    my (%found);
    for (my $i=0; $i<=$#tested; $i++) {
        my (@not_found);
        foreach (sort keys %{$seenref}) {
            exists ${${$seenref}{$_}}{$tested[$i]}
                ? push @{$found{$tested[$i]}}, $_
                : push @not_found, $_;
        }
        $found{$tested[$i]} = [] if (@not_found == keys %{$seenref});
    }
    return \%found;
}

sub are_members_any {
    my $argref = _alt_construct_tester_2(@_);
    return _are_members_any_engine(_argument_checker_2($argref));
}    

sub _are_members_any_engine {
    my $testedref = pop(@_);
    my @tested = @{$testedref};
    my $seenrefsref = _calc_seen1(@_);
    my $seenref = _calculate_seen_only($seenrefsref);
    my (%present);
    for (my $i=0; $i<=$#tested; $i++) {
        foreach (keys %{$seenref}) {
            unless (defined $present{$tested[$i]}) {
                $present{$tested[$i]} = 1 if ${$seenref}{$_}{$tested[$i]};
            }
        }
        $present{$tested[$i]} = 0 if (! defined $present{$tested[$i]});
    }
    return \%present;
}

sub get_bag {
    return @{ get_bag_ref(@_) };
}

sub get_bag_ref {
    my ($argref, $unsorted) = _alt_construct_tester(@_);
    $unsorted  
        ? return          _bag_engine(_argument_checker($argref))
        : return [ sort @{_bag_engine(_argument_checker($argref))} ];
}

sub _bag_engine {
    my @listrefs = @_;
    my (@bag);
    if (ref($listrefs[0]) eq 'ARRAY') { 
        foreach my $lref (@listrefs) {
            foreach my $el (@{$lref}) {
                push(@bag, $el);
            }
        }
    } else {
        foreach my $lref (@listrefs) {
            foreach my $key (keys %{$lref}) {
                for (my $j=1; $j <= ${$lref}{$key}; $j++) {
                    push(@bag, $key);
                }
            }
        }
    }
    return \@bag;
}

sub get_version {
    return $List::Compare::Functional::VERSION;
}

1;

__END__