Queue/Definition: Difference between revisions
→{{header|OxygenBasic}}
(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
'FIRST IN FIRST OUT▼
'==========
Class Queue
'==========
▲ 'FIRST IN FIRST OUT
bstring buf 'buffer to hold queue content
int bg 'buffer base offset
int i 'indexer
int le 'length of buffer
method
====================
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
end if
EncodeLength ls 'length of input s
mid buf,i+1,s 'append input s
i+=ls
end method
method
=========================
int p at (bg+
bg+=sizeof int
end method
method pop(string *s) as
============================
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
le=len buf
i-=bg 'shrink buf
bg=0
end if
end method
method clear()
==============
buf=""
le=0
bg=0
i=0
end method
end class 'Queue▼
▲end class
'====
'DEMO
'====
new Queue fifo
string s
'
Line 4,495 ⟶ 4,527:
fifo.push "Sat on a wall"
'
do
er=fifo.pop s
if er then print "(buffer empty)" : exit do
print s
loop
</lang>▼
del fifo
▲ </lang>
=={{header|Oz}}==
|