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 oxygenbasic>
<lang>
'FIRST IN FIRST OUT

'==========
'==========
Class Queue
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 Encodelength(sys ls)
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 : le=len buf 'expand buf
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 GetLength() as sys
method popLength() as int
=========================
if bg>=i then return -1 'buffer empty
if bg>=i then return -1 'buffer empty
int p at (bg+strptr buf)
bg+=sizeof int
int p at (bg+strptr buf)
bg+=sizeof int
return p
return p
end method
end method

method pop(string *s) as sys
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 : bg=0 : le=len buf : i-=bg 'shrink buf
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
end class 'Queue


end class


'====
'====
'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"
'
'
sys er
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

</lang>
del fifo
</lang>


=={{header|Oz}}==
=={{header|Oz}}==