\ Object lists--adding and searching vandys
only extensions definitions
Collection -> subclass: List ivars:
intcell nelem
intcell elems endivars
List -> :method add { w self -- } self List>nelem inc
self List>elems @ self List>nelem @ cells bkrealloc self List>elems !
w self List>nelem @ 1- cells self List>elems @ + ! method;
List -> :method indexOf { w self -- n T | F } self List>elems @
self List>nelem @ 0 ?do @+ w = if drop i unloop true exit then
loop drop false method;
\ Object lists--adding, testing, removing, iteration vandys
List -> :method remove { w self -- }
w self -> indexOf 0= abort" Not in list"
cells { idx } self List>nelem dec self List>nelem @ cells idx -
{ #move } self List>elems @ idx + { dest }
dest cell+ ( src ) dest #move move method;
List -> :method in? ( w self -- ? )
-> indexOf ?dup if nip then method;
List -> :method size ( self -- u ) List>nelem @ method;
List -> :method do { arg fn self -- }
self List>elems @ self List>nelem @ 0 ?do
( 'elem ) @+ arg swap fn execute loop drop method;
List -> :method reverseDo { arg fn self -- }
self List>nelem @ ?dup 0= if exit then ( nelem )
self List>elems @ 0 rot 1- do
arg over i cells + @ fn execute -1 +loop drop method;
\ Object lists--basic cell access vandys
: (range) ( idx list -- ) List>nelem @ >= abort" Bad index" ; scrLocal
List -> :method @ { idx self -- val } idx dup self (range)
cells self List>elems @ + @ method;
List -> :method ! { val idx self -- } val idx dup self (range)
cells self List>elems @ + ! method;
\ Object lists--cleanup, display vandys
List -> :method free ( self -- ) dup List>elems @ bkfree
super-> free method;
List -> :method empty! ( self -- ) List>nelem off method;
List -> :method size! ( nelem self -- )
2dup List>nelem @ > abort" Growing List"
over cells over List>elems @ swap bkrealloc ( nelem self elems' )
over List>elems ! List>nelem ! method;
\ Object lists--creation utilities vandys
: args>list ( args... narg -- list ) List -> new
swap 0 do tuck -> add loop ;
vandys
A List object
# of values in the List
The actual array of values
:method add Add the value to the list of values
:method indexOf Return index of value and true, else just false
vandys
:method remove Remove cell value from list
:method in? Tell if cell value is in list
:method size Tell how many entries are in a list
:method do Iterate across entries in list
:method reverseDo Iterate in reverse order
vandys
: (range) Common code for a range check
:method @ Fetch value from given index in list
:method ! Store value to given index of list
vandys
:method deinit Free the dynamic memory hanging off the list, then let our
superclass clean up the rest
:method empty! Remove all contents from List
:method List:size! Set the size of the List; it's illegal to try
and grow it. For shrinking we even go to the trouble telling the
bucket allocator about the new size.
vandys
: args>list Convert a counted # of arguments on the operand stack
into a List of those arguments