Table_Tips.lua 9.9 KB

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