factor/basis/heaps/heaps.factor

188 lines
4.6 KiB
Factor
Raw Permalink Normal View History

! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
! Slava Pestov.
2007-11-02 15:41:19 -04:00
! See http://factorcode.org/license.txt for BSD license.
2019-01-11 11:19:50 -05:00
USING: accessors arrays assocs fry kernel kernel.private locals
math math.order math.private sequences sequences.private summary
vectors ;
2007-11-02 15:41:19 -04:00
IN: heaps
GENERIC: heap-push* ( value key heap -- entry )
GENERIC: heap-peek ( heap -- value key )
GENERIC: heap-pop* ( heap -- )
2008-02-21 20:12:37 -05:00
GENERIC: heap-delete ( entry heap -- )
GENERIC: heap-empty? ( heap -- ? )
2008-02-21 15:16:22 -05:00
GENERIC: heap-size ( heap -- n )
2007-11-02 15:41:19 -04:00
<PRIVATE
2008-02-21 15:16:22 -05:00
2009-07-18 01:52:24 -04:00
TUPLE: heap { data vector } ;
2007-11-02 15:41:19 -04:00
: <heap> ( class -- heap )
2014-12-01 18:16:47 -05:00
V{ } clone swap boa ; inline
2008-02-22 17:16:00 -05:00
TUPLE: entry value key heap index ;
2008-02-21 15:16:22 -05:00
2012-09-14 15:07:56 -04:00
: <entry> ( value key heap -- entry )
f entry boa ; inline
2008-02-21 15:16:22 -05:00
2007-11-02 15:41:19 -04:00
PRIVATE>
2008-04-04 01:33:06 -04:00
TUPLE: min-heap < heap ;
2007-11-02 15:41:19 -04:00
: <min-heap> ( -- min-heap ) min-heap <heap> ;
2007-11-02 15:41:19 -04:00
2008-04-04 01:33:06 -04:00
TUPLE: max-heap < heap ;
2007-11-02 15:41:19 -04:00
: <max-heap> ( -- max-heap ) max-heap <heap> ;
2007-11-02 15:41:19 -04:00
2008-07-14 04:33:13 -04:00
M: heap heap-empty? ( heap -- ? )
2010-04-01 20:05:32 -04:00
data>> empty? ; inline
2008-02-21 15:16:22 -05:00
2008-07-14 04:33:13 -04:00
M: heap heap-size ( heap -- n )
2012-09-14 15:07:56 -04:00
data>> length ; inline
2008-02-21 15:16:22 -05:00
2007-11-02 15:41:19 -04:00
<PRIVATE
2008-02-21 15:16:22 -05:00
2013-03-06 22:41:37 -05:00
: left ( n -- m )
{ fixnum } declare 1 fixnum-shift-fast 1 fixnum+fast ; inline
2008-02-21 15:16:22 -05:00
2013-03-06 22:41:37 -05:00
: right ( n -- m )
{ fixnum } declare 1 fixnum-shift-fast 2 fixnum+fast ; inline
2008-02-21 15:16:22 -05:00
2013-03-06 22:41:37 -05:00
: up ( n -- m )
{ fixnum } declare 1 fixnum-fast 2/ ; inline
2008-02-21 15:16:22 -05:00
: data-nth ( n data -- entry )
nth-unsafe { entry } declare ; inline
2008-02-21 15:16:22 -05:00
: data-set-nth ( entry n data -- )
[ [ >>index ] keep ] dip set-nth-unsafe ; inline
2008-02-21 15:16:22 -05:00
: data-push ( entry data -- n )
[ length [ >>index ] keep ]
[ [ set-nth ] keepd ] bi ; inline
2007-11-02 15:41:19 -04:00
GENERIC: heap-compare ( entry1 entry2 heap -- ? )
2008-02-21 15:16:22 -05:00
2014-07-09 21:12:10 -04:00
M: min-heap heap-compare
drop { entry entry } declare [ key>> ] bi@ after? ; inline
2008-02-21 15:16:22 -05:00
2014-07-09 21:12:10 -04:00
M: max-heap heap-compare
drop { entry entry } declare [ key>> ] bi@ before? ; inline
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
PRIVATE>
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
: >entry< ( entry -- value key )
[ value>> ] [ key>> ] bi ; inline
2014-12-01 18:16:47 -05:00
M: heap heap-peek ( heap -- value key )
data>> first >entry< ;
2014-12-01 18:16:47 -05:00
<PRIVATE
! names and optimizations inspired by cpython/Lib/heapq.py,
! refer to it from in depth explanations of the optimizations.
! called bubble-up in the literature... but we keep cpython's name.
2014-12-01 18:16:47 -05:00
:: sift-down ( heap from to -- )
heap data>> :> data
to data data-nth :> tmp
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
to t [ over from > and ] [
dup up
dup data data-nth
2014-12-01 18:16:47 -05:00
dup tmp heap heap-compare [
rot data data-set-nth t
2014-12-01 18:16:47 -05:00
] [
2drop f
] if
] while
tmp swap data data-set-nth ; inline
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
PRIVATE>
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
M: heap heap-push*
[ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
: heap-push ( value key heap -- )
heap-push* drop ;
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
: heap-push-all ( assoc heap -- )
'[ swap _ heap-push ] assoc-each ;
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
<PRIVATE
2007-11-02 15:41:19 -04:00
! 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 [...]
2019-01-10 19:47:45 -05:00
! Indeed the code is 33% slower if we remove this optimization.
2014-12-01 18:16:47 -05:00
:: sift-up ( heap n -- )
heap data>> :> data
data length :> end
n data data-nth :> tmp
2014-12-01 18:16:47 -05:00
n dup left [ dup end < ] [
dup 1 fixnum+fast
dup end < [
2dup [ data data-nth ] bi@ heap heap-compare
] [ f ] if
2014-12-01 18:16:47 -05:00
[ nip ] [ drop ] if
[ data data-nth swap data data-set-nth ]
2014-12-01 18:16:47 -05:00
[ dup left ] bi
] while drop
2007-11-02 15:41:19 -04:00
tmp over data data-set-nth
2014-12-01 18:16:47 -05:00
heap n rot sift-down ; inline
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
PRIVATE>
2014-12-01 18:16:47 -05:00
M: heap heap-pop*
dup data>> f over first index<< [ pop ] keep
[ 2drop ] [ set-first 0 sift-up ] if-empty ;
2014-12-01 18:16:47 -05:00
: heap-pop ( heap -- value key )
[ heap-peek ] [ heap-pop* ] bi ;
2014-12-01 18:16:47 -05:00
: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
[ drop '[ _ heap-empty? ] ]
[ '[ _ heap-pop @ ] until ] 2bi ; inline
2014-12-01 18:16:47 -05:00
: heap-pop-all ( heap -- alist )
[ heap-size <vector> ] keep
[ swap 2array suffix! ] slurp-heap { } like ;
2007-11-02 15:41:19 -04:00
2008-08-29 03:13:27 -04:00
ERROR: bad-heap-delete ;
2013-03-06 22:41:37 -05:00
M: bad-heap-delete summary
2008-08-29 03:13:27 -04:00
drop "Invalid entry passed to heap-delete" ;
2012-09-14 13:03:30 -04:00
2014-12-01 18:16:47 -05:00
<PRIVATE
2008-02-22 17:16:00 -05:00
: entry>index ( entry heap -- n )
over heap>> eq? [ bad-heap-delete ] unless
index>> dup [ bad-heap-delete ] unless
{ fixnum } declare ; inline
2008-02-22 17:16:00 -05:00
2014-12-01 18:16:47 -05:00
PRIVATE>
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 ;
2007-11-05 12:35:44 -05:00
2013-03-31 22:01:04 -04:00
: >min-heap ( assoc -- min-heap )
dup assoc-size <vector> min-heap boa
[ heap-push-all ] keep ;
2013-03-31 22:01:04 -04:00
2014-06-10 20:18:37 -04:00
: >max-heap ( assoc -- max-heap )
dup assoc-size <vector> max-heap boa
[ heap-push-all ] keep ;