Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function equal(t1, t2, inner)
- local count = 0
- if inner then
- for i,s in next,t1 do
- count = count + 1
- if type(t1[i]) == "table" and
- type(t2[i]) == "table" then
- if not equal(t1[i], t2[i], true) then
- return false
- end
- elseif t1[i] ~= t2[i] then
- return false
- end
- end
- else
- for i,s in next,t1 do
- count = count + 1
- if s ~= t2[i] then
- return false
- end
- end
- end
- for i,s in next,t2 do
- count = count - 1
- end
- return count == 0
- end
- local function swap(t, a, b)
- local save = t[a]
- t[a] = t[b]
- t[b] = save
- end
- local function copy(t, inner)
- if type(t) ~= "table" then
- error("table expected, got "..type(t), 2)
- end
- local new = {}
- if inner then
- for i,s in next,t do
- if type(s) == "table" then
- new[i] = copy(s, true)
- else
- new[i] = s
- end
- end
- else
- for i,s in next,t do
- new[i] = s
- end
- end
- return new
- end
- local function items(t, inner)
- local count = 0
- if inner then
- for i,s in next,t do
- if type(s) == "table" then
- count = count + items(s, true)
- else
- count = count + 1
- end
- end
- else
- for i,s in next,t do
- count = count + 1
- end
- end
- return count
- end
- local function find(t, a, inner)
- if inner then
- for i,s in next,t do
- if type(s) == "table" then
- local x = {find(s, a, true)}
- if table.items(x) > 0 then
- return i,unpack(x)
- end
- else
- if s == a then
- return i
- end
- end
- end
- else
- for i,s in next,t do
- if s == a then
- return i
- end
- end
- end
- end
- local function clear(t)
- if type(t) ~= "table" then
- error("table expected, got "..type(t))
- end
- local l = {}
- for i,s in next,t do
- l[#l + 1] = i
- end
- for i,s in next,l do
- t[s] = nil
- end
- end
- function _G.prev(t, o)
- for i = 1, table.items(t) do
- o = next(t, o)
- end
- return o, t[o]
- end
- _G.table.equal = equal
- _G.table.swap = swap
- _G.table.copy = copy
- _G.table.items = items
- _G.table.find = find
- _G.table.clear = clear
Advertisement
Add Comment
Please, Sign In to add comment