-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath-table.lua
265 lines (242 loc) · 5.67 KB
/
path-table.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
---@class PathTable
local M = {}
M.__index = M
local dirMT = {}
---@param weakMode string
---@return PathTable.Dir
local function createDir(weakMode)
return setmetatable({}, dirMT[weakMode])
end
local wk = { __mode = 'k' }
local wv = { __mode = 'v' }
local wkv = { __mode = 'kv' }
local dirChildMT = {}
dirChildMT[''] = { __index = function (t, k)
local dir = createDir ''
t[k] = dir
return dir
end }
dirChildMT['k'] = {
__mode = 'k',
__index = function (t, k)
local dir = createDir 'k'
t[k] = dir
return dir
end
}
dirChildMT['v'] = {
__mode = '',
__index = function (t, k)
local dir = createDir 'v'
t[k] = dir
return dir
end
}
dirChildMT['kv'] = {
__mode = 'k',
__index = function (t, k)
local dir = createDir 'kv'
t[k] = dir
return dir
end
}
dirMT[''] = { __index = function (t, k)
if k == 'childDirs' then
local child = setmetatable({}, dirChildMT[''])
t[k] = child
return child
end
if k == 'values' then
local values = {}
t[k] = values
return values
end
error('Invalid key ' .. tostring(k))
end }
dirMT['k'] = { __index = function (t, k)
if k == 'childDirs' then
local child = setmetatable({}, dirChildMT['k'])
t[k] = child
return child
end
if k == 'values' then
local values = setmetatable({}, wk)
t[k] = values
return values
end
error('Invalid key ' .. tostring(k))
end }
dirMT['v'] = { __index = function (t, k)
if k == 'childDirs' then
local child = setmetatable({}, dirChildMT['v'])
t[k] = child
return child
end
if k == 'values' then
local values = setmetatable({}, wv)
t[k] = values
return values
end
error('Invalid key ' .. tostring(k))
end }
dirMT['kv'] = { __index = function (t, k)
if k == 'childDirs' then
local child = setmetatable({}, dirChildMT['kv'])
t[k] = child
return child
end
if k == 'values' then
local values = setmetatable({}, wkv)
t[k] = values
return values
end
error('Invalid key ' .. tostring(k))
end }
---@class PathTable.Dir
---@field childDirs table<any, PathTable.Dir>
---@field values table<any, any>
---@field fields any[] | false
M.weakMode = ''
---@private
---@param t PathTable.Dir
---@param path any[]
---@param index integer
---@param value any
function M:_set(t, path, index, value)
local key = path[index]
local isLastKey = index == #path
if isLastKey then
t.values[key] = value
return
end
-- fallback to childs part
self:_set(t.childDirs[key], path, index + 1, value)
end
---@private
---@param t PathTable.Dir
---@param path any[]
---@param index integer
---@return any
function M:_get(t, path, index)
local key = path[index]
local isLastKey = index == #path
if isLastKey then
local values = rawget(t, 'values')
if not values then
return nil
end
local value = values[key]
if value == nil and not next(values) then
t.values = nil
end
return value
end
-- try childs part
local childDirs = rawget(t, 'childDirs')
if childDirs then
local child = rawget(childDirs, key)
if not child then
return nil
end
local value = self:_get(child, path, index + 1)
if value == nil and not next(child) then
childDirs[key] = nil
if not next(childDirs) then
t.childDirs = nil
end
end
return value
end
return nil
end
---@private
---@param t PathTable.Dir
---@param path any[]
---@param index integer
---@return boolean
function M:_delete(t, path, index)
local key = path[index]
local isLastKey = index == #path
if isLastKey then
local values = rawget(t, 'values')
if values then
values[key] = nil
if not next(values) then
t.values = nil
end
end
return true
end
-- try childs part
local childDirs = rawget(t, 'childDirs')
if childDirs then
if self:_delete(childDirs[key], path, index + 1) then
if not next(childDirs[key]) then
childDirs[key] = nil
if not next(childDirs) then
t.childDirs = nil
end
end
return true
end
end
return false
end
function M:clear()
---@private
self.root = createDir(self.weakMode)
end
---@param path any[]
---@param value any
---@return PathTable
function M:set(path, value)
if #path == 0 then
error('path is empty')
end
if value == nil then
error('value is nil')
end
self:_set(self.root, path, 1, value)
return self
end
---@param path any[]
---@return any
function M:get(path)
if #path == 0 then
error('path is empty')
end
return self:_get(self.root, path, 1)
end
---@param path any[]
---@return boolean
function M:has(path)
return self:get(path) ~= nil
end
---@param path any[]
---@return boolean
function M:delete(path)
if #path == 0 then
error('path is empty')
end
return self:_delete(self.root, path, 1)
end
---@class PathTable.API
local API = {}
---@param weakKey? boolean
---@param weakValue? boolean
---@return PathTable
function API.create(weakKey, weakValue)
local weakMode = ''
if weakKey then
weakMode = weakMode .. 'k'
end
if weakValue then
weakMode = weakMode .. 'v'
end
local pt = setmetatable({
weakMode = weakMode,
}, M)
pt:clear()
return pt
end
return API