MattiasBuelens

Common table

Jul 24th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. --[[
  2.  
  3.     Common utilities
  4.     Table functions
  5.  
  6. --]]
  7.  
  8. common = common or {}
  9.  
  10. function common.find(haystack, needle)
  11.     assert(type(haystack) == "table", "invalid haystack")
  12.  
  13.     for k, v in ipairs(haystack) do
  14.         if v == needle then
  15.             return k
  16.         end
  17.     end
  18.  
  19.     return nil
  20. end
  21.  
  22. function common.ifind(haystack, needle)
  23.     assert(type(haystack) == "table", "invalid haystack")
  24.  
  25.     for i, v in ipairs(haystack) do
  26.         if v == needle then
  27.             return i
  28.         end
  29.     end
  30.  
  31.     return nil
  32. end
  33.  
  34. function common.clear(t)
  35.     assert(type(t) == "table", "invalid table")
  36.  
  37.     for k in pairs(t) do
  38.         t[k] = nil
  39.     end
  40. end
  41.  
  42. function common.clone(t)
  43.     if type(t) ~= "table" then
  44.         return t
  45.     end
  46.  
  47.     local c = { }
  48.     for k, v in pairs(t) do
  49.         c[k] = v
  50.     end
  51.  
  52.     return setmetatable(c, getmetatable(t))
  53. end
  54.  
  55. function common.serialize(t)
  56.     return textutils.serialize(t)
  57. end
  58.  
  59. function common.getKeys(src)
  60.     local out = {}
  61.     for key in pairs(src) do
  62.         table.insert(out, key)
  63.     end
  64.     return out
  65. end
  66.  
  67. function common.getValues(src)
  68.     local out = {}
  69.     for _, value in pairs(src) do
  70.         table.insert(out, value)
  71.     end
  72.     return out
  73. end
  74.  
  75. function common.combine(keys, values)
  76.     assert(type(keys) == "table", "invalid keys table")
  77.     local out = {}
  78.     if type(values) == "table" then
  79.         -- Match keys with values
  80.         for i, key in ipairs(keys) do
  81.             out[key] = values[i]
  82.         end
  83.     else
  84.         -- Single value for all keys
  85.         for i, key in ipairs(keys) do
  86.             out[key] = values
  87.         end
  88.     end
  89.     return out
  90. end
  91.  
  92. -- Randomly shuffle a list using the Fisher-Yates shuffle
  93. --
  94. -- Adapted from Wikipedia
  95. -- http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_.22inside-out.22_algorithm
  96. function common.shuffle(src)
  97.     assert(type(src) == "table", "invalid source")
  98.     local out = { src[1] }
  99.     local j
  100.     for i = 2, #src do
  101.         j = math.random(i) -- 1 <= j <= i
  102.         out[i] = out[j]
  103.         out[j] = src[i]
  104.     end
  105.     return out
  106. end
  107.  
  108. -- Randomly cycle a list using Sattolo's algorithm
  109. --
  110. -- This differs from shuffling the list because
  111. -- no element can ever end up on its original position
  112. function common.randomCycle(src)
  113.     assert(type(src) == "table", "invalid source")
  114.     local out = { src[1] }
  115.     local j
  116.     for i = 2, #src do
  117.         j = math.random(i-1) -- 1 <= j <= i-1
  118.         out[i] = out[j]
  119.         out[j] = src[i]
  120.     end
  121.     return out
  122. end
Advertisement
Add Comment
Please, Sign In to add comment