Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
22
Task/Queue-Definition/Forth/queue-definition-1.fth
Normal file
22
Task/Queue-Definition/Forth/queue-definition-1.fth
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
1024 constant size
|
||||
create buffer size cells allot
|
||||
here constant end
|
||||
variable head buffer head !
|
||||
variable tail buffer tail !
|
||||
variable used 0 used !
|
||||
|
||||
: empty? used @ 0= ;
|
||||
: full? used @ size = ;
|
||||
|
||||
: next ( ptr -- ptr )
|
||||
cell+ dup end = if drop buffer then ;
|
||||
|
||||
: put ( n -- )
|
||||
full? abort" buffer full"
|
||||
\ begin full? while pause repeat
|
||||
tail @ ! tail @ next tail ! 1 used +! ;
|
||||
|
||||
: get ( -- n )
|
||||
empty? abort" buffer empty"
|
||||
\ begin empty? while pause repeat
|
||||
head @ @ head @ next head ! -1 used +! ;
|
||||
42
Task/Queue-Definition/Forth/queue-definition-2.fth
Normal file
42
Task/Queue-Definition/Forth/queue-definition-2.fth
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
0
|
||||
field: list-next
|
||||
field: list-val
|
||||
constant list-struct
|
||||
|
||||
: insert ( x list-addr -- )
|
||||
list-struct allocate throw >r
|
||||
swap r@ list-val !
|
||||
dup @ r@ list-next !
|
||||
r> swap ! ;
|
||||
|
||||
: remove ( list-addr -- x )
|
||||
>r r@ @ ( list-node )
|
||||
r@ @ dup list-val @ ( list-node x )
|
||||
swap list-next @ r> !
|
||||
swap free throw ;
|
||||
|
||||
0
|
||||
field: queue-last \ points to the last entry (head of the list)
|
||||
field: queue-nextaddr \ points to the pointer to the next-inserted entry
|
||||
constant queue-struct
|
||||
|
||||
: init-queue ( queue -- )
|
||||
>r 0 r@ queue-last !
|
||||
r@ queue-last r> queue-nextaddr ! ;
|
||||
|
||||
: make-queue ( -- queue )
|
||||
queue-struct allocate throw dup init-queue ;
|
||||
|
||||
: empty? ( queue -- f )
|
||||
queue-last @ 0= ;
|
||||
|
||||
: enqueue ( x queue -- )
|
||||
dup >r queue-nextaddr @ insert
|
||||
r@ queue-nextaddr @ @ list-next r> queue-nextaddr ! ;
|
||||
|
||||
: dequeue ( queue -- x )
|
||||
dup empty? abort" dequeue applied to an empty queue"
|
||||
dup queue-last remove ( queue x )
|
||||
over empty? if
|
||||
over init-queue then
|
||||
nip ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue