差分
ナビゲーションに移動
検索に移動
ページの作成:「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>
-- 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>