1
0

checktable.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. assert(rawget(_G, "stat") == nil) -- module not loaded before
  2. if T == nil then
  3. stat = function () print"`querytab' nao ativo" end
  4. return
  5. end
  6. function checktable (t)
  7. local asize, hsize, ff = T.querytab(t)
  8. local l = {}
  9. for i=0,hsize-1 do
  10. local key,val,next = T.querytab(t, i + asize)
  11. if key == nil then
  12. assert(l[i] == nil and val==nil and next==nil)
  13. elseif key == "<undef>" then
  14. assert(val==nil)
  15. else
  16. assert(t[key] == val)
  17. local mp = T.hash(key, t)
  18. if l[i] then
  19. assert(l[i] == mp)
  20. elseif mp ~= i then
  21. l[i] = mp
  22. else -- list head
  23. l[mp] = {mp} -- first element
  24. while next do
  25. assert(ff <= next and next < hsize)
  26. if l[next] then assert(l[next] == mp) else l[next] = mp end
  27. table.insert(l[mp], next)
  28. key,val,next = T.querytab(t, next)
  29. assert(key)
  30. end
  31. end
  32. end
  33. end
  34. l.asize = asize; l.hsize = hsize; l.ff = ff
  35. return l
  36. end
  37. function mostra (t)
  38. local asize, hsize, ff = T.querytab(t)
  39. print(asize, hsize, ff)
  40. print'------'
  41. for i=0,asize-1 do
  42. local _, v = T.querytab(t, i)
  43. print(string.format("[%d] -", i), v)
  44. end
  45. print'------'
  46. for i=0,hsize-1 do
  47. print(i, T.querytab(t, i+asize))
  48. end
  49. print'-------------'
  50. end
  51. function stat (t)
  52. t = checktable(t)
  53. local nelem, nlist = 0, 0
  54. local maxlist = {}
  55. for i=0,t.hsize-1 do
  56. if type(t[i]) == 'table' then
  57. local n = table.getn(t[i])
  58. nlist = nlist+1
  59. nelem = nelem + n
  60. if not maxlist[n] then maxlist[n] = 0 end
  61. maxlist[n] = maxlist[n]+1
  62. end
  63. end
  64. print(string.format("hsize=%d elements=%d load=%.2f med.len=%.2f (asize=%d)",
  65. t.hsize, nelem, nelem/t.hsize, nelem/nlist, t.asize))
  66. for i=1,table.getn(maxlist) do
  67. local n = maxlist[i] or 0
  68. print(string.format("%5d %10d %.2f%%", i, n, n*100/nlist))
  69. end
  70. end