「LuaでQueue」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator (ページの作成:「LuaでQueueを使いたい。<syntaxhighlight lang="lua"> -- Queueクラス Queue = {} function Queue.new(max) local obj = { _buff = {}, _count = 0, _max = max or 0 }...」) |
imported>Administrator |
||
37行目: | 37行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | [[category: Lua]] |
2018年8月15日 (水) 06:01時点における最新版
LuaでQueueを使いたい。
-- 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