| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | ! Copyright (C) 2005, 2010 Slava Pestov, Joe Groff. | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: kernel math math.order strings arrays vectors sequences | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | sequences.private accessors fry combinators ;
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | IN: grouping | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | MIXIN: chunking | 
					
						
							|  |  |  | INSTANCE: chunking sequence | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: group@ ( n groups -- from to seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | M: chunking set-nth group@ <slice> 0 swap copy ;
 | 
					
						
							|  |  |  | M: chunking like drop { } like ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | MIXIN: subseq-chunking | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | INSTANCE: subseq-chunking chunking | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | INSTANCE: subseq-chunking sequence | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: subseq-chunking nth group@ subseq ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | MIXIN: slice-chunking | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | INSTANCE: slice-chunking chunking | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | INSTANCE: slice-chunking sequence | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: slice-chunking nth group@ <slice> ; inline
 | 
					
						
							|  |  |  | M: slice-chunking nth-unsafe group@ slice boa ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | MIXIN: abstract-groups | 
					
						
							|  |  |  | INSTANCE: abstract-groups sequence | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | M: abstract-groups length | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  |     [ seq>> length ] [ n>> ] bi [ + 1 - ] keep /i ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | M: abstract-groups set-length | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  |     [ n>> * ] [ seq>> ] bi set-length ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | M: abstract-groups group@ | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  |     [ n>> [ * dup ] keep + ] [ seq>> ] bi [ length min ] keep ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | MIXIN: abstract-clumps | 
					
						
							|  |  |  | INSTANCE: abstract-clumps sequence | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: abstract-clumps length | 
					
						
							| 
									
										
										
										
											2009-10-23 03:58:15 -04:00
										 |  |  |     [ seq>> length 1 + ] [ n>> ] bi [-] ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: abstract-clumps set-length | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  |     [ n>> + 1 - ] [ seq>> ] bi set-length ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: abstract-clumps group@ | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  |     [ n>> over + ] [ seq>> ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | TUPLE: chunking-seq { seq read-only } { n read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : check-groups ( n -- n )
 | 
					
						
							|  |  |  |     dup 0 <= [ "Invalid group count" throw ] when ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : new-groups ( seq n class -- groups )
 | 
					
						
							|  |  |  |     [ check-groups ] dip boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | : slice-mod ( n length -- n' )
 | 
					
						
							|  |  |  |     2dup >= [ - ] [ drop ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : check-circular-clumps ( seq n -- seq n )
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  |     2dup 1 - swap bounds-check 2drop ; inline
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | TUPLE: groups < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: groups subseq-chunking | 
					
						
							|  |  |  | INSTANCE: groups abstract-groups | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <groups> ( seq n -- groups )
 | 
					
						
							|  |  |  |     groups new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | TUPLE: sliced-groups < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: sliced-groups slice-chunking | 
					
						
							|  |  |  | INSTANCE: sliced-groups abstract-groups | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <sliced-groups> ( seq n -- groups )
 | 
					
						
							|  |  |  |     sliced-groups new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | TUPLE: clumps < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: clumps subseq-chunking | 
					
						
							|  |  |  | INSTANCE: clumps abstract-clumps | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <clumps> ( seq n -- clumps )
 | 
					
						
							|  |  |  |     clumps new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | TUPLE: sliced-clumps < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: sliced-clumps slice-chunking | 
					
						
							|  |  |  | INSTANCE: sliced-clumps abstract-clumps | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <sliced-clumps> ( seq n -- clumps )
 | 
					
						
							|  |  |  |     sliced-clumps new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : group ( seq n -- array ) <groups> { } like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : clump ( seq n -- array ) <clumps> { } like ;
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : monotonic? ( seq quot -- ? )
 | 
					
						
							|  |  |  |     over length 2 < [ 2drop t ] [ | 
					
						
							|  |  |  |         over length 2 = [ | 
					
						
							|  |  |  |             [ first2-unsafe ] dip call
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             [ 2 <sliced-clumps> ] dip
 | 
					
						
							| 
									
										
										
										
											2009-01-26 00:04:35 -05:00
										 |  |  |             '[ first2-unsafe @ ] all?
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  |         ] if
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-equal? ( seq -- ? ) [ = ] monotonic? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  | : all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | TUPLE: circular-slice { from read-only } { to read-only } { seq read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | INSTANCE: circular-slice virtual-sequence | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | M: circular-slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | M: circular-slice hashcode* [ sequence-hashcode ] recursive-hashcode ;
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | M: circular-slice length [ to>> ] [ from>> ] bi - ; inline
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | M: circular-slice virtual-exemplar seq>> ; inline
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | M: circular-slice virtual@ | 
					
						
							|  |  |  |     [ from>> + ] [ seq>> ] bi [ length slice-mod ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <circular-slice> circular-slice | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: sliced-circular-clumps < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: sliced-circular-clumps sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sliced-circular-clumps length | 
					
						
							|  |  |  |     seq>> length ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sliced-circular-clumps nth | 
					
						
							|  |  |  |     [ n>> over + ] [ seq>> ] bi <circular-slice> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <sliced-circular-clumps> ( seq n -- clumps )
 | 
					
						
							|  |  |  |     check-circular-clumps sliced-circular-clumps boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: circular-clumps < chunking-seq ;
 | 
					
						
							|  |  |  | INSTANCE: circular-clumps sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: circular-clumps length | 
					
						
							|  |  |  |     seq>> length ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: circular-clumps nth | 
					
						
							|  |  |  |     [ n>> over + ] [ seq>> ] bi [ <circular-slice> ] [ like ] bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <circular-clumps> ( seq n -- clumps )
 | 
					
						
							|  |  |  |     check-circular-clumps circular-clumps boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : circular-clump ( seq n -- array )
 | 
					
						
							|  |  |  |     <circular-clumps> { } like ; inline
 |