]> gitweb.factorcode.org Git - factor.git/commitdiff
math.combinatorics: speed up selections word.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 17 Jan 2018 18:36:12 +0000 (10:36 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 17 Jan 2018 18:36:12 +0000 (10:36 -0800)
basis/math/combinatorics/combinatorics.factor

index db2645047e4928c96a58f3a645ccb27cb9948540..df3025ff7aa7afa8517a42ef2a2a5bc639ad8271 100644 (file)
@@ -3,8 +3,8 @@
 
 USING: accessors arrays assocs binary-search classes.tuple
 combinators fry hints kernel kernel.private locals math
-math.order math.ranges namespaces sequences sequences.private
-sorting strings vectors ;
+math.functions math.order math.ranges namespaces sequences
+sequences.private sorting strings vectors ;
 IN: math.combinatorics
 
 <PRIVATE
@@ -251,10 +251,20 @@ PRIVATE>
 
 <PRIVATE
 
-: (selections) ( seq n -- selections )
-    [ dup [ 1sequence ] curry { } map-as dup ] [ 1 - ] bi* [
-        cartesian-product concat [ concat ] map
-    ] with times ;
+:: next-selection ( seq n -- )
+    1 seq length 1 - [
+        over 0 =
+    ] [
+        [ seq [ + n /mod ] change-nth-unsafe ] keep
+    ] do until 2drop ; inline
+
+:: (selections) ( seq n -- selections )
+    seq length :> len
+    n 0 <array> :> idx
+    len n ^ [
+        idx seq nths-unsafe
+        idx len next-selection
+    ] replicate ;
 
 PRIVATE>