misc.lua 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. --- Miscellaneous functions
  2. -- @module modpol.util.misc
  3. modpol.util = {}
  4. --- Returns a copy of the table inputted
  5. -- @function modpol.util.copy_table
  6. -- @param t table to copy
  7. -- @return copy of table
  8. function modpol.util.copy_table(t)
  9. local t2 = {}
  10. if pairs(t) then
  11. for k,v in pairs(t) do
  12. if type(v) == "table" then
  13. t2[k] = modpol.util.copy_table(v)
  14. else
  15. t2[k] = v
  16. end
  17. end
  18. end
  19. return t2
  20. end
  21. --- Returns the number of elements in a pairs table
  22. -- @function modpol.util.num_pairs
  23. -- @param t pairs table
  24. -- @return number of elements in pairs table
  25. function modpol.util.num_pairs(t)
  26. local i = 0
  27. for k,v in pairs(t) do
  28. i = i + 1
  29. end
  30. return i
  31. end
  32. function modpol.util.lazy_table_length(tbl, lazy_val)
  33. local count = 0
  34. for k, v in ipairs(tbl) do
  35. if v ~= lazy_val then
  36. count = count + 1
  37. end
  38. end
  39. return count
  40. end