Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub compare_components {
- my $self = shift;
- my $compA = shift;
- my $compB = shift;
- my $result = 0;
- # when $compA > $compB, $result > 0
- # when $compA > $compB, $result < 0
- # when $compA == $compB, $result == 0
- # test if $compA depends on $compB
- my $id = $compB->id;
- foreach my $dep ($compA->dependencies) {
- if($dep eq $id) {
- # depends, so superior
- $result++;
- }
- }
- # test if $compB depends on $compA
- $id = $compA->id;
- foreach my $dep ($compB->dependencies) {
- if($dep eq $id) {
- # depends, so superior
- # if we fall on a circular dependency, $result will be 0
- $result--;
- }
- }
- # if not guessed from dependencies, use index
- if($result == 0) {
- if($compA->index > $compB->index) {
- $result++;
- } elsif($compB->index > $compA->index) {
- $result --;
- }
- }
- return $result;
- }
- sub organize_components {
- # There must be a better way to do that
- my $self = shift;
- my $comp_list = shift;
- my $start = 0;
- if(@_) {
- $start = shift;
- }
- my $end = (@{$comp_list} - 1);
- if(@_) {
- $end = shift;
- }
- my $left = $start;
- my $right = $end;
- my $tested = $comp_list->[$start];
- if($start >= $end) {
- return 1;
- }
- while(1) {
- while($self->compare_components($comp_list->[$right], $tested) > 0) {
- $right--;
- }
- while($self->compare_components($comp_list->[$left], $tested) < 0) {
- $left++;
- }
- if($left < $right) {
- my $temp = $comp_list->[$left];
- $comp_list->[$left] = $comp_list->[$right];
- $comp_list->[$right] = $temp;
- } else {
- last;
- }
- }
- $self->organize_components($comp_list, $start, $right);
- $self->organize_components($comp_list, ($right + 1), $end);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement