Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.01 KB | None | 0 0
  1. sub compare_components {
  2.     my $self = shift;
  3.     my $compA = shift;
  4.     my $compB = shift;
  5.     my $result = 0;
  6.  
  7.     # when $compA > $compB, $result > 0
  8.     # when $compA > $compB, $result < 0
  9.     # when $compA == $compB, $result == 0
  10.    
  11.     # test if $compA depends on $compB
  12.     my $id = $compB->id;
  13.     foreach my $dep ($compA->dependencies) {
  14.         if($dep eq $id) {
  15.             # depends, so superior
  16.             $result++;
  17.         }
  18.     }
  19.  
  20.     # test if $compB depends on $compA
  21.     $id = $compA->id;
  22.     foreach my $dep ($compB->dependencies) {
  23.         if($dep eq $id) {
  24.             # depends, so superior
  25.             # if we fall on a circular dependency, $result will be 0
  26.             $result--;
  27.         }
  28.     }
  29.  
  30.     # if not guessed from dependencies, use index
  31.     if($result == 0) {
  32.         if($compA->index > $compB->index) {
  33.             $result++;
  34.         } elsif($compB->index > $compA->index) {
  35.             $result --;
  36.         }
  37.     }
  38.  
  39.     return $result;
  40. }
  41.  
  42. sub organize_components {
  43.     # There must be a better way to do that
  44.     my $self = shift;
  45.     my $comp_list = shift;
  46.     my $start = 0;
  47.     if(@_) {
  48.         $start = shift;
  49.     }
  50.     my $end = (@{$comp_list} - 1);
  51.     if(@_) {
  52.         $end = shift;
  53.     }
  54.     my $left = $start;
  55.     my $right = $end;
  56.     my $tested = $comp_list->[$start];
  57.  
  58.     if($start >= $end) {
  59.         return 1;
  60.     }
  61.  
  62.     while(1) {
  63.         while($self->compare_components($comp_list->[$right], $tested) > 0) {
  64.             $right--;
  65.         }
  66.         while($self->compare_components($comp_list->[$left], $tested) < 0) {
  67.             $left++;
  68.         }
  69.  
  70.         if($left < $right) {
  71.             my $temp = $comp_list->[$left];
  72.             $comp_list->[$left] = $comp_list->[$right];
  73.             $comp_list->[$right] = $temp;
  74.         } else {
  75.             last;
  76.         }
  77.     }
  78.  
  79.     $self->organize_components($comp_list, $start, $right);
  80.     $self->organize_components($comp_list, ($right + 1), $end);
  81.    
  82.     return 1;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement