Possseidon

advTable

Apr 1st, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. local function equal(t1, t2, inner)
  2.   local count = 0
  3.   if inner then
  4.     for i,s in next,t1 do
  5.       count = count + 1
  6.       if type(t1[i]) == "table" and
  7.          type(t2[i]) == "table" then
  8.         if not equal(t1[i], t2[i], true) then
  9.           return false
  10.         end
  11.       elseif t1[i] ~= t2[i] then
  12.         return false
  13.       end
  14.     end
  15.   else
  16.     for i,s in next,t1 do
  17.       count = count + 1
  18.       if s ~= t2[i] then
  19.         return false
  20.       end
  21.     end
  22.   end
  23.   for i,s in next,t2 do
  24.     count = count - 1
  25.   end
  26.   return count == 0
  27. end
  28.  
  29. local function swap(t, a, b)
  30.   local save = t[a]
  31.   t[a] = t[b]
  32.   t[b] = save
  33. end
  34.  
  35. local function copy(t, inner)
  36.   if type(t) ~= "table" then
  37.     error("table expected, got "..type(t), 2)
  38.   end
  39.   local new = {}
  40.   if inner then
  41.     for i,s in next,t do
  42.       if type(s) == "table" then
  43.         new[i] = copy(s, true)
  44.       else
  45.         new[i] = s
  46.       end
  47.     end
  48.   else
  49.     for i,s in next,t do
  50.       new[i] = s
  51.     end
  52.   end
  53.   return new
  54. end
  55.  
  56. local function items(t, inner)
  57.   local count = 0
  58.   if inner then
  59.     for i,s in next,t do
  60.       if type(s) == "table" then
  61.         count = count + items(s, true)
  62.       else
  63.         count = count + 1
  64.       end
  65.     end
  66.   else
  67.     for i,s in next,t do
  68.       count = count + 1
  69.     end
  70.   end
  71.   return count
  72. end
  73.  
  74. local function find(t, a, inner)
  75.   if inner then
  76.     for i,s in next,t do
  77.       if type(s) == "table" then
  78.         local x = {find(s, a, true)}
  79.         if table.items(x) > 0 then
  80.           return i,unpack(x)
  81.         end
  82.       else
  83.         if s == a then
  84.           return i
  85.         end
  86.       end
  87.     end
  88.   else
  89.     for i,s in next,t do
  90.       if s == a then
  91.         return i
  92.       end
  93.     end
  94.   end
  95. end
  96.  
  97. local function clear(t)
  98.   if type(t) ~= "table" then
  99.     error("table expected, got "..type(t))
  100.   end
  101.   local l = {}
  102.   for i,s in next,t do
  103.     l[#l + 1] = i
  104.   end
  105.   for i,s in next,l do
  106.     t[s] = nil
  107.   end
  108. end
  109.  
  110. function _G.prev(t, o)
  111.   for i = 1, table.items(t) do
  112.     o = next(t, o)
  113.   end
  114.   return o, t[o]
  115. end
  116.  
  117. _G.table.equal = equal
  118. _G.table.swap = swap
  119. _G.table.copy = copy
  120. _G.table.items = items
  121. _G.table.find = find
  122. _G.table.clear = clear
Advertisement
Add Comment
Please, Sign In to add comment