Queue/Definition: Difference between revisions
Content added Content deleted
(Update code for recent versions of Julia) |
|||
Line 4,435: | Line 4,435: | ||
=={{header|OxygenBasic}}== |
=={{header|OxygenBasic}}== |
||
This buffer pushes any primitive data (auto converted to strings), and pops strings. The buffer can expand or contract according to usage. |
This buffer pushes any primitive data (auto converted to strings), and pops strings. The buffer can expand or contract according to usage. |
||
<lang |
<lang> |
||
⚫ | |||
'========== |
'========== |
||
Class Queue |
Class Queue |
||
'========== |
'========== |
||
⚫ | |||
string buf |
|||
sys bg,i,le |
|||
bstring buf 'buffer to hold queue content |
|||
int bg 'buffer base offset |
|||
int i 'indexer |
|||
int le 'length of buffer |
|||
method |
method constructor() |
||
==================== |
|||
buf="" |
|||
le=0 |
|||
bg=0 |
|||
i=0 |
|||
end method |
|||
method destructor() |
|||
=================== |
|||
del buf |
|||
le=0 |
|||
bg=0 |
|||
i=0 |
|||
end method |
|||
method Encodelength(int ls) |
|||
=========================== |
|||
int p at (i+strptr buf) |
int p at (i+strptr buf) |
||
p=ls |
p=ls |
||
i+=sizeof int |
i+=sizeof int |
||
end method |
end method |
||
method push(string s) |
method push(string s) |
||
===================== |
|||
ls=len s |
int ls=len s |
||
if i+ls+8>le then |
if i+ls+8>le then |
||
buf+=nuls 8000+ls*2 |
buf+=nuls 8000+ls*2 'extend buf |
||
le=len buf |
|||
end if |
end if |
||
EncodeLength ls |
EncodeLength ls 'length of input s |
||
mid buf,i+1,s |
mid buf,i+1,s 'append input s |
||
i+=ls |
i+=ls |
||
'EncodeLength ls |
|||
end method |
end method |
||
method |
method popLength() as int |
||
========================= |
|||
if bg>=i then return -1 'buffer empty |
|||
int p at (bg+strptr buf) |
|||
bg+ |
int p at (bg+strptr buf) |
||
bg+=sizeof int |
|||
return p |
|||
end method |
end method |
||
method pop(string *s) as |
method pop(string *s) as int |
||
============================ |
|||
sys ls=GetLength |
|||
int ls=popLength |
|||
if ls<0 then s="" : return ls 'empty buffer |
if ls<0 then s="" : return ls 'empty buffer |
||
s=mid buf,bg+1,ls |
s=mid buf,bg+1,ls |
||
bg+=ls |
bg+=ls |
||
'cleanup buffer |
|||
if bg>1e6 then |
if bg>1e6 then |
||
buf=mid buf,bg+1 |
buf=mid buf,bg+1 |
||
le=len buf |
|||
i-=bg 'shrink buf |
|||
bg=0 |
|||
end if |
end if |
||
end method |
end method |
||
method clear() |
method clear() |
||
============== |
|||
buf="" : le="" : bg=0 : i=0 |
|||
buf="" |
|||
le=0 |
|||
bg=0 |
|||
i=0 |
|||
end method |
end method |
||
⚫ | |||
⚫ | |||
'==== |
'==== |
||
'DEMO |
|||
'TEST |
|||
'==== |
'==== |
||
Queue fifo |
new Queue fifo |
||
string s |
string s |
||
' |
' |
||
Line 4,495: | Line 4,527: | ||
fifo.push "Sat on a wall" |
fifo.push "Sat on a wall" |
||
' |
' |
||
int er |
|||
do |
do |
||
er=fifo.pop s |
er=fifo.pop s |
||
if er then print "(buffer empty)" : exit do |
if er then print "(buffer empty)" : exit do |
||
print s |
print s |
||
loop |
|||
end do |
|||
⚫ | |||
del fifo |
|||
⚫ | |||
=={{header|Oz}}== |
=={{header|Oz}}== |