Skip to content
This repository
Browse code

Specialize Range iteration for Numeric to avoid polymorphic cmp

  • Loading branch information...
commit 71a89388c954e385339156e02559e55664c28556 1 parent 2ad9ba8
Stefan O'Rear authored

Showing 1 changed file with 19 additions and 2 deletions. Show diff stats Hide diff stats

  1. +19 2 lib/CORE.setting
21 lib/CORE.setting
@@ -2917,6 +2917,20 @@ my class Grammar is Cursor {
2917 2917 sub WHAT(\x) { x.WHAT }
2918 2918 sub HOW(\x) { x.HOW }
2919 2919 constant Inf = 1 / 0;
  2920 +my class NumericRangeIter is IterCursor {
  2921 + has $.current;
  2922 + has $.limit;
  2923 + has $.exclusive;
  2924 +
  2925 + method reify() {
  2926 + my $c ::= $!current;
  2927 + my \l = $!limit;
  2928 +
  2929 + $c < l ?? ($c, NumericRangeIter.new(current => $c.succ, limit => l, exclusive => $!exclusive)) !!
  2930 + ($c > l || $!exclusive) ?? () !! ($c,);
  2931 + }
  2932 +}
  2933 +
2920 2934 my class RangeIter is IterCursor {
2921 2935 has $.current;
2922 2936 has $.limit;
@@ -2960,8 +2974,11 @@ my class Range is Cool does Positional {
2960 2974 method minmax($cmp = &infix:<cmp>) { self.iterator.list.minmax($cmp) }
2961 2975
2962 2976 method iterator() {
2963   - &infix:<,>(RangeIter.new(:current($!excludes_min ?? $!min.succ !! $!min),
2964   - :limit($!max), :exclusive($!excludes_max))).iterator
  2977 + my \mn = $!min;
  2978 + my \mx = $!max;
  2979 + &infix:<,>((((mn ~~ Numeric) && (mx ~~ Numeric)) ?? NumericRangeIter !! RangeIter)\
  2980 + .new(:current($!excludes_min ?? mn.succ !! mn),
  2981 + :limit(mx), :exclusive($!excludes_max))).iterator
2965 2982 }
2966 2983
2967 2984 method Str() { self.list.Str }

0 comments on commit 71a8938

Please sign in to comment.
Something went wrong with that request. Please try again.