Jump to content

Queue/Definition: Difference between revisions

(Update code for recent versions of Julia)
Line 4,435:
=={{header|OxygenBasic}}==
This buffer pushes any primitive data (auto converted to strings), and pops strings. The buffer can expand or contract according to usage.
<lang oxygenbasic>
'FIRST IN FIRST OUT
 
'==========
Class Queue
'==========
 
'FIRST IN FIRST OUT
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 Encodelengthconstructor(sys ls)
====================
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)
p=ls
i+=sizeof int
end method
 
method push(string s)
=====================
int ls=len s
if i+ls+8>le then
buf+=nuls 8000+ls*2 : le=len buf 'expandextend buf
le=len buf
end if
EncodeLength ls 'length of input s
mid buf,i+1,s 'append input s
i+=ls
'EncodeLength ls
end method
 
method GetLengthpopLength() as sysint
=========================
if bg>=i then return -1 'buffer empty
int p at (bg+strptr buf)
int p at (bg+=sizeofstrptr intbuf)
bg+=sizeof int
return p
end method
 
method pop(string *s) as sysint
============================
sys ls=GetLength
int ls=popLength
if ls<0 then s="" : return ls 'empty buffer
s=mid buf,bg+1,ls
bg+=ls
'cleanup buffer
if bg>1e6 then
buf=mid buf,bg+1 : bg=0 : le=len buf : i-=bg 'shrink buf
le=len buf
i-=bg 'shrink buf
bg=0
end if
end method
 
method clear()
==============
buf="" : le="" : bg=0 : i=0
buf=""
le=0
bg=0
i=0
end method
end class 'Queue
 
end class
 
'====
'DEMO
'TEST
'====
 
new Queue fifo
string s
'
Line 4,495 ⟶ 4,527:
fifo.push "Sat on a wall"
'
sysint er
do
er=fifo.pop s
if er then print "(buffer empty)" : exit do
print s
loop
end do
 
</lang>
del fifo
</lang>
 
=={{header|Oz}}==
54

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.