Guest User

Untitled

a guest
Oct 31st, 2013
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.39 KB | None | 0 0
  1. sub find_connections {
  2.  
  3.     my ($this, $degree, $is_course) = @_;
  4.     my ($type, $type_id);
  5.  
  6.     if ($is_course) {
  7.         $sql = "SELECT * FROM course WHERE course_id = '$this'";
  8.         $type = "course";
  9.         $type_id = 0;
  10.     }
  11.     else {
  12.         $already_expanded[@already_expanded] = $this;
  13.         $sql = "SELECT * FROM friend WHERE user1 = '$this' LIMIT 0, 200";
  14.         $type = "friend";
  15.         $type_id = 1;
  16.     }
  17.  
  18.     my $return = $db->query($sql);
  19.  
  20.     while (my %row = $return->fetchhash()) {
  21.  
  22.     my $new_id;
  23.     if (!$is_course) {
  24.         # get the new id from the friend pair
  25.         #if ($row{user1} eq $this) {
  26.         #  $new_id = $row{user2};
  27.         #} else {
  28.         #  $new_id = $row{user1};
  29.         #}
  30.         $new_id = $row{user2};
  31.     }
  32.     else {
  33.         $new_id = $row{id};
  34.     }
  35.  
  36.     next unless ($degree > $num_degrees or $cs{$new_id} or can_see($user, $new_id, $type));
  37.  
  38.     # see if the id is already a node; if not, add it
  39.     my $node_exists = 0;
  40.     for (my $i = 0; $i < @node and !$node_exists; $i++) {
  41.  
  42.         check_connection_row("#...pass vars by reference...");
  43.  
  44.     }
  45.  
  46. }
  47.  
  48. sub check_connection_row {
  49.  
  50.     $node_exists = 1 if ($node[$i]{id} eq $new_id and $node[$i]{type} eq 1);
  51.  
  52.     if (!$node_exists and $degree <= $num_degrees) {
  53.         my $next_node = @node;
  54.         $node[$next_node]{id} = $new_id;
  55.         $node[$next_node]{type} = 1;
  56.     }
  57.  
  58.     # see if there's an edge between the two already; if not, add it
  59.     if ($node_exists or $degree <= $num_degrees) {
  60.    
  61.         my $edge_exists = 0;
  62.         my $this_index = find_node ($this, $type_id);
  63.         my $new_index = find_node ($new_id, 1);
  64.  
  65.         for (my $i = 0; $i < @edge and !$edge_exists; $i++) {
  66.        
  67.             my $cond1 = $edge[$i][0] eq $this_index and $edge[$i][1] eq $new_index; # would use meaninful condition name if I knew the subject
  68.             my $cond2 = $edge[$i][0] eq $new_index and $edge[$i][1] eq $this_index; # would use meaninful condition name if I knew the subject
  69.            
  70.             $edge_exists = 1 if ($cond1 or $cond2);
  71.  
  72.         }
  73.  
  74.         unless ($edge_exists) {
  75.             my $next_edge = @edge;
  76.             $edge[$next_edge][0] = $this_index;
  77.             $edge[$next_edge][1] = $new_index;
  78.         }
  79.  
  80.     }
  81.  
  82.     return unless ($degree < $num_degrees + 1);
  83.  
  84.     # if we want to consider friends farther out, do so now
  85.     my $expansion_exists = 0;
  86.  
  87.     for (my $i = 0; $i < @already_expanded and !$expansion_exists; $i++) {
  88.         $expansion_exists = 1 if ($already_expanded[$i] eq $new_id);
  89.     }
  90.  
  91.     find_connections ($new_id, $degree + 1, 0) if (!$expansion_exists && !$is_course or can_see ($user, $new_id, "friend"));
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment