| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  | ! Copyright (C) 2014 John Benediktsson | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 11:57:47 -04:00
										 |  |  | USING: arrays combinators kernel locals math math.order | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  | math.private sequences sequences.private strings vectors ;
 | 
					
						
							| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | IN: sorting.quick | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 11:57:47 -04:00
										 |  |  | :: quicksort ( seq from to quot: ( obj1 obj2 -- <=> ) -- )
 | 
					
						
							| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  |     from to < [ | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  |         from to fixnum+fast 2/ seq nth-unsafe :> pivot | 
					
						
							| 
									
										
										
										
											2014-06-09 20:53:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         from to [ 2dup <= ] [ | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2015-08-15 11:57:47 -04:00
										 |  |  |                 over seq nth-unsafe pivot quot call
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  |                 +lt+ eq?
 | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  |             ] [ [ 1 fixnum+fast ] dip ] while
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2015-08-15 11:57:47 -04:00
										 |  |  |                 dup seq nth-unsafe pivot quot call
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  |                 +gt+ eq?
 | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  |             ] [ 1 fixnum-fast ] while
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-09 20:53:16 -04:00
										 |  |  |             2dup <= [ | 
					
						
							|  |  |  |                 [ seq exchange-unsafe ] | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  |                 [ [ 1 fixnum+fast ] [ 1 fixnum-fast ] bi* ] 2bi
 | 
					
						
							| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  |             ] when
 | 
					
						
							|  |  |  |         ] while
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  |         [ seq from ] dip quot quicksort | 
					
						
							|  |  |  |         [ seq ] dip to quot quicksort | 
					
						
							| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  |     ] when ; inline recursive
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  | : check-array-capacity ( n -- n )
 | 
					
						
							|  |  |  |     integer>fixnum-strict dup array-capacity? | 
					
						
							|  |  |  |     [ "too large" throw ] unless ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-09 14:17:07 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  | : sort! ( seq quot: ( obj1 obj2 -- <=> ) -- )
 | 
					
						
							| 
									
										
										
										
											2015-08-15 20:10:27 -04:00
										 |  |  |     [ 0 over length check-array-capacity 1 - ] dip quicksort ; inline
 | 
					
						
							| 
									
										
										
										
											2014-06-10 19:45:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sort-with! ( seq quot: ( elt -- key ) -- )
 | 
					
						
							|  |  |  |     [ compare ] curry sort! ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : inv-sort-with! ( seq quot: ( elt -- key ) -- )
 | 
					
						
							|  |  |  |     [ compare invert-comparison ] curry sort! ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 11:57:47 -04:00
										 |  |  | GENERIC: natural-sort! ( seq -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object natural-sort!  [ <=> ] sort! ;
 | 
					
						
							|  |  |  | M: array natural-sort! [ <=> ] sort! ;
 | 
					
						
							|  |  |  | M: vector natural-sort! [ <=> ] sort! ;
 | 
					
						
							|  |  |  | M: string natural-sort! [ <=> ] sort! ;
 |