[Bio] / FigKernelPackages / set_utilities.pm Repository:
ViewVC logotype

View of /FigKernelPackages/set_utilities.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Mon Dec 5 19:06:30 2005 UTC (14 years, 2 months ago) by olson
Branch: MAIN
CVS Tags: rast_rel_2009_05_18, rast_rel_2008_06_18, rast_rel_2008_06_16, rast_rel_2008_12_18, rast_rel_2008_07_21, rast_2008_0924, rast_rel_2008_04_23, rast_rel_2008_09_30, rast_rel_2009_0925, caBIG-05Apr06-00, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, mgrast_rel_2008_0625, rast_rel_2008_10_09, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, mgrast_rel_2008_1110, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, caBIG-13Feb06-00, rast_rel_2009_03_26, rast_rel_2008_11_24, rast_rel_2008_08_07
Changes since 1.1: +17 -0 lines
Added license words.

#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
# 
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License. 
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

package set_utilities;

require Exporter;
@ISA = (Exporter);
@EXPORT = qw(
	     member
	     union
	     intersection
	     set_diff
	     unique
);

sub member {
    my($x,$set) = @_;
    my($i);

    for ($i=0; $i <= $#{$set}; $i++)
    {
	if ($set->[$i] eq $x) { return 1; }
    }
    return 0;
}

sub set_diff {
    my($s1,$s2) = @_;
    my(@set1,@set2,$p1,$p2,$diff);

    @set1 = sort @$s1;
    @set2 = sort @$s2;
#   print STDERR "set1 has $#set1+1 and set2 has $#set2+1\n";

    $p1   = 0;
    $p2   = 0;
    $diff = [];

    while (($p1 <= $#set1) && ($p2 <= $#set2))
    {
	if    ($set1[$p1] lt $set2[$p2])
	{
	    push(@$diff,$set1[$p1]); $p1++;
	}
	elsif ($set2[$p2] lt $set1[$p1])
	{
	    $p2++;
	}
	else  
	{
	    $p1++; $p2++;
	}
    }
    while ($p1 <= $#set1)
    {
	push(@$diff,$set1[$p1]); $p1++;
    }
    return $diff;
}
	
sub union {
    my($s1,$s2) = @_;
    my(@set1,@set2,$p1,$p2,$union);

    @set1 = sort @$s1;
    @set2 = sort @$s2;
    
    $p1   = 0;
    $p2   = 0;
    $union = [];

    while (($p1 <= $#set1) || ($p2 <= $#set2))
    {
	if    ($p2 > $#set2)
	{
	    push(@$union,$set1[$p1++]);
	}
	elsif ($p1 > $#set1)
	{
	    push(@$union,$set2[$p2++]);
	}
	elsif ($set1[$p1] lt $set2[$p2])
	{
	    push(@$union,$set1[$p1++]); 
	}
	elsif ($set2[$p2] lt $set1[$p1])
	{
	    push(@$union,$set2[$p2++]); 
	}
	else  
	{
	    push(@$union,$set1[$p1++]); 
	    $p2++;
	}
    }
    return $union;
}

sub intersection {
    my($s1,$s2) = @_;
    my(@set1,@set2,$p1,$p2,$intersection);

    @set1 = sort @$s1;
    @set2 = sort @$s2;
    
    $p1   = 0;
    $p2   = 0;
    $intersection = [];

    while (($p1 <= $#set1) && ($p2 <= $#set2))
    {
	if ($set1[$p1] lt $set2[$p2])
	{
	    $p1++;
	}
	elsif ($set2[$p2] lt $set1[$p1])
	{
	    $p2++;
	}
	else  
	{
	    push(@$intersection,$set1[$p1++]); 
	    $p2++;
	}
    }
    return $intersection;
}
    
sub unique {
# &unique(\@L) -> \@Lunique
    my($f)   = @_;
    my(@xL)  = sort(@$f);
    my(@ans) = ();
    my($i);
    
    for ($i=0; $i <= $#xL; $i++)
    {
	if (($i == $#xL) || ($xL[$i] ne $xL[$i+1]))
	{
	    push(@ans,$xL[$i]);
	}
    }
    return \@ans;
}


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3