Skip to content

Commit

Permalink
Re-implement Combinations iter's .[count|bool]-only
Browse files Browse the repository at this point in the history
Fixes #1330
  • Loading branch information
zoffixznet committed Dec 22, 2017
1 parent 1f99aa1 commit 5eed2b0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
28 changes: 16 additions & 12 deletions src/core/Rakudo/Iterator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ class Rakudo::Iterator {
$n < 1 || $n < $k || $k < 0, # must be HLL comparisons
Rakudo::Iterator.Empty, # nothing to return
class :: does Iterator {
has int $!pulled-count = 0;
has int $!n;
has int $!k;
has int $!b;
Expand Down Expand Up @@ -759,24 +760,27 @@ class Rakudo::Iterator {
),
nqp::if(
nqp::iseq_i($index,$k),
nqp::if(
$!b,
nqp::clone($!combination),
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',
nqp::clone($!combination)
nqp::stmts(
($!pulled-count = nqp::add_i($!pulled-count,1)),
nqp::if(
$!b,
nqp::clone($!combination),
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',
nqp::clone($!combination)
)
)
),
IterationEnd
)
)
}
# XXX TODO: both methods need to account for the number of
# items that were already pulled
# method count-only {
# ([*] ($!n ... 0) Z/ 1 .. min($!n - $!k, $!k)).Int
# }
# method bool-only(--> True) { }

method count-only(--> Int) {
(([*] ($!n ... 0) Z/ 1 .. min($!n - $!k, $!k)).Int)
- $!pulled-count
}
method bool-only(--> Bool) { nqp::p6bool(self.count-only) }
}.new($n,$k,$b)
)
)
Expand Down
8 changes: 8 additions & 0 deletions t/08-performance/01-iterators.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use Test;

plan 2;

is-deeply +combinations(100, 70), 29372339821610944823963760,
'combinations() iterator implements efficient .count-only';
is-deeply ?+combinations(100, 70), True,
'combinations() iterator implements efficient .bool-only';
2 changes: 1 addition & 1 deletion tools/build/Makefile-JVM.in
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ j-test : j-coretest
j-fulltest: j-coretest j-stresstest

j-coretest: j-all
$(J_HARNESS5) t/01-sanity t/02-rakudo t/03-jvm t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text
$(J_HARNESS5) t/01-sanity t/02-rakudo t/03-jvm t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text t/08-performance

# Run the spectests that we know work.
j-spectest: j-testable t/spectest.data
Expand Down
4 changes: 2 additions & 2 deletions tools/build/Makefile-Moar.in
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ m-quicktest: m-quicktest$(HARNESS_TYPE)
m-stresstest: m-stresstest$(HARNESS_TYPE)

m-coretest5: m-all
$(M_HARNESS5) t/01-sanity t/02-rakudo t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text
$(M_HARNESS5) t/01-sanity t/02-rakudo t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text t/08-performance

# Run the spectests that we know work.
m-spectest5: m-testable t/spectest.data
Expand All @@ -248,7 +248,7 @@ m-stresstest5: m-testable t/spectest.data


m-coretest6: m-all
$(M_HARNESS6) t/01-sanity t/02-rakudo t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text
$(M_HARNESS6) t/01-sanity t/02-rakudo t/04-nativecall t/05-messages t/06-telemetry t/07-pod-to-text t/08-performance

# Run the spectests that we know work.
m-spectest6: m-testable t/spectest.data
Expand Down

0 comments on commit 5eed2b0

Please sign in to comment.