LuaでQueue

提供: MonoBook
2018年8月15日 (水) 06:01時点におけるimported>Administratorによる版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

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