66 lines
932 B
Plaintext
66 lines
932 B
Plaintext
'FIRST IN FIRST OUT
|
|
|
|
'==========
|
|
Class Queue
|
|
'==========
|
|
|
|
string buf
|
|
sys bg,i,le
|
|
|
|
method Encodelength(sys ls)
|
|
int p at (i+strptr buf)
|
|
p=ls
|
|
i+=sizeof int
|
|
end method
|
|
|
|
method push(string s)
|
|
ls=len s
|
|
if i+ls+8>le then
|
|
buf+=nuls 8000+ls*2 : le=len buf 'expand buf
|
|
end if
|
|
EncodeLength ls
|
|
mid buf,i+1,s
|
|
i+=ls
|
|
'EncodeLength ls
|
|
end method
|
|
|
|
method GetLength() as sys
|
|
if bg>=i then return -1 'buffer empty
|
|
int p at (bg+strptr buf)
|
|
bg+=sizeof int
|
|
return p
|
|
end method
|
|
|
|
method pop(string *s) as sys
|
|
sys ls=GetLength
|
|
if ls<0 then s="" : return ls 'empty buffer
|
|
s=mid buf,bg+1,ls
|
|
bg+=ls
|
|
if bg>1e6 then
|
|
buf=mid buf,bg+1 : bg=0 : le=len buf : i-=bg 'shrink buf
|
|
end if
|
|
end method
|
|
|
|
method clear()
|
|
buf="" : le="" : bg=0 : i=0
|
|
end method
|
|
|
|
end class
|
|
|
|
'====
|
|
'TEST
|
|
'====
|
|
|
|
Queue fifo
|
|
string s
|
|
'
|
|
fifo.push "HumptyDumpty"
|
|
fifo.push "Sat on a wall"
|
|
'
|
|
sys er
|
|
do
|
|
er=fifo.pop s
|
|
if er then print "(buffer empty)" : exit do
|
|
print s
|
|
end do
|