summaryrefslogtreecommitdiff
path: root/script/linked-table.lua
blob: a63a528c6b54b1754064cf2545ebc81e319f4b44 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---@class linked-table
---@field _left  table
---@field _right table
local mt = {}
mt.__index = mt
mt._size = 0

local HEAD = {'<HEAD>'}
local TAIL = {'<TAIL>'}

---@param node any
---@return boolean
function mt:has(node)
    return self._left[node] ~= nil
end

---@param node any
---@return boolean
function mt:isValidNode(node)
    if node == nil
    or node == HEAD
    or node == TAIL then
        return false
    end
    return true
end

---@param node any
---@param afterWho any
---@return boolean
function mt:pushAfter(node, afterWho)
    if not self:isValidNode(node) then
        return false
    end
    if self:has(node) then
        return false
    end
    local right = self._right[afterWho]
    if not right then
        return false
    end
    self._right[afterWho] = node
    self._right[node]     = right
    self._left[right]     = node
    self._left[node]      = afterWho

    self._size = self._size + 1
    return true
end

---@param node any
---@param beforeWho any
---@return boolean
function mt:pushBefore(node, beforeWho)
    if node == nil then
        return false
    end
    local left = self._left[beforeWho]
    if not left then
        return false
    end
    return self:pushAfter(node, left)
end

---@param node any
---@return boolean
function mt:pop(node)
    if not self:isValidNode(node) then
        return false
    end
    local left = self._left[node]
    if not left then
        return false
    end
    local right = self._right[node]
    self._right[left] = right
    self._left[right] = left

    self._right[node] = nil
    self._left[node]  = nil

    self._size = self._size - 1
    return true
end

---@param node any
---@return boolean
function mt:pushHead(node)
    return self:pushAfter(node, HEAD)
end

---@param node any
---@return boolean
function mt:pushTail(node)
    return self:pushBefore(node, TAIL)
end

---@param node any
---@return any
function mt:getAfter(node)
    if node == nil then
        node = HEAD
    end
    local right = self._right[node]
    if right == TAIL then
        return nil
    end
    return right
end

---@return any
function mt:getHead()
    return self:getAfter(HEAD)
end

---@return any
function mt:getBefore(node)
    if node == nil then
        node = TAIL
    end
    local left = self._left[node]
    if left == HEAD then
        return nil
    end
    return left
end

---@return any
function mt:getTail()
    return self:getBefore(TAIL)
end

---@return boolean
function mt:popHead()
    return self:pop(self:getHead())
end

---@return boolean
function mt:popTail()
    return self:pop(self:getTail())
end

---@param old any
---@param new any
---@return boolean
function mt:replace(old, new)
    if not self:isValidNode(old)
    or not self:isValidNode(new) then
        return false
    end
    local left = self._left[old]
    if not left then
        return false
    end
    local right = self._right[old]
    self._right[left] = new
    self._right[new]  = right
    self._left[right] = new
    self._left[new]   = left

    self._right[old]  = nil
    self._left[old]   = nil
    return true
end

---@return integer
function mt:getSize()
    return self._size
end

---@param start any
---@param revert? boolean
---@return fun():any
function mt:pairs(start, revert)
    if revert then
        if start == nil then
            start = self._left[TAIL]
        end
        local next = start
        return function ()
            local current = next
            if current == HEAD then
                return nil
            end
            next = self._left[current]
            return current
        end
    else
        if start == nil then
            start = self._right[HEAD]
        end
        local next = start
        return function ()
            local current = next
            if current == TAIL then
                return nil
            end
            next = self._right[current]
            return current
        end
    end
end

---@param start any
---@param revert? boolean
---@return string
function mt:dump(start, revert)
    local t = {}
    for node in self:pairs(start, revert) do
        t[#t+1] = tostring(node)
    end
    return table.concat(t, ' ')
end

function mt:reset()
    self._left  = { [TAIL] = HEAD }
    self._right = { [HEAD] = TAIL }

    self._size = 0
end

---@return linked-table
return function ()
    local self = setmetatable({}, mt)
    self:reset()
    return self
end