-- 这里是扩充table的一些功能 function table.nums(t) local count = 0 for k, v in pairs(t) do count = count + 1 end return count end function table.empty(t) for k, v in pairs(t) do return false; end return true; end function table.keys(t) local keys = {} for k, v in pairs(t) do keys[#keys + 1] = k end return keys end function table.values(t) local values = {} for k, v in pairs(t) do values[#values + 1] = v end return values end function table.merge(dest, src) for k, v in pairs(src) do dest[k] = v end end function table.mergeAll(src , dest) for k, v in pairs(src) do if type(v) == "table" then if not dest[k] then dest[k] = v; else table.mergeAll(v , dest[k]); end else dest[k] = v end end end function table.imerge(dest, src) for k, v in ipairs(src) do table.insert(dest , v) end end --[[-- insert list. **Usage:** local dest = {1, 2, 3} local src = {4, 5, 6} table.insertTo(dest, src) -- dest = {1, 2, 3, 4, 5, 6} dest = {1, 2, 3} table.insertTo(dest, src, 5) -- dest = {1, 2, 3, nil, 4, 5, 6} @param table dest @param table src @param table begin insert position for dest ]] function table.insertTo(dest, src, begin) begin = tonumber(begin) if begin == nil then begin = #dest + 1 end local len = #src for i = 0, len - 1 do dest[i + begin] = src[i + 1] end end --[[ search target index at list. @param table list @param * target @param int from idx, default 1 @param bool useNaxN, the len use table.maxn(true) or #(false) default:false @param return index of target at list, if not return -1 ]] function table.indexOf(list, target, from, useMaxN) local len = (useMaxN and #list) or table.maxn(list) if from == nil then from = 1 end for i = from, len do if list[i] == target then return i end end return -1 end function table.indexOfKey(list, key, value, from, useMaxN) local len = (useMaxN and #list) or table.maxn(list) if from == nil then from = 1 end local item = nil for i = from, len do item = list[i] if item ~= nil and item[key] == value then return i end end return -1 end function table.removeItem(t, item, removeAll) for i = #t, 1, -1 do if t[i] == item then table.remove(t, i) if not removeAll then break end end end end function table.map(t, fun) for k,v in pairs(t) do t[k] = fun(v, k) end end function table.walk(t, fun) for k,v in pairs(t) do fun(v, k) end end function table.filter(t, fun) for k,v in pairs(t) do if not fun(v, k) then t[k] = nil end end end function table.find(t, item) return table.keyOfItem(t, item) ~= nil end function table.unique(t) local r = {} local n = {} for i = #t, 1, -1 do local v = t[i] if not r[v] then r[v] = true n[#n + 1] = v end end return n end function table.keyOfItem(t, item) for k,v in pairs(t) do if v == item then return k end end return nil end function table.valueOfItem(t, item) for k,v in pairs(t) do if v == item then return v end end return nil end -- 把表转换成类似这种格式的字符串{a='b';};,字符串用二进制格式表示"\01\02"之类 function table.toString(t) local tableString = vardump(t , ""); return tableString:sub(8); end -- 把表转换成类似这种格式的字符串{a='b';};,字符串用可见文本形式表示 function table.tostring(t) if not t then return "nil" end local oldFunc = string.toLuaString; string.toLuaString = tostring; local tableString = vardump(t , ""); string.toLuaString = oldFunc return tableString:sub(8); end -- 把表转换成类似这种格式的字符串return {a='b';}; function table.saveString(t) return "return " .. table.toString(t); end -- 从字符串中载入一个表 function table.loadString(tableString) local data , err = loadstring(tableString); if data == nil then print("table.loadString()", tostring(tableString)) --error(err); uploadLogs(GAME_ERROR_TYPE.TABLELOADSTRING) return nil else return data(); end end -- 将一个table数据保存到本地文件 function table.saveToLocFile(t, filename) if not t or not filename then return end local tableString = table.saveString(t) saveStringToFile(tableString, filename) end -- 从本地文件读取一个table数据 function table.loadFromLocFile(filename) if not filename then return end local tableString = loadStringFromFile(filename) if not tableString then return nil end return table.loadString(tableString) end -- 把表保存到文件中 function table.saveFile(t , filename) local file , err = io.open(filename , "w"); if file == nil then error(err); return; end file:write("return ") file:write(table.toString(t)); file:close(); end if EditorMode then -- 从文件中载入一个表 function table.loadFile(filename) local n,r = loadLuaFile(filename); if n == nil then --error("载入文件" .. tostring(filename) .. "时出错" .. r); return nil end return n(); end else -- 从文件中载入一个表 local tableCache = {}; function table.loadFile(filename) local cache = tableCache[filename]; if cache then return cache; end print("table.loadFile:" .. filename); TimeSpan:enterSpan("table.loadFile:" .. filename); local n,r = loadLuaFile(filename); if n == nil then --error("载入文件" .. tostring(filename) .. "时出错" .. r); return nil end TimeSpan:enterSpan("runTable"); cache = n(); TimeSpan:leaveSpan(); tableCache[filename] = cache; TimeSpan:leaveSpan(); return cache; end end -- 兼容版本 if not table.getn then function table.getn(t) return #t; end end local rawpairs = pairs ------------------------------------------- -- 可以按指定顺序遍历的map迭代器 -- @param tbl 要迭代的表 -- @param func 比较函数 -- @example -- for k,v in pairs(tbl,defaultComp) do print(k,v) end function table.sortPairs(tbl, func) if func == nil then return rawpairs(tbl) end -- 为tbl创建一个对key排序的数组 -- 自己实现插入排序,table.sort遇到nil时会失效 local ary = {} for key in rawpairs(tbl) do ary[#ary+1] = key end table.sort(ary,func); -- 定义并返回迭代器 local i = 0 local iter = function () i = i + 1 if ary[i] == nil then return nil else return ary[i], tbl[ary[i]] end end return iter end -------------------------------- -- 通用比较器(Comparator) -- @return 对比结果 function table.defaultSort( op1, op2 ) local type1, type2 = type(op1), type(op2) local num1, num2 = tonumber(op1), tonumber(op2) if ( num1 ~= nil) and (num2 ~= nil) then return num1 < num2 elseif type1 ~= type2 then return type1 < type2 elseif type1 == "string" then return op1 < op2 elseif type1 == "boolean" then return op1 -- 以上处理: number, string, boolean else -- 处理剩下的: function, table, thread, userdata return tostring(op1) < tostring(op2) -- tostring后比较字符串 end end