in reply to better union of sets algorithm?
Update: This assumes you can get the lists sorted for free, or at least for cheaper than the n lg n it would take to process the hashes. If that's the case, here's some simple code that demonstrates the idea, using an object to make the idea clearer:
#!/usr/bin/perl use warnings; use strict; my @a1 = sort qw(foo bar blarn schmark floogle foo blarn); my @a2 = sort qw(flirp schnirp blarn floogle florn flimple flange); my @a3 = sort qw(bar bar floogle bar florn bar bar); my $sml = SortedMultiList->new(\@a1,\@a2,\@a3); my $last; while (defined(my $next = $sml->shift_lowest)) { print $next,"\n" if (!defined($last) or ($last ne $next)); $last = $next; } package SortedMultiList; sub new { my $class = shift; my $self = [@_]; bless $self, $class; } sub shift_lowest { my $self = shift; my($lowest_arr,$lowest); foreach my $a (@$self) { next unless (@$a); if (!defined($lowest) or ($a->[0] lt $lowest)) { $lowest_arr = $a; $lowest = $a->[0]; } } return $lowest_arr ? shift(@$lowest_arr) : undef; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: better union of sets algorithm?
by blokhead (Monsignor) on Mar 11, 2005 at 06:18 UTC | |
by sgifford (Prior) on Mar 11, 2005 at 06:50 UTC | |
by demerphq (Chancellor) on Mar 11, 2005 at 08:25 UTC | |
by Anonymous Monk on Mar 11, 2005 at 10:17 UTC | |
by demerphq (Chancellor) on Mar 11, 2005 at 10:30 UTC | |
by blokhead (Monsignor) on Mar 11, 2005 at 07:22 UTC | |
by blazar (Canon) on Mar 11, 2005 at 09:09 UTC | |
by Anonymous Monk on Mar 11, 2005 at 10:49 UTC | |
|
Re^2: better union of sets algorithm?
by Anonymous Monk on Mar 11, 2005 at 10:14 UTC |