表map
# 在table的基础上做了轻量级封装,可以获取到元素数量,如果用table,需要每次遍历所有k,v才行
local map = {} map.__cname = "util.map" local null = {} map.null = null map.__newindex = function(self, k, v) error(string.format("use map:Add to add new k-v: %s-%s", k, v)) end map.__index = map function map.new() local obj = {} obj:ctor() setmetatable(obj, map) return obj end function map.attach(tb) setmetatable(tb, map) local count = 0 for k, v in pairs(tb) do count = count + 1 end tb._count = count tb._ver = 0 return tb end function map:ctor() self._count = 0 self._ver = 0 end function map:Clear() if self._count < 1 then return end for k, v in pairs(self) do self[k] = nil end self._count = 0 self:_IcrVersion() end function map:_IcrVersion() self._ver = self._ver + 1 end function map:GetCount() return self._count end function map:Add(k, v) local oldV = self[k] assert(nil == oldV) if nil == v then v = null end rawset(self, k, v) --self[k] = v self._count = self._count + 1 self:_IcrVersion() end function map:Remove(k) local oldV = self[k] if nil == oldV then return false, nil end self[k] = nil self._count = self._count - 1 self:_IcrVersion() return true, oldV end function map:Set(k, v) local oldV = self[k] if nil == oldV then self:Add(k, v) else oldV.value = v end end function map:Get(k) return self[k] end function map:ContainsKey(k) return nil ~= self[k] end function map:__tostring() if self._count <= 0 then return "" end local strTb = {} local count = 0 for k, v in self do if count > 0 then table.insert(strTb, ",") end table.insert(strTb, tostring(k)) table.insert(strTb, "->") if null == v then table.insert(strTb, "null") else table.insert(strTb, tostring(v)) end count = count + 1 end return table.concat(strTb) end function map:ipairs() error("use map:pairs") end function map:pairs() local version = self._ver local function iterator(tb, k) if version ~= self._ver then error("map modified in pairs") end local n_k, v = next(self, k) return n_k, v end return iterator end function map:IsReadOnly() return false end return map
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!