| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | USING: accessors combinators fry kernel math math.order | 
					
						
							|  |  |  | sequences sequences.private ;
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | IN: grouping | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:30:57 -04:00
										 |  |  | ERROR: groups-error seq n ;
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | GENERIC: group@ ( n groups -- from to seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: chunking { seq read-only } { n read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 03:12:49 -05:00
										 |  |  | INSTANCE: chunking sequence | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: chunking nth-unsafe group@ <slice-unsafe> ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 19:57:52 -05:00
										 |  |  | M: chunking set-nth-unsafe group@ <slice-unsafe> 0 swap copy ;
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: chunking like drop { } like ; inline
 | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | : check-groups ( seq n -- seq n )
 | 
					
						
							|  |  |  |     dup 0 <= [ groups-error ] when ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | : new-groups ( seq n class -- groups )
 | 
					
						
							|  |  |  |     [ check-groups ] dip boa ; inline
 | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | TUPLE: groups < chunking ;
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | : <groups> ( seq n -- groups )
 | 
					
						
							|  |  |  |     groups new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: clumps < chunking ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: clumps length | 
					
						
							| 
									
										
										
										
											2012-10-28 10:28:05 -04:00
										 |  |  |     dup seq>> length [ drop 0 ] [ | 
					
						
							| 
									
										
										
										
											2012-10-29 12:47:15 -04:00
										 |  |  |         swap [ 1 + ] [ n>> ] bi* [-] | 
					
						
							| 
									
										
										
										
											2012-10-28 10:28:05 -04:00
										 |  |  |     ] if-zero ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | M: clumps group@ | 
					
						
							| 
									
										
										
										
											2012-10-29 12:47:15 -04:00
										 |  |  |     [ n>> over + ] [ seq>> ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 13:11:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | : <clumps> ( seq n -- clumps )
 | 
					
						
							|  |  |  |     clumps new-groups ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 14:38:05 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : map-like ( seq n quot -- seq )
 | 
					
						
							| 
									
										
										
										
											2018-06-19 20:15:05 -04:00
										 |  |  |     keepd '[ _ like ] map ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 14:38:05 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 14:38:05 -04:00
										 |  |  | : group ( seq n -- array ) [ <groups> ] map-like ; inline
 | 
					
						
							| 
									
										
										
										
											2008-06-09 06:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 14:38:05 -04:00
										 |  |  | : clump ( seq n -- array ) [ <clumps> ] map-like ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 01:08:54 -04:00
										 |  |  | : monotonic? ( seq quot: ( elt1 elt2 -- ? ) -- ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-06 10:31:45 -05:00
										 |  |  |     over length dup 2 < [ 3drop t ] [ | 
					
						
							|  |  |  |         2 = [ | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  |             [ first2-unsafe ] dip call
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							| 
									
										
										
										
											2017-10-24 20:22:55 -04:00
										 |  |  |             [ [ first-unsafe 1 ] [ setup-each ] bi ] dip
 | 
					
						
							| 
									
										
										
										
											2013-03-06 10:31:45 -05:00
										 |  |  |             '[ @ _ keep swap ] (all-integers?) nip
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  |         ] if
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-06 10:31:45 -05:00
										 |  |  | : all-equal? ( seq -- ? ) [ = ] monotonic? ;
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:24:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-06 10:31:45 -05:00
										 |  |  | : all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 18:32:41 -04:00
										 |  |  | TUPLE: circular-slice | 
					
						
							|  |  |  |     { from integer read-only } | 
					
						
							|  |  |  |     { to integer read-only } | 
					
						
							|  |  |  |     { seq read-only } ;
 | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:20:47 -05:00
										 |  |  | INSTANCE: circular-slice virtual-sequence | 
					
						
							| 
									
										
										
										
											2010-03-17 02:13:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-28 23:05:42 -04:00
										 |  |  | M: circular-slice equal? over circular-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@ | 
					
						
							| 
									
										
										
										
											2012-10-28 08:12:46 -04:00
										 |  |  |     [ from>> + ] [ seq>> ] bi [ length rem ] keep ; inline
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | C: <circular-slice> circular-slice | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 00:12:28 -04:00
										 |  |  | TUPLE: circular-clumps | 
					
						
							|  |  |  |     { seq read-only } | 
					
						
							|  |  |  |     { n read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | INSTANCE: circular-clumps sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: circular-clumps length | 
					
						
							|  |  |  |     seq>> length ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: circular-clumps nth | 
					
						
							| 
									
										
										
										
											2013-03-23 14:38:05 -04:00
										 |  |  |     [ n>> over + ] [ seq>> ] bi <circular-slice> ; inline
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <circular-clumps> ( seq n -- clumps )
 | 
					
						
							| 
									
										
										
										
											2012-10-28 08:12:46 -04:00
										 |  |  |     circular-clumps new-groups ; inline
 | 
					
						
							| 
									
										
										
										
											2010-02-25 20:05:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : circular-clump ( seq n -- array )
 | 
					
						
							| 
									
										
										
										
											2013-03-28 23:05:42 -04:00
										 |  |  |     [ <circular-clumps> ] map-like ; inline
 |