Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized Range for getting its size if its a numeric range #2330

Closed
p6rt opened this issue Jan 16, 2011 · 8 comments
Closed

optimized Range for getting its size if its a numeric range #2330

p6rt opened this issue Jan 16, 2011 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 16, 2011

Migrated from rt.perl.org#82312 (status was 'resolved')

Searchable as RT82312$

@p6rt
Copy link
Author

p6rt commented Jan 16, 2011

From payload@lavabit.com

fixes the problem which you encounter when you try to evaluate
+(23..2300000000)

~ payload

@p6rt
Copy link
Author

p6rt commented Jan 16, 2011

From payload@lavabit.com

0001-optimized-Range-for-getting-its-size-if-its-a-numeri.patch
From b5d95340a193df9af8ccb185c33ef4177a7ab527 Mon Sep 17 00:00:00 2001
From: payload <payload@lavabit.com>
Date: Sun, 16 Jan 2011 19:33:04 +0100
Subject: [PATCH] optimized Range for getting its size if its a numeric range

---
 src/core/Range.pm |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/core/Range.pm b/src/core/Range.pm
index fc1395b..8a07b53 100644
--- a/src/core/Range.pm
+++ b/src/core/Range.pm
@@ -103,6 +103,16 @@ class Range is Iterable does Positional {
     multi method roll(Whatever) {
         self.roll(Inf);
     }
+    
+    # to optimize the calculation of the size of a big range
+    # +(42..$big) doesnt take too much time now
+    multi method Numeric () {
+        nextsame unless $.max ~~ Numeric and $.min ~~ Numeric;
+        my $lo := $.min + $.excludes_min;
+        my $hi := $.max - $.excludes_max;
+        return 0 if $hi < $lo;
+        return ($hi - $lo + 1).floor;
+    }
 }
 
 
-- 
1.7.1

@p6rt
Copy link
Author

p6rt commented Jan 17, 2011

From @moritz

On 01/16/2011 07​:47 PM, Gilbert R. Roehrbein (via RT) wrote​:

fixes the problem which you encounter when you try to evaluate
+(23..2300000000)

... and creates others. Consider

(0..^3.3).Numeric

Where you patch makes it return 3, but 4 is the correct answer.

Maybe checking for ~~ Int instead of ~~ Numeric helps, but we also need
to more tests.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Jan 17, 2011

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jan 19, 2011

From payload@lavabit.com

On Mo, 2011-01-17 at 13​:34 -0800, Moritz Lenz via RT wrote​:

On 01/16/2011 07​:47 PM, Gilbert R. Roehrbein (via RT) wrote​:

fixes the problem which you encounter when you try to evaluate
+(23..2300000000)

... and creates others. Consider

(0..^3.3).Numeric

Where you patch makes it return 3, but 4 is the correct answer.

shame :( but fixed it ^^

Maybe checking for ~~ Int instead of ~~ Numeric helps, but we also need
to more tests.

does now work for following ranges

for $(^3.3), $(0..3), $(-2..0), $(-2.9..3.1), $(1.9..3.1) {
  my $a := +$_;
  my $b := .elems;
  say "{.perl}\t\t$a != $b" if $a != $b;
}

@p6rt
Copy link
Author

p6rt commented Jan 19, 2011

From payload@lavabit.com

another-fix.patch
diff --git a/src/core/Range.pm b/src/core/Range.pm
index fc1395b..baffe3f 100644
--- a/src/core/Range.pm
+++ b/src/core/Range.pm
@@ -103,6 +103,16 @@ class Range is Iterable does Positional {
     multi method roll(Whatever) {
         self.roll(Inf);
     }
+    
+    # to optimize the calculation of the size of a big range
+    # +(42..$big) doesnt take too much time now
+    multi method Numeric () {
+        nextsame unless $.max ~~ Numeric and $.min ~~ Numeric;
+        my $lo := $.min + $.excludes_min;
+        return 0 if $.max < $lo;
+        my $ret := ($.max - $lo + 1).floor;
+        return $ret - ($.excludes_max and $.max == $lo + $ret);
+    }
 }
 
 

@p6rt
Copy link
Author

p6rt commented Mar 29, 2013

From @coke

On Wed Jan 19 01​:29​:06 2011, payload wrote​:

On Mo, 2011-01-17 at 13​:34 -0800, Moritz Lenz via RT wrote​:

On 01/16/2011 07​:47 PM, Gilbert R. Roehrbein (via RT) wrote​:

fixes the problem which you encounter when you try to evaluate
+(23..2300000000)

... and creates others. Consider

(0..^3.3).Numeric

Where you patch makes it return 3, but 4 is the correct answer.

shame :( but fixed it ^^

Maybe checking for ~~ Int instead of ~~ Numeric helps, but we also need
to more tests.

does now work for following ranges

for $(^3.3), $(0..3), $(-2..0), $(-2.9..3.1), $(1.9..3.1) {
my $a := +$_;
my $b := .elems;
say "{.perl}\t\t$a != $b" if $a != $b;
}

Sorry about the delay​:

Added tests to S02-types/range.t, and applied a heavily modified version of the patch (there
were some edge cases that were still failing with the patch as is.)

$ time ./perl6 -e 'say +(123..123121231231231212321)'
123121231231231212199

real 0m0.475s
user 0m0.369s
sys 0m0.101s

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Mar 29, 2013

@coke - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Mar 29, 2013
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant