-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathQueue.lua
51 lines (43 loc) · 1.02 KB
/
Queue.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- Lua好挫逼啊,队列这种基础数据结构也要自己撸
-- 不用C++的next指针形式。(都是基于lua表查找,达不到指针的性能)
-- FIFO队列
require("PreLoad")
local Queue = class("Queue")
function Queue:ctor()
self._data = {}
self._tailIndex = 1
self._headIndex = 1
end
function Queue:IsEmpty()
return self._headIndex == self._tailIndex
end
function Queue:NotEmpty()
return self._headIndex ~= self._tailIndex
end
function Queue:Enqueue(item)
self._headIndex = self._headIndex + 1
self._data[self._headIndex] = item
end
function Queue:Dequeue()
if self._headIndex == self._tailIndex then
return nil
else
self._tailIndex = self._tailIndex + 1
return self._data[self._tailIndex]
end
end
-- local q = Queue.New()
-- print(q:IsEmpty())
-- q:Enqueue("w")
-- q:Enqueue("h")
-- print(q:IsEmpty())
-- print(q:Dequeue())
-- q:Enqueue("a")
-- q:Enqueue("t")
-- print(q:Dequeue())
-- print(q:Dequeue())
-- print(q:IsEmpty())
-- print(q:Dequeue())
-- print(q:Dequeue())
-- print(q:IsEmpty())
return Queue