Lua实现Map

通过Lua中自带的table来实现一个Map,可以根据键值来插入移除取值

map = {}

local this = map

function this:new()
o = {}
setmetatable(o, self)
self.__index = self
self.count =
return o
end function this:insert(k, v)
if nil == self[k] then
self[k] = v
self.count = self.count +
end
end function this:remove(k)
if nil ~= self[k] then
self[k] = nil
if self.count > then
self.count = self.count -
end
end
end function this:getpair(k)
local value = nil
if nil ~= self[k] then
value = self[k]
--print("getpair")
end
return value
end function this:clear()
for k, _ in pairs(self) do
if nil ~= self[k] then
self[k] = nil
end
end
self.count =
end return map
上一篇:samba使用


下一篇:Asynchronous programming patterns : Task-based asynchronous pattern:TAP vs the Asynchronous Progr