メインメニューを開く

差分

LuaでQueue

685 バイト追加, 2018年8月15日 (水) 06:00
ページの作成:「LuaでQueueを使いたい。<syntaxhighlight lang="lua"> -- Queueクラス Queue = {} function Queue.new(max) local obj = { _buff = {}, _count = 0, _max = max or 0 }...」
LuaでQueueを使いたい。<syntaxhighlight lang="lua">
-- Queueクラス
Queue = {}

function Queue.new(max)
local obj = { _buff = {}, _count = 0, _max = max or 0 }
return setmetatable(obj, {__index = Queue})
end

function Queue:enqueue(x)
if self._count < self._max then
table.insert(self._buff, x)
self._count = self._count + 1
end
end

function Queue:dequeue()
if self._count <= 0 then
return nil
else
self._count = self._count - 1
return table.remove(self._buff, 1)
end
end

function Queue:isEmpty()
return self._count <= 0
end

function Queue:count()
return self._count
end

function Queue:max()
return self._max
end

</syntaxhighlight>
匿名利用者