]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/heaps/heaps.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / heaps / heaps.factor
index dccf1e5e5500542f82130dd3df8a1efed50a0fb2..2c6d4c8102b06ebf247a540cdfb92b7d2ba6a31d 100644 (file)
@@ -1,16 +1,14 @@
 ! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
 ! Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs combinators fry growable kernel
-kernel.private math math.order math.private sequences
-sequences.private summary vectors ;
-
+USING: accessors arrays assocs fry kernel kernel.private
+math math.order math.private sequences sequences.private summary
+vectors ;
 IN: heaps
 
 GENERIC: heap-push* ( value key heap -- entry )
 GENERIC: heap-peek ( heap -- value key )
 GENERIC: heap-pop* ( heap -- )
-GENERIC: heap-pop ( heap -- value key )
 GENERIC: heap-delete ( entry heap -- )
 GENERIC: heap-empty? ( heap -- ? )
 GENERIC: heap-size ( heap -- n )
@@ -20,7 +18,7 @@ GENERIC: heap-size ( heap -- n )
 TUPLE: heap { data vector } ;
 
 : <heap> ( class -- heap )
-    [ V{ } clone ] dip boa ; inline
+    V{ } clone swap boa ; inline
 
 TUPLE: entry value key heap index ;
 
@@ -37,11 +35,9 @@ TUPLE: max-heap < heap ;
 
 : <max-heap> ( -- max-heap ) max-heap <heap> ;
 
-M: heap heap-empty? ( heap -- ? )
-    data>> empty? ; inline
+M: heap heap-empty? data>> empty? ; inline
 
-M: heap heap-size ( heap -- n )
-    data>> length ; inline
+M: heap heap-size data>> length ; inline
 
 <PRIVATE
 
@@ -54,32 +50,15 @@ M: heap heap-size ( heap -- n )
 : up ( n -- m )
     { fixnum } declare 1 fixnum-fast 2/ ; inline
 
-: data-nth ( n heap -- entry )
-    data>> nth-unsafe { entry } declare ; inline
-
-: left-value ( n heap -- entry )
-    [ left ] dip data-nth ; inline
-
-: right-value ( n heap -- entry )
-    [ right ] dip data-nth ; inline
-
-: data-set-nth ( entry n heap -- )
-    [ [ swap index<< ] 2keep ] dip
-    data>> set-nth-unsafe ; inline
+: data-nth ( n data -- entry )
+    nth-unsafe { entry } declare ; inline
 
-: data-push ( entry heap -- n )
-    dup heap-size [
-        swap
-        [ data>> ensure 2drop ]
-        [ data-set-nth ] 2bi
-    ] keep ; inline
+: data-set-nth ( entry n data -- )
+    [ [ >>index ] keep ] dip set-nth-unsafe ; inline
 
-: data-first ( heap -- entry )
-    data>> first ; inline
-
-: data-exchange ( m n heap -- )
-    [ '[ _ data-nth ] bi@ ]
-    [ '[ _ data-set-nth ] bi@ ] 3bi ; inline
+: data-push ( entry data -- n )
+    [ length [ >>index ] keep ]
+    [ [ set-nth ] keepd ] bi ; inline
 
 GENERIC: heap-compare ( entry1 entry2 heap -- ? )
 
@@ -89,113 +68,118 @@ M: min-heap heap-compare
 M: max-heap heap-compare
     drop { entry entry } declare [ key>> ] bi@ before? ; inline
 
-: heap-bounds-check? ( m heap -- ? )
-    heap-size >= ; inline
+PRIVATE>
 
-: left-bounds-check? ( m heap -- ? )
-    [ left ] dip heap-bounds-check? ; inline
+: >entry< ( entry -- value key )
+    [ value>> ] [ key>> ] bi ; inline
 
-: right-bounds-check? ( m heap -- ? )
-    [ right ] dip heap-bounds-check? ; inline
+M: heap heap-peek
+    data>> first >entry< ;
 
-: continue? ( m n heap -- ? )
-    [ data-nth nip ]
-    [ nip data-nth ]
-    [ 2nip ] 3tri heap-compare ; inline
+<PRIVATE
 
-DEFER: up-heap
+! names and optimizations inspired by cpython/Lib/heapq.py,
+! refer to it from in depth explanations of the optimizations.
 
-: (up-heap) ( n heap -- )
-    [ dup up ] dip
-    3dup continue? [
-        [ data-exchange ] [ up-heap ] 2bi
-    ] [
-        3drop
-    ] if ; inline recursive
+! called bubble-up in the literature... but we keep cpython's name.
+:: sift-down ( heap from to -- )
+    heap data>>      :> data
+    to data data-nth :> tmp
 
-: up-heap ( n heap -- )
-    over 0 > [ (up-heap) ] [ 2drop ] if ; inline recursive
+    to t [ over from > and ] [
+        dup up
+        dup data data-nth
+        dup tmp heap heap-compare [
+            rot data data-set-nth t
+        ] [
+            2drop f
+        ] if
+    ] while
 
-: (child) ( m heap -- n )
-    { [ drop ] [ left-value ] [ right-value ] [ nip ] } 2cleave
-    heap-compare [ right ] [ left ] if ; inline
+    tmp swap data data-set-nth ; inline
 
-: child ( m heap -- n )
-    2dup right-bounds-check?
-    [ drop left ] [ (child) ] if ; inline
+PRIVATE>
 
-DEFER: down-heap
+M: heap heap-push*
+    [ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
 
-: (down-heap) ( m heap -- )
-    [ drop ] [ child ] [ nip ] 2tri
-    3dup continue? [
-        3drop
-    ] [
-        [ data-exchange ] [ down-heap ] 2bi
-    ] if ; inline recursive
+: heap-push ( value key heap -- )
+    heap-push* drop ;
 
-: down-heap ( m heap -- )
-    2dup left-bounds-check?
-    [ 2drop ] [ (down-heap) ] if ; inline recursive
+: heap-push-all ( assoc heap -- )
+    '[ swap _ heap-push ] assoc-each ;
 
-PRIVATE>
+<PRIVATE
+
+! called bubble-down in the literature... but we keep cpython's name.
+! A quote from cpython's implementation:
+! > We *could* break out of the loop as soon as we find a pos where newitem <=
+! > both its children, but turns out that's not a good idea [...]
+! Indeed the code is 33% slower if we remove this optimization.
+:: sift-up ( heap n -- )
+    heap data>>     :> data
+    data length     :> end
+    n data data-nth :> tmp
+
+    n dup left [ dup end < ] [
+        dup 1 fixnum+fast
+        dup end < [
+            2dup [ data data-nth ] bi@ heap heap-compare
+        ] [ f ] if
+        [ nip ] [ drop ] if
+        [ data data-nth swap data data-set-nth ]
+        [ dup left ] bi
+    ] while drop
+
+    tmp over data data-set-nth
+    heap n rot sift-down ; inline
 
-M: heap heap-push* ( value key heap -- entry )
-    [ <entry> dup ] [ data-push ] [ up-heap ] tri ;
+PRIVATE>
 
-: heap-push ( value key heap -- ) heap-push* drop ;
+M: heap heap-pop*
+    dup data>> f over first index<< [ pop ] keep
+    [ 2drop ] [ set-first 0 sift-up ] if-empty ;
 
-: heap-push-all ( assoc heap -- )
-    '[ swap _ heap-push ] assoc-each ;
+: heap-pop ( heap -- value key )
+    [ heap-peek ] [ heap-pop* ] bi ;
 
-: >entry< ( entry -- value key )
-    [ value>> ] [ key>> ] bi ; inline
+: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
+    [ drop '[ _ heap-empty? ] ]
+    [ '[ _ heap-pop @ ] until ] 2bi ; inline
 
-M: heap heap-peek ( heap -- value key )
-    data-first >entry< ;
+: heap-pop-all ( heap -- alist )
+    [ heap-size <vector> ] keep
+    [ swap 2array suffix! ] slurp-heap { } like ;
 
 ERROR: bad-heap-delete ;
 
 M: bad-heap-delete summary
     drop "Invalid entry passed to heap-delete" ;
 
+<PRIVATE
+
 : entry>index ( entry heap -- n )
     over heap>> eq? [ bad-heap-delete ] unless
-    index>> { fixnum } declare ; inline
+    index>> dup [ bad-heap-delete ] unless
+    { fixnum } declare ; inline
 
-M: heap heap-delete ( entry heap -- )
-    [ entry>index ] keep
-    2dup heap-size 1 - = [
-        nip data>> pop*
-    ] [
-        [ nip data>> pop ]
-        [ data-set-nth ]
-        [ down-heap ] 2tri
-    ] if ;
-
-M: heap heap-pop* ( heap -- )
-    [ data-first ] keep heap-delete ;
-
-M: heap heap-pop ( heap -- value key )
-    [ data-first dup ] keep heap-delete >entry< ;
-
-: heap-pop-all ( heap -- alist )
-    [ dup heap-empty? not ]
-    [ dup heap-pop swap 2array ]
-    produce nip ;
-
-ERROR: not-a-heap obj ;
-
-: check-heap ( heap -- heap )
-    dup heap? [ not-a-heap ] unless ; inline
+PRIVATE>
 
-: slurp-heap ( heap quot: ( elt -- ) -- )
-    [ check-heap ] dip over heap-empty? [ 2drop ] [
-        [ [ heap-pop drop ] dip call ] [ slurp-heap ] 2bi
-    ] if ; inline recursive
+M:: heap heap-delete ( entry heap -- )
+    entry heap entry>index :> n
+    heap data>>            :> data
+    data pop               :> nth-entry
+    f entry index<<
+    n data length = [
+        nth-entry n data data-set-nth
+        n 0 = [ t ] [ nth-entry n up data data-nth heap heap-compare ] if
+        [ heap n sift-up ] [ heap 0 n sift-down ] if
+    ] unless ;
 
 : >min-heap ( assoc -- min-heap )
-    <min-heap> [ heap-push-all ] keep ;
+    dup assoc-size <vector> min-heap boa
+    [ heap-push-all ] keep ;
 
 : >max-heap ( assoc -- max-heap )
-    <max-heap> [ heap-push-all ] keep ;
+    dup assoc-size <vector> max-heap boa
+    [ heap-push-all ] keep ;