...clipped... (not applicable) #I was computing the intersection and not the union.
my @unique = do{ my %seen; undef @seen{@list1, @list2, @list3}; sort keys %seen;};
package ListMerger; use strict; sub new { my $pkg = shift; my $self; return bless \$self, $pkg; } #call with references to lists as arguments #ex: @union = $obj->union(\@list1,\@list2,\@list3); sub union { my $self = shift; my @lists = map{@{$_}}@_; return do{ my %seen; undef @seen{@lists}; sort keys %seen; }; } #call with references to lists as arguments sub intersection { my $self = shift; my @lists = map{@{$_}}@_; my $list_count = scalar(@_); return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_}<$list_count)}keys %seen; sort keys %seen; }; } #call with references to lists as arguments sub exclusion { my $self = shift; my @lists = map{@{$_}}@_; my $list_count = scalar(@_); return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_} == $list_count)}keys %se +en; sort keys %seen; }; } #call with references to list as arguments sub shared { my $self = shift; my @lists = map{@{$_}}@_; return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_} == 1)}keys %seen; sort keys %seen; }; } 1;
In reply to Re: better union of sets algorithm?
by injunjoel
in thread better union of sets algorithm?
by perrin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |