Paste: limited-list

Author: kobi
Mode: factor
Date: Sat, 23 Oct 2010 08:16:01
Plain Text |
USING: accessors kernel math slots.syntax ;
QUALIFIED-WITH: binary-tree bst
IN: limited-list

TUPLE: limited-list btree { limit integer } { count integer initial: 0 } ;

: empty? ( limited-list -- ? )
    btree>> >boolean not ;   ! has binary-tree?
: vacancy? ( limited-list -- ? )
    slots[ count limit ] < ;
: inc ( limited-list -- limited-list' )
    [ 1 + ] change-count ;

GENERIC: belongs? ( node limited-list -- ? )
GENERIC: update ( limited-list -- )    
    
! possible additions    
! --------------
DEFER: update
: add-first-node ( node limited-list -- )
    inc swap bst:<binary-tree> >>btree update ;

: regular-add ( node limited-list -- )
    inc btree>> bst:add-node ;
    
: limited-add ( node limited-list -- )
    regular-add update ;
! --------------    
    

: add-node ( node limited-list -- )
    dup empty? [ add-first-node ]
    [ dup vacancy? [ regular-add ] 
        [   ! limit reached: dont add if value >= max.
            2dup belongs? [ limited-add ] [ 2drop ] if
        ] if 
    ]  if ;
    
    
: add ( value limited-list -- )
    [ bst:<node> ] dip add-node ;

Annotation: oops - now removing the border value

Author: kobi
Mode: factor
Date: Sat, 23 Oct 2010 11:52:29
Plain Text |
GENERIC: remove-border ( limited-list -- )
    

: limited-add ( node limited-list -- )
    dup remove-border
    regular-add update-borders ;

New Annotation

Summary:
Author:
Mode:
Body: