| 
									
										
										
										
											2010-07-14 12:09:57 -04:00
										 |  |  | ! Copyright (c) 2007-2010 Slava Pestov, Doug Coleman, Aaron Schaefer, John Benediktsson. | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-07-26 11:12:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-15 18:09:25 -04:00
										 |  |  | USING: accessors arrays assocs binary-search classes.tuple | 
					
						
							| 
									
										
										
										
											2013-06-14 14:27:45 -04:00
										 |  |  | combinators fry hints kernel kernel.private locals math | 
					
						
							|  |  |  | math.order math.ranges memoize namespaces sequences | 
					
						
							| 
									
										
										
										
											2013-10-01 23:44:57 -04:00
										 |  |  | sequences.private sorting strings vectors ;
 | 
					
						
							| 
									
										
										
										
											2012-06-01 17:07:39 -04:00
										 |  |  | FROM: sequences => change-nth ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: math.combinatorics | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 14:27:45 -04:00
										 |  |  | ! Specialized version of nths-unsafe for performance | 
					
						
							|  |  |  | : (nths-unsafe) ( indices seq -- seq' )
 | 
					
						
							|  |  |  |     [ { array } declare ] dip
 | 
					
						
							|  |  |  |     [ [ nth-unsafe ] curry ] keep map-as ; inline
 | 
					
						
							|  |  |  | GENERIC: nths-unsafe ( indices seq -- seq' )
 | 
					
						
							|  |  |  | M: string nths-unsafe (nths-unsafe) ;
 | 
					
						
							|  |  |  | M: array nths-unsafe (nths-unsafe) ;
 | 
					
						
							| 
									
										
										
										
											2013-10-01 23:44:57 -04:00
										 |  |  | M: vector nths-unsafe (nths-unsafe) ;
 | 
					
						
							| 
									
										
										
										
											2013-06-14 14:27:45 -04:00
										 |  |  | M: iota-tuple nths-unsafe (nths-unsafe) ;
 | 
					
						
							|  |  |  | M: object nths-unsafe (nths-unsafe) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | : possible? ( n m -- ? )
 | 
					
						
							|  |  |  |     0 rot between? ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | : twiddle ( n k -- n k )
 | 
					
						
							|  |  |  |     2dup - dupd > [ dupd - ] when ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-05 14:02:38 -04:00
										 |  |  | MEMO: factorial ( n -- n! )
 | 
					
						
							| 
									
										
										
										
											2012-04-24 13:03:45 -04:00
										 |  |  |     dup 1 > [ [1,b] product ] [ drop 1 ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : nPk ( n k -- nPk )
 | 
					
						
							|  |  |  |     2dup possible? [ dupd - [a,b) product ] [ 2drop 0 ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : nCk ( n k -- nCk )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 14:43:20 -04:00
										 |  |  |     twiddle [ nPk ] keep factorial /i ;
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Factoradic-based permutation methodology | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | : factoradic ( n -- factoradic )
 | 
					
						
							| 
									
										
										
										
											2012-04-24 21:42:34 -04:00
										 |  |  |     0 [ over 0 > ] [ 1 + [ /mod ] keep swap ] produce reverse! 2nip ;
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-05 10:28:22 -04:00
										 |  |  | : bump-indices ( seq n -- )
 | 
					
						
							|  |  |  |     '[ dup _ >= [ 1 + ] when ] map! drop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (>permutation) ( seq n index -- seq )
 | 
					
						
							|  |  |  |     swap [ dupd head-slice ] dip bump-indices ;
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >permutation ( factoradic -- permutation )
 | 
					
						
							| 
									
										
										
										
											2013-04-05 10:28:22 -04:00
										 |  |  |     reverse! dup [ (>permutation) ] each-index reverse! ;
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : permutation-indices ( n seq -- permutation )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     length [ factoradic ] dip 0 pad-head >permutation ;
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-18 18:17:48 -04:00
										 |  |  | : permutation-iota ( seq -- iota )
 | 
					
						
							|  |  |  |     length factorial iota ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-18 18:31:52 -05:00
										 |  |  | : permutation ( n seq -- seq' )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 15:43:02 -04:00
										 |  |  |     [ permutation-indices ] keep nths-unsafe ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-23 11:29:20 -04:00
										 |  |  | TUPLE: permutations length seq ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <permutations> ( seq -- permutations )
 | 
					
						
							|  |  |  |     [ length factorial ] keep permutations boa ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: permutations length length>> ; inline
 | 
					
						
							|  |  |  | M: permutations nth-unsafe seq>> permutation ;
 | 
					
						
							|  |  |  | M: permutations hashcode* tuple-hashcode ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: permutations immutable-sequence | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-15 18:05:07 -04:00
										 |  |  | TUPLE: k-permutations length skip k seq ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: <k-permutations> ( seq k -- permutations )
 | 
					
						
							|  |  |  |     seq length :> n | 
					
						
							|  |  |  |     n k nPk :> len | 
					
						
							| 
									
										
										
										
											2013-05-15 18:09:25 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { [ len k [ zero? ] either? ] [ { } ] } | 
					
						
							|  |  |  |         { [ n k = ] [ seq <permutations> ] } | 
					
						
							|  |  |  |         [ len n factorial over /i k seq k-permutations boa ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2013-05-15 18:05:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: k-permutations length length>> ; inline
 | 
					
						
							|  |  |  | M: k-permutations nth-unsafe | 
					
						
							|  |  |  |     [ skip>> * ] | 
					
						
							|  |  |  |     [ seq>> [ permutation-indices ] keep ] | 
					
						
							|  |  |  |     [ k>> swap [ head ] dip nths-unsafe ] tri ;
 | 
					
						
							|  |  |  | M: k-permutations hashcode* tuple-hashcode ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: k-permutations immutable-sequence | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  | DEFER: next-permutation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:12:57 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : permutations-quot ( seq quot -- seq quot' )
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     [ [ permutation-iota ] [ length iota >array ] [ ] tri ] dip
 | 
					
						
							|  |  |  |     '[ drop _ [ _ nths-unsafe @ ] keep next-permutation drop ] ; inline
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:12:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : each-permutation ( ... seq quot: ( ... elt -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:12:57 -04:00
										 |  |  |     permutations-quot each ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-06 12:07:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : map-permutations ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:12:57 -04:00
										 |  |  |     permutations-quot map ; inline
 | 
					
						
							| 
									
										
										
										
											2012-04-18 17:43:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : filter-permutations ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
 | 
					
						
							| 
									
										
										
										
											2012-04-18 18:17:48 -04:00
										 |  |  |     selector [ each-permutation ] dip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-23 11:29:20 -04:00
										 |  |  | : all-permutations ( seq -- seq' )
 | 
					
						
							|  |  |  |     [ ] map-permutations ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-01 23:44:57 -04:00
										 |  |  | : all-permutations? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
 | 
					
						
							|  |  |  |     permutations-quot all? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : find-permutation ( ... seq quot: ( ... elt -- ... ? ) -- ... elt/f )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:12:57 -04:00
										 |  |  |     [ permutations-quot find drop ] | 
					
						
							|  |  |  |     [ drop over [ permutation ] [ 2drop f ] if ] 2bi ; inline
 | 
					
						
							| 
									
										
										
										
											2012-04-21 02:31:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : reduce-permutations ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
 | 
					
						
							| 
									
										
										
										
											2008-11-06 12:07:19 -05:00
										 |  |  |     swapd each-permutation ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 20:00:31 -05:00
										 |  |  | : inverse-permutation ( seq -- permutation )
 | 
					
						
							| 
									
										
										
										
											2011-04-07 12:01:21 -04:00
										 |  |  |     <enum> sort-values keys ;
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-02 12:54:11 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cut-point ( seq -- n )
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     [ last ] keep [ [ > ] keep swap ] find-last drop nip ; inline
 | 
					
						
							| 
									
										
										
										
											2012-03-02 12:54:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : greater-from-last ( n seq -- i )
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     [ nip ] [ nth ] 2bi [ > ] curry find-last drop ; inline
 | 
					
						
							| 
									
										
										
										
											2012-03-02 12:54:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : reverse-tail! ( n seq -- seq )
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     [ swap 1 + tail-slice reverse! drop ] keep ; inline
 | 
					
						
							| 
									
										
										
										
											2012-03-02 12:54:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (next-permutation) ( seq -- seq )
 | 
					
						
							|  |  |  |     dup cut-point [ | 
					
						
							|  |  |  |         swap [ greater-from-last ] 2keep
 | 
					
						
							|  |  |  |         [ exchange ] [ reverse-tail! nip ] 3bi
 | 
					
						
							|  |  |  |     ] [ reverse! ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  | HINTS: (next-permutation) array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-02 12:54:11 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : next-permutation ( seq -- seq )
 | 
					
						
							|  |  |  |     dup [ ] [ drop (next-permutation) ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-06 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | ! Combinadic-based combination methodology | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-01 17:07:39 -04:00
										 |  |  | ! "Algorithm 515: Generation of a Vector from the Lexicographical Index" | 
					
						
							|  |  |  | ! Buckles, B. P., and Lybanon, M. ACM | 
					
						
							|  |  |  | ! Transactions on Mathematical Software, Vol. 3, No. 2, June 1977. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: combination-indices ( x! p n -- seq )
 | 
					
						
							|  |  |  |     x 1 + x! | 
					
						
							|  |  |  |     p 0 <array> :> c 0 :> k! 0 :> r! | 
					
						
							|  |  |  |     p 1 - [| i | | 
					
						
							|  |  |  |         i [ 0 ] [ 1 - c nth ] if-zero i c set-nth
 | 
					
						
							|  |  |  |         [ k x < ] [ | 
					
						
							|  |  |  |             i c [ 1 + ] change-nth
 | 
					
						
							|  |  |  |             n i c nth - p i 1 + - nCk r! | 
					
						
							|  |  |  |             k r + k! | 
					
						
							|  |  |  |         ] do while k r - k! | 
					
						
							|  |  |  |     ] each-integer
 | 
					
						
							|  |  |  |     p 2 < [ 0 ] [ p 2 - c nth ] if
 | 
					
						
							|  |  |  |     p 1 < [ drop ] [ x + k - p 1 - c set-nth ] if
 | 
					
						
							|  |  |  |     c [ 1 - ] map! ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:43:07 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-21 23:11:47 -04:00
										 |  |  | : combination ( m seq k -- seq' )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 15:43:02 -04:00
										 |  |  |     swap [ length combination-indices ] [ nths-unsafe ] bi ;
 | 
					
						
							| 
									
										
										
										
											2012-04-21 23:11:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-01 17:07:39 -04:00
										 |  |  | TUPLE: combinations seq k length ;
 | 
					
						
							| 
									
										
										
										
											2012-04-23 11:29:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <combinations> ( seq k -- combinations )
 | 
					
						
							| 
									
										
										
										
											2012-06-01 17:07:39 -04:00
										 |  |  |     2dup [ length ] [ nCk ] bi* combinations boa ;
 | 
					
						
							| 
									
										
										
										
											2012-04-23 11:29:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: combinations length length>> ; inline
 | 
					
						
							| 
									
										
										
										
											2012-06-01 17:07:39 -04:00
										 |  |  | M: combinations nth-unsafe [ seq>> ] [ k>> ] bi combination ;
 | 
					
						
							| 
									
										
										
										
											2012-04-23 11:29:20 -04:00
										 |  |  | M: combinations hashcode* tuple-hashcode ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: combinations immutable-sequence | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-max-index ( seq n -- i )
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     over length - '[ _ + >= ] find-index drop ; inline
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  | : increment-rest ( i seq -- )
 | 
					
						
							|  |  |  |     [ nth ] [ swap tail-slice ] 2bi
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     [ drop 1 + dup ] map! 2drop ; inline
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : increment-last ( seq -- )
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  |     [ [ length 1 - ] keep [ 1 + ] change-nth ] unless-empty ; inline
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  | :: next-combination ( seq n -- seq )
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  |     seq n find-max-index [ | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |         1 [-] seq increment-rest | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         seq increment-last | 
					
						
							| 
									
										
										
										
											2013-10-14 18:44:19 -04:00
										 |  |  |     ] if* seq ; inline
 | 
					
						
							| 
									
										
										
										
											2013-05-01 14:18:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  | :: combinations-quot ( seq k quot -- seq quot' )
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  |     seq length :> n | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     n k nCk iota k iota >array seq quot n | 
					
						
							|  |  |  |     '[ drop _ [ _ nths-unsafe @ ] keep _ next-combination drop ] ; inline
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:57:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : each-combination ( ... seq k quot: ( ... elt -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     combinations-quot each ; inline
 | 
					
						
							| 
									
										
										
										
											2009-11-27 23:50:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : map-combinations ( ... seq k quot: ( ... elt -- ... newelt ) -- ... newseq )
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     combinations-quot map ; inline
 | 
					
						
							| 
									
										
										
										
											2009-11-27 23:50:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : filter-combinations ( ... seq k quot: ( ... elt -- ... ? ) -- ... newseq )
 | 
					
						
							| 
									
										
										
										
											2012-04-18 18:17:48 -04:00
										 |  |  |     selector [ each-combination ] dip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 12:04:16 -04:00
										 |  |  | : map>assoc-combinations ( ... seq k quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     [ combinations-quot ] dip map>assoc ; inline
 | 
					
						
							| 
									
										
										
										
											2009-11-27 23:50:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-18 18:31:52 -05:00
										 |  |  | : all-combinations ( seq k -- seq' )
 | 
					
						
							| 
									
										
										
										
											2011-10-15 22:19:44 -04:00
										 |  |  |     [ ] map-combinations ;
 | 
					
						
							| 
									
										
										
										
											2009-05-07 21:23:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-01 23:44:57 -04:00
										 |  |  | : all-combinations? ( ... seq k quot: ( ... elt -- ... ? ) -- ... ? )
 | 
					
						
							|  |  |  |     combinations-quot all? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:15:47 -04:00
										 |  |  | : find-combination ( ... seq k quot: ( ... elt -- ... ? ) -- ... elt/f )
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:04:17 -04:00
										 |  |  |     [ combinations-quot find drop ] | 
					
						
							|  |  |  |     [ drop pick [ combination ] [ 3drop f ] if ] 3bi ; inline
 | 
					
						
							| 
									
										
										
										
											2012-04-21 02:31:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 10:17:36 -04:00
										 |  |  | : reduce-combinations ( ... seq k identity quot: ( ... prev elt -- ... next ) -- ... result )
 | 
					
						
							| 
									
										
										
										
											2009-05-07 21:23:58 -04:00
										 |  |  |     [ -rot ] dip each-combination ; inline
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:09:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-subsets ( seq -- subsets )
 | 
					
						
							| 
									
										
										
										
											2010-07-26 11:12:36 -04:00
										 |  |  |     dup length [0,b] [ all-combinations ] with map concat ;
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:09:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-13 17:34:14 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:09:57 -04:00
										 |  |  | : (selections) ( seq n -- selections )
 | 
					
						
							| 
									
										
										
										
											2013-04-06 16:06:23 -04:00
										 |  |  |     [ dup [ 1sequence ] curry { } map-as dup ] [ 1 - ] bi* [ | 
					
						
							|  |  |  |         cartesian-product concat [ concat ] map
 | 
					
						
							| 
									
										
										
										
											2010-08-13 17:34:14 -04:00
										 |  |  |     ] with times ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:09:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : selections ( seq n -- selections )
 | 
					
						
							| 
									
										
										
										
											2010-07-26 11:12:36 -04:00
										 |  |  |     dup 0 > [ (selections) ] [ 2drop { } ] if ;
 |