Skip to content

Commit

Permalink
simplify some code in Hash.pick
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Jul 14, 2010
1 parent a8deb9a commit 22f2fc2
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/core/Hash.pm
Expand Up @@ -101,15 +101,15 @@ role Hash is EnumMap {
my $value = @weights[*-1].rand;
return self.keys[0] if @weights[0] > $value;
my ($l, $r) = (0, @weights.elems-1);
my $middle = $l + floor( ($r-$l)/2);
while ($middle > $l) {
my $middle = floor ($r + $l) / 2;
while $middle > $l {
if @weights[$middle] < $value {
$l = $middle;
}
else {
$r = $middle;
}
$middle = $l + floor( ($r-$l)/2);
$middle = floor ($r + $l) / 2;
}
self.keys[$r];
}
Expand All @@ -120,32 +120,28 @@ role Hash is EnumMap {
my $value = @weights[*-1].rand;
return self.keys[0] if @weights[0] > $value;
my ($l, $r) = (0, @weights.elems-1);
my $middle = $l + floor( ($r-$l)/2);
while ($middle > $l) {
my $middle = floor ($r + $l) / 2;
while $middle > $l {
if @weights[$middle] < $value {
$l = $middle;
}
else {
$r = $middle;
}
$middle = $l + floor( ($r-$l)/2);
$middle = floor ($r + $l) / 2;
}
return self.keys[$r];
}

if $replace {
gather {
while $num > 0 {
take self.pick();
$num--;
}
take self.pick() for ^$num;
}
} else {
my %copyHash = @.pairs.grep({ .value != 0});
gather {
while $num > 0 && %copyHash {
my $picked;
take $picked = %copyHash.pick();
take my $picked = %copyHash.pick();
unless --%copyHash{$picked} {
%copyHash.delete($picked);
}
Expand Down

0 comments on commit 22f2fc2

Please sign in to comment.