heaps: simplify by using data vector directly.
parent
5ad9c55f47
commit
0d7233f2da
|
@ -9,7 +9,6 @@ IN: heaps
|
||||||
GENERIC: heap-push* ( value key heap -- entry )
|
GENERIC: heap-push* ( value key heap -- entry )
|
||||||
GENERIC: heap-peek ( heap -- value key )
|
GENERIC: heap-peek ( heap -- value key )
|
||||||
GENERIC: heap-pop* ( heap -- )
|
GENERIC: heap-pop* ( heap -- )
|
||||||
GENERIC: heap-pop ( heap -- value key )
|
|
||||||
GENERIC: heap-delete ( entry heap -- )
|
GENERIC: heap-delete ( entry heap -- )
|
||||||
GENERIC: heap-empty? ( heap -- ? )
|
GENERIC: heap-empty? ( heap -- ? )
|
||||||
GENERIC: heap-size ( heap -- n )
|
GENERIC: heap-size ( heap -- n )
|
||||||
|
@ -53,18 +52,15 @@ M: heap heap-size ( heap -- n )
|
||||||
: up ( n -- m )
|
: up ( n -- m )
|
||||||
{ fixnum } declare 1 fixnum-fast 2/ ; inline
|
{ fixnum } declare 1 fixnum-fast 2/ ; inline
|
||||||
|
|
||||||
: data-nth ( n heap -- entry )
|
: data-nth ( n data -- entry )
|
||||||
data>> nth-unsafe { entry } declare ; inline
|
nth-unsafe { entry } declare ; inline
|
||||||
|
|
||||||
: data-first ( heap -- entry )
|
: data-set-nth ( entry n data -- )
|
||||||
data>> first ; inline
|
[ [ >>index ] keep ] dip set-nth-unsafe ; inline
|
||||||
|
|
||||||
: data-set-nth ( entry n heap -- )
|
: data-push ( entry data -- n )
|
||||||
[ [ >>index ] keep ] dip data>> set-nth-unsafe ; inline
|
[ length [ >>index ] keep ]
|
||||||
|
[ [ set-nth ] 2keep drop ] bi ; inline
|
||||||
: data-push ( entry heap -- n )
|
|
||||||
[ heap-size [ >>index ] keep ]
|
|
||||||
[ data>> [ set-nth ] 2keep drop ] bi ; inline
|
|
||||||
|
|
||||||
GENERIC: heap-compare ( entry1 entry2 heap -- ? )
|
GENERIC: heap-compare ( entry1 entry2 heap -- ? )
|
||||||
|
|
||||||
|
@ -74,38 +70,36 @@ M: min-heap heap-compare
|
||||||
M: max-heap heap-compare
|
M: max-heap heap-compare
|
||||||
drop { entry entry } declare [ key>> ] bi@ before? ; inline
|
drop { entry entry } declare [ key>> ] bi@ before? ; inline
|
||||||
|
|
||||||
: data-compare ( m n heap -- ? )
|
|
||||||
[ '[ _ data-nth ] bi@ ] [ heap-compare ] bi ; inline
|
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: >entry< ( entry -- value key )
|
: >entry< ( entry -- value key )
|
||||||
[ value>> ] [ key>> ] bi ; inline
|
[ value>> ] [ key>> ] bi ; inline
|
||||||
|
|
||||||
M: heap heap-peek ( heap -- value key )
|
M: heap heap-peek ( heap -- value key )
|
||||||
data-first >entry< ;
|
data>> first >entry< ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
:: sift-down ( heap from to -- )
|
:: sift-down ( heap from to -- )
|
||||||
to heap data-nth :> tmp
|
heap data>> :> data
|
||||||
|
to data data-nth :> tmp
|
||||||
|
|
||||||
to t [ over from > and ] [
|
to t [ over from > and ] [
|
||||||
dup up
|
dup up
|
||||||
dup heap data-nth
|
dup data data-nth
|
||||||
dup tmp heap heap-compare [
|
dup tmp heap heap-compare [
|
||||||
rot heap data-set-nth t
|
rot data data-set-nth t
|
||||||
] [
|
] [
|
||||||
2drop f
|
2drop f
|
||||||
] if
|
] if
|
||||||
] while
|
] while
|
||||||
|
|
||||||
tmp swap heap data-set-nth ; inline
|
tmp swap data data-set-nth ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
M: heap heap-push*
|
M: heap heap-push*
|
||||||
[ <entry> dup ] [ data-push ] [ 0 rot sift-down ] tri ;
|
[ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
|
||||||
|
|
||||||
: heap-push ( value key heap -- )
|
: heap-push ( value key heap -- )
|
||||||
heap-push* drop ;
|
heap-push* drop ;
|
||||||
|
@ -116,18 +110,21 @@ M: heap heap-push*
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
:: sift-up ( heap n -- )
|
:: sift-up ( heap n -- )
|
||||||
heap heap-size :> end
|
heap data>> :> data
|
||||||
n heap data-nth :> tmp
|
data length :> end
|
||||||
|
n data data-nth :> tmp
|
||||||
|
|
||||||
n dup left [ dup end < ] [
|
n dup left [ dup end < ] [
|
||||||
dup 1 fixnum+fast
|
dup 1 fixnum+fast
|
||||||
dup end < [ 2dup heap data-compare ] [ f ] if
|
dup end < [
|
||||||
|
2dup [ data data-nth ] bi@ heap heap-compare
|
||||||
|
] [ f ] if
|
||||||
[ nip ] [ drop ] if
|
[ nip ] [ drop ] if
|
||||||
[ heap data-nth swap heap data-set-nth ]
|
[ data data-nth swap data data-set-nth ]
|
||||||
[ dup left ] bi
|
[ dup left ] bi
|
||||||
] while drop
|
] while drop
|
||||||
|
|
||||||
tmp over heap data-set-nth
|
tmp over data data-set-nth
|
||||||
heap n rot sift-down ; inline
|
heap n rot sift-down ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
@ -139,8 +136,8 @@ M: heap heap-pop*
|
||||||
pop* drop
|
pop* drop
|
||||||
] if ; inline
|
] if ; inline
|
||||||
|
|
||||||
M: heap heap-pop
|
: heap-pop ( heap -- value key )
|
||||||
[ data-first >entry< ] [ heap-pop* ] bi ;
|
[ heap-peek ] [ heap-pop* ] bi ;
|
||||||
|
|
||||||
: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
|
: slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
|
||||||
[ drop '[ _ heap-empty? ] ]
|
[ drop '[ _ heap-empty? ] ]
|
||||||
|
@ -169,7 +166,7 @@ M: heap heap-delete
|
||||||
nip data>> pop*
|
nip data>> pop*
|
||||||
] [
|
] [
|
||||||
[ nip data>> pop ]
|
[ nip data>> pop ]
|
||||||
[ data-set-nth ]
|
[ data>> data-set-nth ]
|
||||||
[ swap sift-up ] 2tri
|
[ swap sift-up ] 2tri
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue