From 3f3a6ea1f772b2a288276692131dca7705d218a2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 22 Feb 2008 16:16:00 -0600 Subject: [PATCH] Fix alarms with image saves --- core/heaps/heaps-tests.factor | 4 ++-- core/heaps/heaps.factor | 14 ++++++++++---- extra/alarms/alarms.factor | 13 ++++++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/core/heaps/heaps-tests.factor b/core/heaps/heaps-tests.factor index ce9a417476..f199ba8837 100755 --- a/core/heaps/heaps-tests.factor +++ b/core/heaps/heaps-tests.factor @@ -15,8 +15,8 @@ IN: temporary ! Binary Min Heap { 1 2 3 4 5 6 } [ 0 left 0 right 1 left 1 right 2 left 2 right ] unit-test -{ t } [ t 5 t 3 T{ min-heap } heap-compare ] unit-test -{ f } [ t 5 t 3 T{ max-heap } heap-compare ] unit-test +{ t } [ t 5 f t 3 f T{ min-heap } heap-compare ] unit-test +{ f } [ t 5 f t 3 f T{ max-heap } heap-compare ] unit-test [ t 2 ] [ t 300 pick heap-push t 200 pick heap-push t 400 pick heap-push t 3 pick heap-push t 2 pick heap-push heap-pop ] unit-test diff --git a/core/heaps/heaps.factor b/core/heaps/heaps.factor index 158e298631..caab0d8f8e 100755 --- a/core/heaps/heaps.factor +++ b/core/heaps/heaps.factor @@ -22,9 +22,9 @@ GENERIC: heap-size ( heap -- n ) : ( class -- heap ) >r V{ } clone r> construct-delegate ; inline -TUPLE: entry value key index ; +TUPLE: entry value key heap index ; -: ( value key -- entry ) f entry construct-boa ; +: ( value key heap -- entry ) f entry construct-boa ; PRIVATE> @@ -153,7 +153,7 @@ DEFER: down-heap PRIVATE> M: priority-queue heap-push* ( value key heap -- entry ) - >r dup r> [ data-push ] keep up-heap ; + [ dup ] keep [ data-push ] keep up-heap ; : heap-push ( value key heap -- ) heap-push* drop ; @@ -166,8 +166,14 @@ M: priority-queue heap-push* ( value key heap -- entry ) M: priority-queue heap-peek ( heap -- value key ) data-first >entry< ; +: entry>index ( entry heap -- n ) + over entry-heap eq? [ + "Invalid entry passed to heap-delete" throw + ] unless + entry-index ; + M: priority-queue heap-delete ( entry heap -- ) - >r entry-index r> + [ entry>index ] keep 2dup heap-size 1- = [ nip data-pop* ] [ diff --git a/extra/alarms/alarms.factor b/extra/alarms/alarms.factor index 7cac654b60..bbc20ea981 100755 --- a/extra/alarms/alarms.factor +++ b/extra/alarms/alarms.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: arrays calendar combinators generic init kernel math -namespaces sequences heaps boxes threads debugger quotations ; +namespaces sequences heaps boxes threads debugger quotations +assocs ; IN: alarms TUPLE: alarm quot time interval entry ; @@ -67,8 +68,13 @@ SYMBOL: alarm-thread dup trigger-alarms alarm-thread-loop ; +: cancel-alarms ( alarms -- ) + [ + heap-pop-all [ nip alarm-entry box> drop ] assoc-each + ] when* ; + : init-alarms ( -- ) - alarms set-global + alarms global [ cancel-alarms ] change-at [ alarm-thread-loop ] "Alarms" spawn alarm-thread set-global ; @@ -83,4 +89,5 @@ PRIVATE> from-now f add-alarm ; : cancel-alarm ( alarm -- ) - alarm-entry box> alarms get-global heap-delete ; + alarm-entry ?box + [ alarms get-global heap-delete ] [ drop ] if ;