factor/basis/heaps/heaps.factor

202 lines
4.7 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.
2013-03-06 22:41:37 -05:00
USING: accessors arrays assocs combinators fry growable kernel
kernel.private 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 )
2008-11-29 14:28:52 -05:00
[ V{ } clone ] dip boa ; 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
: left-value ( n heap -- entry )
2008-11-29 14:28:52 -05:00
[ left ] dip data-nth ; inline
2008-02-21 15:16:22 -05:00
: right-value ( n heap -- entry )
2008-11-29 14:28:52 -05:00
[ right ] dip data-nth ; inline
2008-02-21 15:16:22 -05:00
: data-set-nth ( entry n heap -- )
[ [ swap index<< ] 2keep ] dip
2008-11-12 00:04:30 -05:00
data>> set-nth-unsafe ; inline
2008-02-21 15:16:22 -05:00
: data-push ( entry heap -- n )
dup heap-size [
swap
[ data>> ensure 2drop ]
[ data-set-nth ] 2bi
] keep ; inline
2008-02-21 15:16:22 -05:00
: data-first ( heap -- entry )
2008-04-04 01:33:06 -04:00
data>> first ; inline
2008-02-21 15:16:22 -05:00
: data-exchange ( m n heap -- )
[ '[ _ data-nth ] bi@ ]
[ '[ _ data-set-nth ] bi@ ] 3bi ; 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
: heap-bounds-check? ( m heap -- ? )
2008-02-21 15:16:22 -05:00
heap-size >= ; inline
2007-11-05 12:35:44 -05:00
2007-11-02 15:41:19 -04:00
: left-bounds-check? ( m heap -- ? )
2008-11-29 14:28:52 -05:00
[ left ] dip heap-bounds-check? ; inline
2007-11-02 15:41:19 -04:00
: right-bounds-check? ( m heap -- ? )
2008-11-29 14:28:52 -05:00
[ right ] dip heap-bounds-check? ; inline
: continue? ( m n heap -- ? )
[ data-nth nip ]
[ nip data-nth ]
2012-09-14 13:03:30 -04:00
[ 2nip ] 3tri heap-compare ; inline
DEFER: up-heap
: (up-heap) ( n heap -- )
2008-11-29 14:28:52 -05:00
[ dup up ] dip
3dup continue? [
[ data-exchange ] [ up-heap ] 2bi
2007-11-02 15:41:19 -04:00
] [
3drop
2009-07-18 01:52:24 -04:00
] if ; inline recursive
2007-11-02 15:41:19 -04:00
: up-heap ( n heap -- )
2009-07-18 01:52:24 -04:00
over 0 > [ (up-heap) ] [ 2drop ] if ; inline recursive
: (child) ( m heap -- n )
{ [ drop ] [ left-value ] [ right-value ] [ nip ] } 2cleave
2012-09-14 13:03:30 -04:00
heap-compare [ right ] [ left ] if ; inline
2007-11-02 15:41:19 -04:00
: child ( m heap -- n )
2dup right-bounds-check?
2012-09-14 13:03:30 -04:00
[ drop left ] [ (child) ] if ; inline
2007-11-02 15:41:19 -04:00
DEFER: down-heap
: (down-heap) ( m heap -- )
[ drop ] [ child ] [ nip ] 2tri
3dup continue? [
2007-11-02 15:41:19 -04:00
3drop
] [
[ data-exchange ] [ down-heap ] 2bi
2009-07-18 01:52:24 -04:00
] if ; inline recursive
2007-11-02 15:41:19 -04:00
: down-heap ( m heap -- )
2dup left-bounds-check?
[ 2drop ] [ (down-heap) ] if ; inline recursive
2007-11-02 15:41:19 -04:00
PRIVATE>
2008-07-14 04:33:13 -04:00
M: heap heap-push* ( value key heap -- entry )
[ <entry> dup ] [ data-push ] [ up-heap ] tri ;
: heap-push ( value key heap -- ) heap-push* drop ;
2007-11-02 15:41:19 -04:00
2007-12-20 01:06:36 -05:00
: heap-push-all ( assoc heap -- )
'[ swap _ heap-push ] assoc-each ;
2007-11-02 15:41:19 -04:00
: >entry< ( entry -- value key )
2009-07-18 01:52:24 -04:00
[ value>> ] [ key>> ] bi ; inline
2008-07-14 04:33:13 -04:00
M: heap heap-peek ( heap -- value key )
data-first >entry< ;
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
2008-02-22 17:16:00 -05:00
: entry>index ( entry heap -- n )
2008-08-29 17:42:22 -04:00
over heap>> eq? [ bad-heap-delete ] unless
2013-03-06 22:41:37 -05:00
index>> { fixnum } declare ; inline
2008-02-22 17:16:00 -05:00
2008-07-14 04:33:13 -04:00
M: heap heap-delete ( entry heap -- )
[ entry>index ] keep
2dup heap-size 1 - = [
nip data>> pop*
2007-11-02 15:41:19 -04:00
] [
[ nip data>> pop ]
[ data-set-nth ]
[ down-heap ] 2tri
] if ;
2007-11-05 12:35:44 -05:00
2008-07-14 04:33:13 -04:00
M: heap heap-pop* ( heap -- )
[ data-first ] keep heap-delete ;
2008-07-14 04:33:13 -04:00
M: heap heap-pop ( heap -- value key )
[ data-first dup ] keep heap-delete >entry< ;
: heap-pop-all ( heap -- alist )
[ dup heap-empty? not ]
2014-12-01 01:30:15 -05:00
[ dup heap-pop swap 2array ]
produce nip ;
ERROR: not-a-heap object ;
2013-03-06 22:41:37 -05:00
: check-heap ( heap -- heap )
dup heap? [ not-a-heap ] unless ; inline
: slurp-heap ( heap quot: ( value key -- ) -- )
2013-03-06 22:41:37 -05:00
[ check-heap ] dip over heap-empty? [ 2drop ] [
[ [ heap-pop ] dip call ] [ slurp-heap ] 2bi
] if ; inline recursive
2013-03-31 22:01:04 -04:00
: >min-heap ( assoc -- min-heap )
<min-heap> [ heap-push-all ] keep ;
2014-06-10 20:18:37 -04:00
: >max-heap ( assoc -- max-heap )
2013-03-31 22:01:04 -04:00
<max-heap> [ heap-push-all ] keep ;