123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- function table.contains(t, value)
- return table.getKey(t, value) ~= nil
- end
- function table.count(t)
- if not t then
- return 0
- end
- local n = 0
- for _, _ in pairs(t) do
- n = n + 1
- end
- return n
- end
- function table.copy(t)
- ---@class nt
- local nt = {}
- for k, v in pairs(t) do
- nt[k] = v
- end
- return nt
- end
- function table.copyFrom(t,from)
- for k, v in pairs(from) do
- t[k] = v
- end
- return t
- end
- function table.getKey(t, value)
- for k, v in pairs(t) do
- if v == value then
- return k
- end
- end
- end
- function table.getValue(t, key)
- for k, v in pairs(t) do
- if k == key then
- return v
- end
- end
- end
- function table.isArray(t)
- for i = 1, table.count(t) do
- if not t[i] then
- return false
- end
- end
- return true
- end
- function table.isNullOrEmpty(t)
- return t == nil or next(t) == nil
- end
- function table.keys(t)
- ---@class keys
- local keys = {}
- for k, v in pairs(t) do
- table.insert(keys, k)
- end
- return keys
- end
- -- 将t2合并到t1
- function table.merge(t1, t2)
- for k, v in pairs(t2) do
- t1[k] = v
- end
- end
- function table.concatTable(t,t2)
- for k, v in pairs(t2) do
- table.insert(t,v)
- end
- return t
- end
- local function tableToString(t, processed)
- if type(t) ~= "table" or processed and table.contains(processed, t) then
- return tostring(t)
- end
- if processed then
- table.insert(processed, t)
- end
- local str
- if table.isArray(t) then
- for _, v in ipairs(t) do
- local item = processed and table.toString(v, processed) or tostring(v)
- str = str and (str .. ", " .. item) or item
- end
- else
- for k, v in pairs(t) do
- local item = (processed and table.toString(k, processed) or tostring(k)) .. ":" ..
- (processed and table.toString(v, processed) or tostring(v))
- str = str and (str .. ", " .. item) or item
- end
- end
- return str and "{" .. str .. "}" or "{}"
- end
- local function tableToString(t, processed)
- if type(t) ~= "table" or processed and table.contains(processed, t) then
- return tostring(t)
- end
- if processed then
- table.insert(processed, t)
- end
- local str
- if table.isArray(t) then
- for _, v in ipairs(t) do
- local item = processed and table.toString(v, processed) or tostring(v)
- str = str and (str .. ", " .. item) or item
- end
- else
- for k, v in pairs(t) do
- local item = (processed and table.toString(k, processed) or tostring(k)) .. ":" ..
- (processed and table.toString(v, processed) or tostring(v))
- str = str and (str .. ", " .. item) or item
- end
- end
- return str and "{" .. str .. "}" or "{}"
- end
- local function tableToStringJsonFormat(t, processed,indent)
- if indent == nil then
- indent = ''
- end
- if type(t) ~= "table" or processed and table.contains(processed, t) then
- return tostring(t)
- end
- if processed then
- table.insert(processed, t)
- end
- local space = ' '
- local str
- if table.isArray(t) then
- for i = 1, table.count(t) do
- local v = t[i]
- local item = processed and tableToStringJsonFormat(v, processed,indent.. space) or tostring(v)
- item =string.format('[%s]:%s',i,item)
- str = str and string.format("%s\n%s%s%s",str,indent,space,item) or string.format("%s%s%s",indent,space,item)
- --item = '['..tostring(i)..']:'..item
- --str = str and (str .. "\n" ..indent.. space .. item) or (indent.. space ..item)
- end
- else
- for k, v in pairs(t) do
- local kName = tostring(k)
- if string.isNullOrEmpty(kName) then
- local item = (processed and tableToStringJsonFormat(v, processed,indent.. space) or tostring(v))
- --str = str and (str .. "\n" ..indent.. space .. item) or (indent..space..item)
- str = str and string.format("%s\n%s%s%s",str,indent,space,item) or string.format("%s%s%s",indent,space,item)
- else
- local item = (processed and tableToStringJsonFormat(k, processed,indent.. space) or tostring(k)) .. ":" ..
- (processed and tableToStringJsonFormat(v, processed,indent.. space) or tostring(v))
- --str = str and (str .. "\n" ..indent.. space .. item) or (indent..space..item)
- str = str and string.format("%s\n%s%s%s",str,indent,space,item) or string.format("%s%s%s",indent,space,item)
- end
- end
- end
- return str and string.format("\n%s{\n%s\n%s}",indent,str,indent) or string.format("{\n%s}",indent)
- --return str and "\n"..indent.."{\n"..str .. "\n"..indent.."}" or "{\n"..indent.."}"
- end
- function table.toString(t, recursive)
- return tableToString(t, recursive and {})
- end
- function table.toStringJsonFormat(t, recursive,indent,tablename)
- if type(t)~='table' then
- return tostring(t)
- end
- table.sort(t,function(a,b)
- return string.byte(tostring(a))<string.byte(tostring(b))
- end)
- if tablename then
- return string.format(' %s %s:%s',t,tablename,tableToStringJsonFormat(t, recursive and {},indent))
- else
- return string.format(' %s %s',t,tableToStringJsonFormat(t, recursive and {},indent))
- end
- end
- function table.values(t)
- ---@class values
- local values = {}
- for k, v in pairs(t) do
- table.insert(values, v)
- end
- return values
- end
- ---@return void @根据value值删除,只能是table(List)对象,会重排k值
- function table.removeByValue(t, value)
- for i = #t, 1,-1 do
- if t[i] == value then
- table.remove(t,i)
- return true
- end
- end
- return false
- end
- ---@return void @根据value值删除,如果是table(List)对象,不会重排k值
- function table.removeByKeyValue(t, value)
- for k, v in pairs(t) do
- if v == value then
- t[k] = nil
- end
- end
- end
- ---@generic K,V
- ---@param list table<K, V> | V[]
- ---@param comp fun(a:K, b:K):boolean
- ---@return number @使用 table里面不要有keys和count这样的字段,for i = 1, #t.keys do
- -- t[t.keys[i]]
- -- end
- function table.sortByKeys(t,comp)
- if not t.keys then
- t.keys = {}
- end
- t.count = 0
- local count = table.count(t) - 2--要减掉keys和count这两个变量
- t.count = count
- for i = 1, #t.keys do
- t.keys[i] = nil
- end
- local keyIndex = 1
- for k, v in pairs(t) do
- if k ~= 'keys' and k ~= 'count' then
- t.keys[keyIndex] = k
- keyIndex = keyIndex+1
- end
- end
- return table.sort(t.keys,comp)
- end
- ---@param func function(s:table,k:any,v:any)
- function table.find(t,func,selfTable,compareData)
- for k, v in pairs(t) do
- if func(selfTable,compareData,k,v) then
- return k,v
- end
- end
- return nil
- end
- ---@generic V
- ---@param list table<number, V> | V[]
- ---@param func fun(v:V):boolean
- ---@return number,V
- function table.findByCondi(list,func)
- for k, v in pairs(list) do
- if func(v) then
- return k,v
- end
- end
- return nil
- end
- ---@generic V
- ---@param list table<V, any>
- ---@param func fun(a:V):boolean
- ---@return V,any
- function table.findByCondiKey(list,func)
- for k, v in pairs(list) do
- if func(k) then
- return k,v
- end
- end
- return nil
- end
- -- 返回不重复的value
- function table.diffValue(t)
- local temp = {}
- local hashTable = {}
- for _ , value in pairs(t) do
- if hashTable[value] == nil then
- hashTable[value] = true
- table.insert(temp, value)
- end
- end
- return temp
- end
- function table.clear(t)
- for k, v in pairs(t) do
- t[k] = nil
- end
- end
- function table.clearWithoutFunc(t)
- for k, v in pairs(t) do
- if t[k] and type(t[k]) ~= 'function' then
- t[k] = nil
- end
- end
- end
- ---@return void @键值对顺序遍历table。调用前需要调用forOrderCollectKeys
- ---@param ascending boolean @keys值升序或者降序排序,默认升序
- function table.forOrder(t,func,ascending)
- if t.keys then
- table.clear(t.keys)
- else
- t.keys = {}
- for k, v in pairs(t) do
- t[k] = nil
- end
- end
- for k, v in pairs(t) do
- if v ~= t.keys then
- table.insert(t.keys, k)
- end
- end
- if ascending == nil then
- ascending = true
- end
- table.sort(t.keys,function(a,b)
- if ascending then
- return a<b
- else
- return b<a
- end
- end)
- for k, v in pairs(t.keys) do
- if func then
- func(t[v])
- end
- end
- end
- -----@param recalculateKeys boolean @table发生变化,如果需要顺序遍历table,传入true
- --function table.forOrderCollectKeys(t,recalculateKeys)
- -- if not t.keys or recalculateKeys then
- -- t.keys = table.keys(t)
- -- end
- --end
- function table.insertArray(t,t2)
- for k, v in pairs(t2) do
- table.insert(t,v)
- end
- end
- function table.maxValue(t)
- local maxValue = math.mininteger
- local key
- for k, v in pairs(t) do
- if type(v)=="number" and v > maxValue then
- maxValue = v
- key = k
- end
- end
- return maxValue,key
- end
- -- 去重
- function table.unique(t, bArray, mainKey)
- local check = {}
- local n = {}
- local idx = 1
- for k, v in pairs(t) do
- local judgeKey = v
- if mainKey then
- judgeKey = v[mainKey] or v
- end
- if not check[judgeKey] then
- if bArray then
- n[idx] = v
- idx = idx + 1
- else
- n[k] = v
- end
- check[judgeKey] = true
- end
- end
- return n
- end
- function table.insertOpti(t,t2)
- t[#t+1] = v
- end
- function table.AddRanage(t,t2)
- for k, v in pairs(t2) do
- t[#t+1] = v
- end
- return t
- end
|