table.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. function table.contains(t, value)
  2. return table.getKey(t, value) ~= nil
  3. end
  4. ---统计table的长度
  5. function table.count(t)
  6. if not t then
  7. return 0
  8. end
  9. local n = 0
  10. for _, _ in pairs(t) do
  11. n = n + 1
  12. end
  13. return n
  14. end
  15. function table.copy(t)
  16. ---@class nt
  17. local nt = {}
  18. for k, v in pairs(t) do
  19. nt[k] = v
  20. end
  21. return nt
  22. end
  23. function table.copyFrom(t, from)
  24. for k, v in pairs(from) do
  25. t[k] = v
  26. end
  27. return t
  28. end
  29. function table.getKey(t, value)
  30. for k, v in pairs(t) do
  31. if v == value then
  32. return k
  33. end
  34. end
  35. return nil
  36. end
  37. function table.getValue(t, key)
  38. return t[key]
  39. --[[ for k, v in pairs(t) do
  40. if k == key then
  41. return v
  42. end
  43. end
  44. return nil]]
  45. end
  46. function table.isArray(t)
  47. for i = 1, table.count(t) do
  48. if not t[i] then
  49. return false
  50. end
  51. end
  52. return true
  53. end
  54. function table.isNullOrEmpty(t)
  55. return table.isEmpty(t)
  56. end
  57. function table.notNullOrEmpty(t)
  58. return not table.isEmpty(t)
  59. end
  60. function table.isEmpty(t)
  61. if type(t) ~= "table" then
  62. return true
  63. end
  64. return t == nil or next(t) == nil
  65. end
  66. function table.notEmpty(t)
  67. return not table.isEmpty(t)
  68. end
  69. -- 将table的所有值转换为string
  70. -- 可用于将科学计数形式转换为字符串发送给客户端
  71. function table.valueConvertToString(t)
  72. local newTable = {}
  73. for k, v in pairs(t) do
  74. if type(v) == "table" then
  75. newTable[tostring(k)] = table.valueConvertToString(v)
  76. else
  77. newTable[tostring(k)] = tostring(v)
  78. end
  79. end
  80. return newTable
  81. end
  82. function table.keys(t)
  83. ---@class keys
  84. local keys = {}
  85. for k, v in pairs(t) do
  86. table.insert(keys, k)
  87. end
  88. return keys
  89. end
  90. ---把t2合并到t1里面,如果t2有相同的key,则t2的值覆盖t1的值
  91. function table.merge(t1, t2)
  92. for k, v in pairs(t2) do
  93. t1[k] = v
  94. end
  95. end
  96. ---把t2合并到t1里面,如果t2有相同的key,则t2的值和t1的值是追加关系,value 应该是number类型
  97. function table.mergeAdd(t1, t2)
  98. for key, value in pairs(t2) do
  99. value = tonumber(value)
  100. local existingValue = t1[key]
  101. if existingValue ~= nil then
  102. existingValue = tonumber(existingValue)
  103. t1[key] = existingValue + value
  104. else
  105. t1[key] = value
  106. end
  107. end
  108. end
  109. function table.concatTable(t, t2)
  110. for k, v in pairs(t2) do
  111. table.insert(t, v)
  112. end
  113. return t
  114. end
  115. local function tableToString(t, processed)
  116. if type(t) ~= "table" or processed and table.contains(processed, t) then
  117. return tostring(t)
  118. end
  119. if processed then
  120. table.insert(processed, t)
  121. end
  122. local str
  123. if table.isArray(t) then
  124. for _, v in ipairs(t) do
  125. local item = processed and table.toString(v, processed) or tostring(v)
  126. str = str and (str .. ", " .. item) or item
  127. end
  128. else
  129. for k, v in pairs(t) do
  130. local item = (processed and table.toString(k, processed) or tostring(k)) .. ":" ..
  131. (processed and table.toString(v, processed) or tostring(v))
  132. str = str and (str .. ", " .. item) or item
  133. end
  134. end
  135. return str and "{" .. str .. "}" or "{}"
  136. end
  137. local function tableToStringJsonFormat(t, processed, indent)
  138. if indent == nil then
  139. indent = ''
  140. end
  141. if type(t) ~= "table" or processed and table.contains(processed, t) then
  142. return tostring(t)
  143. end
  144. if processed then
  145. table.insert(processed, t)
  146. end
  147. local space = ' '
  148. local str
  149. if table.isArray(t) then
  150. for i = 1, table.count(t) do
  151. local v = t[i]
  152. local item = processed and tableToStringJsonFormat(v, processed, indent .. space) or tostring(v)
  153. item = string.format('[%s]:%s', i, item)
  154. str = str and string.format("%s\n%s%s%s", str, indent, space, item) or
  155. string.format("%s%s%s", indent, space, item)
  156. -- item = '['..tostring(i)..']:'..item
  157. -- str = str and (str .. "\n" ..indent.. space .. item) or (indent.. space ..item)
  158. end
  159. else
  160. for k, v in pairs(t) do
  161. local kName = tostring(k)
  162. if string.isNullOrEmpty(kName) then
  163. local item = (processed and tableToStringJsonFormat(v, processed, indent .. space) or tostring(v))
  164. -- str = str and (str .. "\n" ..indent.. space .. item) or (indent..space..item)
  165. str = str and string.format("%s\n%s%s%s", str, indent, space, item) or
  166. string.format("%s%s%s", indent, space, item)
  167. else
  168. local item = (processed and tableToStringJsonFormat(k, processed, indent .. space) or tostring(k)) ..
  169. ":" ..
  170. (processed and tableToStringJsonFormat(v, processed, indent .. space) or tostring(v))
  171. -- str = str and (str .. "\n" ..indent.. space .. item) or (indent..space..item)
  172. str = str and string.format("%s\n%s%s%s", str, indent, space, item) or
  173. string.format("%s%s%s", indent, space, item)
  174. end
  175. end
  176. end
  177. return str and string.format("\n%s{\n%s\n%s}", indent, str, indent) or string.format("{\n%s}", indent)
  178. -- return str and "\n"..indent.."{\n"..str .. "\n"..indent.."}" or "{\n"..indent.."}"
  179. end
  180. function table.toString(t, recursive)
  181. return tableToString(t, recursive and {})
  182. end
  183. function table.toStringJsonFormat(t, recursive, indent, tablename)
  184. if type(t) ~= 'table' then
  185. return tostring(t)
  186. end
  187. table.sort(t, function(a, b)
  188. return string.byte(tostring(a)) < string.byte(tostring(b))
  189. end)
  190. if tablename then
  191. return string.format(' %s %s:%s', t, tablename, tableToStringJsonFormat(t, recursive and {}, indent))
  192. else
  193. return string.format(' %s %s', t, tableToStringJsonFormat(t, recursive and {}, indent))
  194. end
  195. end
  196. function table.values(t)
  197. ---@class values
  198. local values = {}
  199. for k, v in pairs(t) do
  200. table.insert(values, v)
  201. end
  202. return values
  203. end
  204. ---@return void @根据value值删除,只能是table(List)对象,会重排k值
  205. function table.removeByValue(t, value)
  206. for i = #t, 1, -1 do
  207. if t[i] == value then
  208. table.remove(t, i)
  209. return true
  210. end
  211. end
  212. return false
  213. end
  214. ---@return void @根据value值删除,如果是table(List)对象,不会重排k值
  215. function table.removeByKeyValue(t, value)
  216. for k, v in pairs(t) do
  217. if v == value then
  218. t[k] = nil
  219. end
  220. end
  221. end
  222. ---@generic K,V
  223. ---@param list table<K, V> | V[]
  224. ---@param comp fun(a:K, b:K):boolean
  225. ---@return number @使用 table里面不要有keys和count这样的字段,for i = 1, #t.keys do
  226. -- t[t.keys[i]]
  227. -- end
  228. function table.sortByKeys(t, comp)
  229. if not t.keys then
  230. t.keys = {}
  231. end
  232. t.count = 0
  233. local count = table.count(t) - 2 -- 要减掉keys和count这两个变量
  234. t.count = count
  235. for i = 1, #t.keys do
  236. t.keys[i] = nil
  237. end
  238. local keyIndex = 1
  239. for k, v in pairs(t) do
  240. if k ~= 'keys' and k ~= 'count' then
  241. t.keys[keyIndex] = k
  242. keyIndex = keyIndex + 1
  243. end
  244. end
  245. return table.sort(t.keys, comp)
  246. end
  247. ---@param func function(s:table,k:any,v:any)
  248. function table.find(t, func, selfTable, compareData)
  249. for k, v in pairs(t) do
  250. if func(selfTable, compareData, k, v) then
  251. return k, v
  252. end
  253. end
  254. return nil
  255. end
  256. ---@generic V
  257. ---@param list table<number, V> | V[]
  258. ---@param func fun(a:V):boolean
  259. ---@return number,V
  260. function table.findByCondi(list, func)
  261. for k, v in pairs(list) do
  262. if func(v) then
  263. return k, v
  264. end
  265. end
  266. return nil
  267. end
  268. ---@generic V
  269. ---@param list table<V, any>
  270. ---@param func fun(a:V):boolean
  271. ---@return V,any
  272. function table.findByCondiKey(list, func)
  273. for k, v in pairs(list) do
  274. if func(k) then
  275. return k, v
  276. end
  277. end
  278. return nil
  279. end
  280. -- 返回不重复的value
  281. function table.diffValue(t)
  282. local temp = {}
  283. local hashTable = {}
  284. for _, value in pairs(t) do
  285. if hashTable[value] == nil then
  286. hashTable[value] = true
  287. table.insert(temp, value)
  288. end
  289. end
  290. return temp
  291. end
  292. function table.clear(t)
  293. for k, v in pairs(t) do
  294. t[k] = nil
  295. end
  296. end
  297. function table.clearWithoutFunc(t)
  298. for k, v in pairs(t) do
  299. if t[k] and type(t[k]) ~= 'function' then
  300. t[k] = nil
  301. end
  302. end
  303. end
  304. ---@return void @键值对顺序遍历table。调用前需要调用forOrderCollectKeys
  305. ---@param ascending boolean @keys值升序或者降序排序,默认升序
  306. function table.forOrder(t, func, ascending)
  307. if t.keys then
  308. table.clear(t.keys)
  309. else
  310. t.keys = {}
  311. for k, v in pairs(t) do
  312. t[k] = nil
  313. end
  314. end
  315. for k, v in pairs(t) do
  316. if v ~= t.keys then
  317. table.insert(t.keys, k)
  318. end
  319. end
  320. if ascending == nil then
  321. ascending = true
  322. end
  323. table.sort(t.keys, function(a, b)
  324. if ascending then
  325. return a < b
  326. else
  327. return b < a
  328. end
  329. end)
  330. for k, v in pairs(t.keys) do
  331. if func then
  332. func(t[v])
  333. end
  334. end
  335. end
  336. -----@param recalculateKeys boolean @table发生变化,如果需要顺序遍历table,传入true
  337. -- function table.forOrderCollectKeys(t,recalculateKeys)
  338. -- if not t.keys or recalculateKeys then
  339. -- t.keys = table.keys(t)
  340. -- end
  341. -- end
  342. function table.insertArray(t, t2)
  343. for k, v in pairs(t2) do
  344. table.insert(t, v)
  345. end
  346. end
  347. function table.maxValue(t)
  348. local maxValue = math.mininteger
  349. local key
  350. for k, v in pairs(t) do
  351. if type(v) == "number" and v > maxValue then
  352. maxValue = v
  353. key = k
  354. end
  355. end
  356. return maxValue, key
  357. end
  358. -- 去重
  359. function table.unique(t, bArray, mainKey)
  360. local check = {}
  361. local n = {}
  362. local idx = 1
  363. for k, v in pairs(t) do
  364. local judgeKey = v
  365. if mainKey then
  366. judgeKey = v[mainKey] or v
  367. end
  368. if not check[judgeKey] then
  369. if bArray then
  370. n[idx] = v
  371. idx = idx + 1
  372. else
  373. n[k] = v
  374. end
  375. check[judgeKey] = true
  376. end
  377. end
  378. return n
  379. end
  380. function table.insertOpti(t, t2)
  381. t[#t + 1] = v
  382. end
  383. function table.AddRanage(t, t2)
  384. for k, v in pairs(t2) do
  385. t[#t + 1] = v
  386. end
  387. return t
  388. end
  389. --- 获取前N个表中元素
  390. ---@param N 前多少个
  391. ---@param t table
  392. function table.getTopN(N, t)
  393. N = N or #t -- 如果没有指定N,则获取所有数据
  394. local topN = {}
  395. for i = 1, math.min(N, #t) do
  396. table.insert(topN, t[i])
  397. end
  398. return topN
  399. end
  400. --- 合并两个table
  401. function table.mergeTable(table1, table2)
  402. for key, value in pairs(table2) do
  403. local itemId = tonumber(key)
  404. local itemNum = tonumber(value)
  405. local oldNum = table1[itemId] or 0
  406. table1[itemId] = oldNum + itemNum
  407. end
  408. end
  409. function table.hasKey(tab, key)
  410. return tab[key] ~= nil
  411. end