On a mailing list, somebody needed help with creating the intersection of several lists -- that is, creating a list of elements that are in all given lists. I came up with the following code. What further optimizations would be possible?

Note that I'm not looking for perl speed tricks, but rather algorithmic enhancements. I used the subroutines to make the main algorithm more readable; also, the implementation is not very perlish since that guy wanted to use it for a C++ library.

use strict; use warnings; my @sets = ([ 0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, ], [ 0, 3, 7, 8, 10, 11, 13, 14, 16, ], [ 0, 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 16, ], [ 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 16, ], [ 0, 1, 2, 3, 7, 8, 9, 10, 11, 13, 14, 15, ]); my $numSets = scalar @sets; my @intersection = (); my $currentSet = 0; my @marker = (0, 0, 0, 0, 0); my $currentItem; # helper functions sub stepMarker { $marker[$currentSet]++; if ($marker[$currentSet] >= scalar @{$sets[$currentSet]}) { # we are at the end of the current set # => abort, there cannot be any more common items last MAIN; } } sub nextSet { $currentSet++; $currentSet %= $numSets; } sub getCurrentItem { return $sets[$currentSet]->[$marker[$currentSet]]; } # main intersection algorithm my $activeItem = getCurrentItem; my $isPresent = 1; MAIN: while (1) { stepMarker(); nextSet; while (($currentItem = getCurrentItem()) < $activeItem) { stepMarker(); } if ($currentItem > $activeItem) { $activeItem = $currentItem; $isPresent = 1; } else { $isPresent++; if ($isPresent == $numSets) { # item has been found present in all sets push @intersection, $activeItem; stepMarker(); $activeItem = getCurrentItem; $isPresent = 1; } } } print "Common items in the $numSets sets: @intersection\n";

In reply to Algorithm Golfing: Intersection of lists by crenz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.