     [ dup heap-pop swap 2array ]
     produce nip ;
 
+ERROR: not-a-heap obj ;
+
+: check-heap ( heap -- heap )
+    dup heap? [ not-a-heap ] unless ; inline
+
 : slurp-heap ( heap quot: ( elt -- ) -- )
-    over heap-empty? [ 2drop ] [
+    [ check-heap ] dip over heap-empty? [ 2drop ] [
         [ [ heap-pop drop ] dip call ] [ slurp-heap ] 2bi
     ] if ; inline recursive


ERROR: bad-heap-delete ;

M: bad-heap-delete summary 
    drop "Invalid entry passed to heap-delete" ;

: entry>index ( entry heap -- n )
    over heap>> eq? [ bad-heap-delete ] unless
    index>> ;

M: heap heap-delete ( entry heap -- )
    [ entry>index ] [ ] bi

! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
! Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators fry growable kernel
kernel.private math math.order math.private sequences
sequences.private summary vectors ;

IN: heaps

GENERIC: heap-push* ( value key heap -- entry )
