2008-02-21 18:07:26 -05:00
|
|
|
! 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
|
|
|
|
|
2008-02-21 18:07:26 -05:00
|
|
|
GENERIC: heap-push* ( value key heap -- entry )
|
2007-12-20 00:34:30 -05:00
|
|
|
GENERIC: heap-peek ( heap -- value key )
|
|
|
|
GENERIC: heap-pop* ( heap -- )
|
2008-02-21 20:12:37 -05:00
|
|
|
GENERIC: heap-delete ( entry heap -- )
|
2007-12-20 00:34:30 -05:00
|
|
|
GENERIC: heap-empty? ( heap -- ? )
|
2008-02-21 15:16:22 -05:00
|
|
|
GENERIC: heap-size ( heap -- n )
|
2007-12-20 00:34:30 -05:00
|
|
|
|
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
|
|
|
|
2007-11-05 12:10:26 -05: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
|
|
|
|
2007-11-05 02:42:37 -05: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
|
|
|
|
2007-11-05 02:42:37 -05: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
|
|
|
|
2018-05-08 18:11:36 -04:00
|
|
|
: data-nth ( n data -- entry )
|
|
|
|
nth-unsafe { entry } declare ; inline
|
2008-02-21 15:16:22 -05:00
|
|
|
|
2018-05-08 18:11:36 -04:00
|
|
|
: data-set-nth ( entry n data -- )
|
|
|
|
[ [ >>index ] keep ] dip set-nth-unsafe ; inline
|
2008-02-21 15:16:22 -05:00
|
|
|
|
2018-05-08 18:11:36 -04:00
|
|
|
: data-push ( entry data -- n )
|
|
|
|
[ length [ >>index ] keep ]
|
2018-06-19 20:15:05 -04:00
|
|
|
[ [ set-nth ] keepd ] bi ; inline
|
2007-11-02 15:41:19 -04:00
|
|
|
|
2011-04-10 13:57:39 -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
|
2007-11-05 02:42:37 -05:00
|
|
|
|
2014-12-01 18:16:47 -05:00
|
|
|
M: heap heap-peek ( heap -- value key )
|
2018-05-08 18:11:36 -04:00
|
|
|
data>> first >entry< ;
|
2007-11-05 02:42:37 -05:00
|
|
|
|
2014-12-01 18:16:47 -05:00
|
|
|
<PRIVATE
|
2008-02-21 18:07:26 -05:00
|
|
|
|
2019-01-10 19:44:22 -05:00
|
|
|
! 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 -- )
|
2018-05-08 18:11:36 -04:00
|
|
|
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
|
2018-05-08 18:11:36 -04:00
|
|
|
dup data data-nth
|
2014-12-01 18:16:47 -05:00
|
|
|
dup tmp heap heap-compare [
|
2018-05-08 18:11:36 -04:00
|
|
|
rot data data-set-nth t
|
2014-12-01 18:16:47 -05:00
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if
|
|
|
|
] while
|
2008-02-21 18:07:26 -05:00
|
|
|
|
2018-05-08 18:11:36 -04:00
|
|
|
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*
|
2018-05-08 18:11:36 -04:00
|
|
|
[ <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
|
|
|
|
2019-01-10 19:44:22 -05:00
|
|
|
! called bubble-down in the literature... but we keep cpython's name.
|
|
|
|
! A quote from cpython's implementation:
|
2019-01-10 15:50:50 -05:00
|
|
|
! > 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 -- )
|
2018-05-08 18:11:36 -04:00
|
|
|
heap data>> :> data
|
|
|
|
data length :> end
|
|
|
|
n data data-nth :> tmp
|
2008-02-21 18:07:26 -05:00
|
|
|
|
2014-12-01 18:16:47 -05:00
|
|
|
n dup left [ dup end < ] [
|
|
|
|
dup 1 fixnum+fast
|
2018-05-08 18:11:36 -04:00
|
|
|
dup end < [
|
2019-01-10 19:44:22 -05:00
|
|
|
2dup [ data data-nth ] bi@ heap heap-compare
|
2018-05-08 18:11:36 -04:00
|
|
|
] [ f ] if
|
2014-12-01 18:16:47 -05:00
|
|
|
[ nip ] [ drop ] if
|
2018-05-08 18:11:36 -04:00
|
|
|
[ 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
|
|
|
|
2018-05-08 18:11:36 -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>
|
2008-02-21 18:07:26 -05:00
|
|
|
|
2014-12-01 18:16:47 -05:00
|
|
|
M: heap heap-pop*
|
2019-01-10 19:44:22 -05:00
|
|
|
dup data>> f over first index<< [ pop ] keep
|
|
|
|
[ 2drop ] [ set-first 0 sift-up ] if-empty ;
|
2014-12-01 18:16:47 -05:00
|
|
|
|
2018-05-08 18:11:36 -04:00
|
|
|
: heap-pop ( heap -- value key )
|
|
|
|
[ heap-peek ] [ heap-pop* ] bi ;
|
2014-12-01 18:16:47 -05:00
|
|
|
|
2014-12-15 11:53:48 -05:00
|
|
|
: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
|
|
|
|
[ drop '[ _ heap-empty? ] ]
|
|
|
|
[ '[ _ heap-pop @ ] until ] 2bi ; inline
|
2014-12-01 18:16:47 -05:00
|
|
|
|
2014-12-15 11:53:48 -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 )
|
2015-08-13 19:13:05 -04:00
|
|
|
over heap>> eq? [ bad-heap-delete ] unless
|
2019-01-04 10:15:45 -05:00
|
|
|
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>
|
|
|
|
|
2019-01-10 19:44:22 -05:00
|
|
|
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 )
|
2014-12-15 16:29:01 -05:00
|
|
|
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 )
|
2014-12-15 16:29:01 -05:00
|
|
|
dup assoc-size <vector> max-heap boa
|
|
|
|
[ heap-push-all ] keep ;
|