]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/binary-search/binary-search.factor
factor: trim using lists
[factor.git] / basis / binary-search / binary-search.factor
index 36e983a1c8c1af71c9b00ed8f2c419f9aa6c9ab8..6de32d8f7ad7e4d4914440f325301a7c6fd55055 100644 (file)
@@ -1,35 +1,34 @@
 ! Copyright (C) 2008, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays combinators hints kernel locals math
-math.order sequences ;
+USING: arrays combinators kernel math math.order sequences
+sequences.private vectors ;
 IN: binary-search
 
 <PRIVATE
 
 :: (search) ( seq from to quot: ( elt -- <=> ) -- i elt )
     from to + 2/ :> midpoint@
-    midpoint@ seq nth :> midpoint
+    midpoint@ seq nth-unsafe :> midpoint
 
     to from - 1 <= [
         midpoint@ midpoint
     ] [
         midpoint quot call {
-            { +eq+ [ midpoint@ midpoint ] }
             { +lt+ [ seq from midpoint@ quot (search) ] }
             { +gt+ [ seq midpoint@ to quot (search) ] }
+            { +eq+ [ midpoint@ midpoint ] }
         } case
     ] if ; inline recursive
 
 PRIVATE>
 
 : search ( seq quot: ( elt -- <=> ) -- i elt )
-    over empty? [ 2drop f f ] [ [ 0 over length ] dip (search) ] if ;
-    inline
-
-: natural-search ( obj seq -- i elt )
-    [ <=> ] with search ;
+    over empty? [ 2drop f f ] [ [ 0 over length ] dip (search) ] if ; inline
 
-HINTS: natural-search array ;
+GENERIC: natural-search ( obj seq -- i elt )
+M: object natural-search [ <=> ] with search ;
+M: array natural-search [ <=> ] with search ;
+M: vector natural-search [ <=> ] with search ;
 
 : sorted-index ( obj seq -- i )
     natural-search drop ;