This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
deque.lua
executable file
·162 lines (121 loc) · 2.94 KB
/
deque.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
local deque = {}
local construct =
function (_, ...)
local o = {}
-- piggyback this to 'init' our deque
deque.clear(o)
for _, v in ipairs({ ... }) do
deque.push_back(o, v)
end
return setmetatable(o, deque)
end
deque.empty =
function (self)
local empty = self._back < self._front
if empty then
deque.clear(self)
end
return empty
end
deque.pop_front =
function (self)
if deque.empty(self) then return end
local i = self._items[self._front]
self._items[self._front] = nil -- GC this
-- push this back
self._front = self._front + 1
return i
end
deque.pop_back =
function (self)
if deque.empty(self) then return end
local i = self._items[self._back]
self._items[self._back] = nil -- GC this
-- bring this in
self._back = self._back - 1
return i
end
deque.push_front =
function (self, i)
self._front = self._front - 1
self._items[self._front] = i
end
deque.push_back =
function (self, i)
self._back = self._back + 1
self._items[self._back] = i
end
deque.clear =
function (self)
self._front = 1
self._back = 0
self._items = {}
end
-- not sure anybody will use this...
deque.swap =
function (self, other)
-- swappy swappy!
self._front, other._front = other._front, self._front
self._back, other._back = other._back, self._back
self._items, other._items = other._items, self._items
end
-- expensive: avoid inserting things in the middle of queues
deque.insert =
function (self, ...)
if select('#', ...) == 1 then
deque.push_back(self, ...)
else
local i, v = ...
local idx = self._front + i - 1
self._back = self._back + 1
for x = self._back, idx, -1 do
self._items[x] = self._items[x - 1] -- move this "up"
end
self._items[idx] = v
end
end
-- expensive: avoid deleting things in the middle of queues
deque.erase =
function (self, ...)
local i, n = ...
if select('#', ...) == 1 then
n = 1
end
self._back = self._back - n
for x = self._front + i - 1, self._back do
self._items[x ] = self._items[x + n]
self._items[x + n] = nil -- GC teim! \o/
end
end
deque.length = function (self) return self._back - self._front + 1 end
deque.front = function (self) return self._items[self._front ] end
deque.back = function (self) return self._items[self._back ] end
deque.at = function (self, i) return self._items[self._front + i - 1] end
-- metamethods
deque.__call = construct -- local d = deque('a', 'b', 'c')
deque.__index = deque -- d:empty() -> false
deque.__len = deque.length -- #d -> 3
deque.__pairs =
function (self)
local k, v
return
function ()
k, v = next(self._items, k)
if k ~= nil then
return k - self._front + 1, v
end
end
end
deque.__ipairs =
function (self)
local i = self._front - 1
return
function ()
i = i + 1
local v = self._items[i]
if v ~= nil then
return i, v
end
end
end
return setmetatable(deque, deque)