]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sorting/quick/quick.factor
factor: trim using lists
[factor.git] / extra / sorting / quick / quick.factor
index 38ee1b96273353e21e47834a52eed9377042c4d0..f7b5ed51a280922358fd15026ad18ae2632cab0d 100644 (file)
@@ -1,31 +1,31 @@
 ! Copyright (C) 2014 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: combinators kernel locals math math.order sequences
-sequences.private ;
+USING: arrays kernel math math.order math.private sequences
+sequences.private strings vectors ;
 
 IN: sorting.quick
 
 <PRIVATE
 
-:: quicksort ( seq from to quot -- )
+:: quicksort ( seq from to quot: ( obj1 obj2 -- <=> ) -- )
     from to < [
-        from to + 2/ seq nth-unsafe :> pivot
+        from to fixnum+fast 2/ seq nth-unsafe :> pivot
 
         from to [ 2dup <= ] [
             [
-                over seq nth-unsafe pivot quot call( x x -- x )
+                over seq nth-unsafe pivot quot call
                 +lt+ eq?
-            ] [ [ 1 + ] dip ] while
+            ] [ [ 1 fixnum+fast ] dip ] while
 
             [
-                dup seq nth-unsafe pivot quot call( x x -- x )
+                dup seq nth-unsafe pivot quot call
                 +gt+ eq?
-            ] [ 1 - ] while
+            ] [ 1 fixnum-fast ] while
 
             2dup <= [
                 [ seq exchange-unsafe ]
-                [ [ 1 + ] [ 1 - ] bi* ] 2bi
+                [ [ 1 fixnum+fast ] [ 1 fixnum-fast ] bi* ] 2bi
             ] when
         ] while
 
@@ -33,10 +33,14 @@ IN: sorting.quick
         [ seq ] dip to quot quicksort
     ] when ; inline recursive
 
+: check-array-capacity ( n -- n )
+    integer>fixnum-strict dup array-capacity?
+    [ "too large" throw ] unless ; inline
+
 PRIVATE>
 
 : sort! ( seq quot: ( obj1 obj2 -- <=> ) -- )
-    [ 0 over length 1 - ] dip quicksort ;
+    [ 0 over length check-array-capacity 1 - ] dip quicksort ; inline
 
 : sort-with! ( seq quot: ( elt -- key ) -- )
     [ compare ] curry sort! ; inline
@@ -44,5 +48,9 @@ PRIVATE>
 : inv-sort-with! ( seq quot: ( elt -- key ) -- )
     [ compare invert-comparison ] curry sort! ; inline
 
-: natural-sort! ( seq -- )
-    [ <=> ] sort! ;
+GENERIC: natural-sort! ( seq -- )
+
+M: object natural-sort!  [ <=> ] sort! ;
+M: array natural-sort! [ <=> ] sort! ;
+M: vector natural-sort! [ <=> ] sort! ;
+M: string natural-sort! [ <=> ] sort! ;