Skip to content

Commit

Permalink
More range improvements. All spectests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Aug 15, 2010
1 parent e8f2c6b commit 3f202d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/core/Cool-str.pm
Expand Up @@ -429,6 +429,8 @@ our Str proto sub infix:<x>($str, $n) {
}

our multi sub infix:<cmp>($a, $b) {
return Order::Increase if $a eqv -Inf || $b eqv Inf;
return Order::Decrease if $a eqv Inf || $b eqv -Inf;
$a lt $b ?? Order::Increase !! ($a gt $b ?? Order::Decrease !! Order::Same);
}

Expand Down
26 changes: 11 additions & 15 deletions src/core/Range.pm
Expand Up @@ -10,34 +10,30 @@ class Range is Iterable does Positional {
self.bless(*, :$min, :$max, :$excludes_min, :$excludes_max);
}

multi method new(Real $min, $max,
multi method new($min, Whatever $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.bless(*, :$min, :max($max.Numeric), :$excludes_min, :$excludes_max);
}

multi method new($min, Real $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.bless(*, :min($min.Numeric), :$max, :$excludes_min, :$excludes_max);
self.bless(*, :$min, :max(+Inf), :$excludes_min, :$excludes_max);
}

multi method new(Whatever $min, $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new(-Inf, $max, :$excludes_min, :$excludes_max);
self.bless(*, :min(-Inf), :$max, :$excludes_min, :$excludes_max);
}

multi method new($min, Whatever $max,
multi method new(Whatever $min, Whatever $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new($min, +Inf, :$excludes_min, :$excludes_max);
fail "*..* is not a valid range";
}

multi method new(Whatever $min, Whatever $max,
multi method new($min, $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new(-Inf, +Inf, :$excludes_min, :$excludes_max);
($min ~~ Real or $max ~~ Real)
?? self.bless(*, :min($min.Numeric), :max($max.Numeric), :$excludes_min, :$excludes_max)
!! nextsame;
}

multi method bounds() { ($.min, $.max) }
Expand Down Expand Up @@ -72,11 +68,11 @@ class Range is Iterable does Positional {
multi method postcircumfix:<[ ]>(\$parcel) { self.Seq[$parcel]; }

my Bool multi method !max_test($topic) {
$topic before $.max || (!$.excludes_max && !($topic after $.max));
$topic before $.max || $.max eqv Inf || (!$.excludes_max && !($topic after $.max));
}

my Bool multi method !min_test($topic) {
$.min before $topic || (!$.excludes_min && !($.min after $topic));
$.min before $topic || $.min eqv -Inf || (!$.excludes_min && !($.min after $topic));
}

}
Expand Down
11 changes: 6 additions & 5 deletions src/core/RangeIter.pm
Expand Up @@ -23,11 +23,12 @@ class RangeIter is Iterator {
.local int count
count = 8
reify_loop:
$P0 = '&infix:<after>'(value, max)
if $P0 goto reify_done
unless excl_max goto reify_value
$P0 = '&infix:<eqv>'(value, max)
if $P0 goto reify_done
$I0 = '&infix:<cmp>'(value, max)
if $I0 < 0 goto reify_value
if $I0 > 0 goto reify_done
if excl_max goto reify_done
push reify, value
goto reify_done
reify_value:
push reify, value
value = value.'succ'()
Expand Down

0 comments on commit 3f202d5

Please sign in to comment.