         position-out-of-bounds
     ] if ;
 
-TUPLE: index-out-of-bounds index gap-buffer ;
-C: <index-out-of-bounds> index-out-of-bounds
+: valid-index? ( i gb -- ? )
+    buffer-length -1 swap between? ;
+
+ERROR: index-out-of-bounds index gap-buffer ;
 
 : index>position ( i gb -- pos )
     2dup valid-index? [

: increment-gap-start ( gb n -- )
    over gap-start>> + swap gap-start<< ;

! generic dispatch identifies numbers as sequences before numbers...
M: number insert* ( elem position gb -- ) [ 1array ] 2dip insert* ;
! : number-insert ( num position gb -- ) [ 1array ] 2dip insert* ;

M: sequence insert* ( seq position gb -- )
    prepare-insert [ insert-elements ] 2keep swap length increment-gap-start ;

: (delete*) ( gb -- )
    dup gap-end>> 1 + over gap-end<< ?decrease ;

GENERIC: delete* ( pos gb -- )

M: gb delete* ( position gb -- )
    tuck move-gap (delete*) ;

! -------- stack/queue operations -----------



: push-end ( obj gb -- ) [ length ] keep insert* ;

: pop-elem ( pos gb -- elem ) [ nth ] 2keep delete* ;

: pop-start ( gb -- elem ) 0 swap pop-elem ;

