factor/basis/heaps/heaps.factor

189 lines
4.2 KiB
Factor
Raw 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.
2014-12-01 18:16:47 -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 -- )
GENERIC: heap-pop ( heap -- value key )
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
ERROR: not-a-heap object ;
: check-heap ( heap -- heap )
2015-08-13 06:20:39 -04:00
dup heap? [ throw-not-a-heap ] unless ; inline
2008-02-21 15:16:22 -05:00
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 heap -- entry )
2013-03-06 22:41:37 -05:00
data>> nth-unsafe { entry } declare ; inline
2008-02-21 15:16:22 -05:00
2014-12-01 18:16:47 -05:00
: data-first ( heap -- entry )
2014-12-13 19:01:20 -05:00
data>> first ; inline
2008-02-21 15:16:22 -05:00
: data-set-nth ( entry n heap -- )
2014-12-01 18:16:47 -05:00
[ [ >>index ] keep ] dip data>> set-nth-unsafe ; inline
2008-02-21 15:16:22 -05:00
: data-push ( entry heap -- n )
2014-12-01 18:16:47 -05:00
[ heap-size [ >>index ] keep ]
[ data>> [ set-nth ] 2keep drop ] 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
: data-compare ( m n heap -- ? )
[ '[ _ data-nth ] bi@ ] [ heap-compare ] bi ; inline
2007-11-05 12:35:44 -05: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
2014-12-01 18:16:47 -05:00
:: sift-down ( heap from to -- )
to heap 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 heap data-nth
dup tmp heap heap-compare [
rot heap data-set-nth t
] [
2drop f
] if
] while
2014-12-01 18:16:47 -05:00
tmp swap heap 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-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
2014-12-01 18:16:47 -05:00
:: sift-up ( heap n -- )
heap heap-size :> end
n heap data-nth :> tmp
2014-12-01 18:16:47 -05:00
n dup left [ dup end < ] [
dup 1 fixnum+fast
dup end < [ 2dup heap data-compare ] [ f ] if
[ nip ] [ drop ] if
[ heap data-nth swap heap data-set-nth ]
[ dup left ] bi
] while drop
2007-11-02 15:41:19 -04:00
2014-12-01 18:16:47 -05:00
tmp over heap data-set-nth
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>> dup length 1 > [
[ pop ] [ set-first ] bi 0 sift-up
] [
pop* drop
] if ; inline
M: heap heap-pop
[ data-first >entry< ] [ heap-pop* ] bi ;
: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
[ check-heap ] dip
[ 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 )
2015-08-13 06:20:39 -04:00
over heap>> eq? [ throw-bad-heap-delete ] unless
2013-03-06 22:41:37 -05:00
index>> { 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>index ] keep
2dup heap-size 1 - = [
nip data>> pop*
2007-11-02 15:41:19 -04:00
] [
[ nip data>> pop ]
[ data-set-nth ]
2014-12-01 18:16:47 -05:00
[ swap sift-up ] 2tri
] if ;
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 ;