miduo_server/luafight/Modules/Battle/Logic/Misc/BattleDictionary.lua

67 lines
1.4 KiB
Lua
Raw Normal View History

2019-03-12 14:05:45 +08:00
BattleDictionary = {}
function BattleDictionary.New()
local o = {}
setmetatable(o, BattleDictionary)
BattleDictionary.__index = BattleDictionary
2019-03-21 14:33:56 +08:00
o.kL = BattleList.New()
o.vL = BattleList.New()
o.kList = o.kL.buffer
o.vList = o.vL.buffer
2019-03-12 14:05:45 +08:00
o.kvList = {}
o.size = 0
return o
end
function BattleDictionary:Add(k,v)
if not self.kvList[k] then
self.size = self.size + 1
self.kvList[k] = v
2019-03-21 14:33:56 +08:00
self.kL:Add(k)
self.vL:Add(v)
2019-03-12 14:05:45 +08:00
end
end
function BattleDictionary:Remove(k)
if self.kvList[k] then
for i=1, self.size do
if self.vList[i] == self.kvList[k] then
self.size = self.size - 1
2019-03-21 14:33:56 +08:00
self.kL:Remove(i)
self.vL:Remove(i)
2019-03-12 14:05:45 +08:00
self.kvList[k] = nil
break
end
end
end
end
2020-04-10 14:52:41 +08:00
function BattleDictionary:Get(k)
return self.kvList[k]
end
2019-03-12 14:05:45 +08:00
function BattleDictionary:Set(k,v)
self:Remove(k)
self:Add(k,v)
end
function BattleDictionary:Clear()
2019-03-21 14:33:56 +08:00
self.kL:Clear()
self.vL:Clear()
2019-03-12 14:05:45 +08:00
self.kvList = {}
self.size = 0
end
function BattleDictionary:Count()
return self.size
end
function BattleDictionary:Foreach(func)
for i = 1, self.size do
if func then
2020-04-10 14:52:41 +08:00
if func(self.kList[i], self.vList[i]) == "break" then
break
end
2019-03-12 14:05:45 +08:00
end
end
end