Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor Seq.sort to resolve some arity < 2 bugs (and separate
the codepaths a bit).
  • Loading branch information
pmichaud committed Feb 23, 2010
1 parent 069df7a commit 9b33a8d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/core/Seq.pm
Expand Up @@ -9,30 +9,23 @@ augment class Seq {
# Parrot already provides a sort method that works on
# ResizablePMCArray, so we aim to make use of that here.
# Instead of sorting the elements directly, we sort an RPA
# of indices, then use that RPA as a slice into self.
# of indices (from 0 to $list.elems), then use that RPA
# as a slice into self.

# If &by.arity < 2, then it represents a block to be applied
# to the elements to obtain the values for sorting.
my ($list, &cmp);
if (&by.?arity // 2) < 2 {
$list = self.map(&by);
&cmp = pir::get_hll_global__Ps('&infix:<cmp>');
my $list = self.map(&by).eager;
self[(^pir::elements($list)).eager.sort(
-> $a, $b { $list[$a] cmp $list[$b] || $a <=> $b }
)];
}
else {
$list = self.eager;
&cmp = &by;
my $list = self.eager;
self[(^pir::elements($list)).eager.sort(
-> $a, $b { &by($list[$a],$list[$b]) || $a <=> $b }
)];
}

# Now we sort the list. We create a RPA (Parcel) list of
# indices from 0 to $list.elems - 1, sort those using
# RPA.sort and &cmp, and slice the result. (If &cmp
# indicates two elements are equal, we use the indices
# for the comparison, producing a stable sort.)
self[
(^pir::elements($list)).eager.sort(
-> $a, $b { &cmp($list[$a], $list[$b]) || $a <=> $b }
)
];
}
}

Expand Down

0 comments on commit 9b33a8d

Please sign in to comment.