-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeneration-table.lua
More file actions
62 lines (54 loc) · 1.16 KB
/
generation-table.lua
File metadata and controls
62 lines (54 loc) · 1.16 KB
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
---@class GenerationTable
local M = {}
M.__index = M
---@package
---@param count integer # 分成多少代,一般2~3代差不多了
function M:init(count)
self.count = count
---@private
---@type table[]
self.list = {{}}
---@private
self.index = 0
end
---@param k any
---@param v any
function M:set(k, v)
local index = self.index % self.count + 1
local current = self.list[index]
self:del(k)
current[k] = v
end
---@param k any
function M:del(k)
for _, t in ipairs(self.list) do
t[k] = nil
end
end
---@param k any
---@return any
function M:get(k)
for _, t in ipairs(self.list) do
local v = t[k]
if v ~= nil then
return v
end
end
end
---@param grave fun(deads?: table)
function M:grow(grave)
self.index = self.index + 1
local index = self.index % self.count + 1
local current = self.list[index]
grave(current)
self.list[index] = {}
end
---@class GenerationTable.API
local API = {}
---@param count integer # 分成多少代,一般2~3代差不多了
function API.create(count)
local t = setmetatable({}, M)
t:init(count)
return t
end
return API