Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

FYI: you can use (0..$#cluster) rather than hardcoding magic numbers, and push onto the array to simplify your initialization (see below).

use strict; use warnings;
Are important to include too. Example (with array names pluralized for clarity):

use strict; use warnings; my @sets = ( [0], [0, 1], [0, 1, 2], [1, 2, 3], [5, 6, 4], ); my @clusters; foreach my $set (@sets) { push @clusters, Set::Scalar->new->insert(@$set); } for my $i (0.. $#clusters) { ...

Definitely put some debug prints into your loop so you can see if what it is actually doing is what you expect it to be doing at each step.

PS:
If this is going to expand to large sets of clusters, it would probably be a good idea to modify your algorithm such that it doesn't have to check the cleared/empty sets over and over. For large clusters, you would want an intersection test that short-circuits after the first match to return just a true/false result too.

I'd suggest having a list of superclusters, and adding one cluster at a time. If the new cluster has no intersections with anything, add it to the list as a new supercluster. If it has an intersection with something, add it to that supercluster and keep searching. For every intersection after the first, add the intersecting supercluster to the originally matched supercluster, and then completely remove the new match (take care about the list shrinking by 1 when you do that).

That way, your debug output could show something like:

0 superclusters: () : adding (0)... 0 intersections. 1 supercluster: ((0)) : adding (1,2,3)... 0 intersections. 2 superclusters: ((0), (1,2,3)) : adding (0,1)... 2 intersections. 1 supercluster: ((0,1,2,3)) : adding (0,1,2)... 1 intersection.


In reply to Re: How can sets be recursively concatenated when intersecting with each other by SuicideJunkie
in thread How can sets be recursively concatenated when intersecting with each other by joni

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-19 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found