Skip to content

Commit

Permalink
Specialize Range iteration for Numeric to avoid polymorphic cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Dec 2, 2012
1 parent 2ad9ba8 commit 71a8938
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/CORE.setting
Expand Up @@ -2917,6 +2917,20 @@ my class Grammar is Cursor {
sub WHAT(\x) { x.WHAT } sub WHAT(\x) { x.WHAT }
sub HOW(\x) { x.HOW } sub HOW(\x) { x.HOW }
constant Inf = 1 / 0; constant Inf = 1 / 0;
my class NumericRangeIter is IterCursor {
has $.current;
has $.limit;
has $.exclusive;
method reify() {
my $c ::= $!current;
my \l = $!limit;
$c < l ?? ($c, NumericRangeIter.new(current => $c.succ, limit => l, exclusive => $!exclusive)) !!
($c > l || $!exclusive) ?? () !! ($c,);
}
}
my class RangeIter is IterCursor { my class RangeIter is IterCursor {
has $.current; has $.current;
has $.limit; has $.limit;
Expand Down Expand Up @@ -2960,8 +2974,11 @@ my class Range is Cool does Positional {
method minmax($cmp = &infix:<cmp>) { self.iterator.list.minmax($cmp) } method minmax($cmp = &infix:<cmp>) { self.iterator.list.minmax($cmp) }
method iterator() { method iterator() {
&infix:<,>(RangeIter.new(:current($!excludes_min ?? $!min.succ !! $!min), my \mn = $!min;
:limit($!max), :exclusive($!excludes_max))).iterator my \mx = $!max;
&infix:<,>((((mn ~~ Numeric) && (mx ~~ Numeric)) ?? NumericRangeIter !! RangeIter)\
.new(:current($!excludes_min ?? mn.succ !! mn),
:limit(mx), :exclusive($!excludes_max))).iterator
} }
method Str() { self.list.Str } method Str() { self.list.Str }
Expand Down

0 comments on commit 71a8938

Please sign in to comment.