-
-
Save erinlin/9418242 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- table library extensions | |
-- allows the table.copy function to return a shallow copy of a range of the table items | |
-- Example: | |
--local t={1,2,3,4,5,6,7,8,9,10} | |
--dump(table.copy(t,3,8)) | |
--prints out 3,4,5,6,7,8 | |
local table_copy = table.copy | |
table.copy = function( t, ... ) | |
if (type(arg[1]) == "number" and (arg[2] == nil or type(arg[2]) == "number")) then | |
local startIndex, endIndex = arg[1], arg[2] | |
if (endIndex == nil) then endIndex = #t end | |
local output = {} | |
for i=startIndex, endIndex do | |
output[ #output+1 ] = t[i] | |
end | |
return output | |
else | |
return table_copy( t, unpack( arg ) ) | |
end | |
end | |
-- like table.copy except this copies entries returned by pairs() | |
-- multiple tables can be combined but beware of overwriting entries | |
table.clone = function( ... ) | |
local output = {} | |
for i=1, #arg do | |
for k,v in pairs(arg[i]) do | |
output[ k ] = v | |
end | |
end | |
return output | |
end | |
table.deepclone = function( tbl ) | |
local output = {} | |
for k,v in pairs(tbl) do | |
if (type(v) == "table") then | |
output[k] = table.deepclone(v) | |
else | |
output[k] = v | |
end | |
end | |
return output | |
end | |
-- returns a new table containing the elements of the passed table which the compare function returned true for | |
-- works with display groups | |
table.find = function( t, compare ) | |
local newT = {} | |
local count = t.numChildren | |
if (count == nil) then | |
count = #t | |
end | |
for i=1, count do | |
local item = t[i] | |
if (compare(item)) then | |
newT[#newT+1] = item | |
end | |
end | |
return newT | |
end | |
-- if sep is a table its items are used as separators for table t | |
-- Example: | |
--local a,b = {1,3,5,7,9}, {2,4,6,8,10} | |
--print(table.concat(a,b)) | |
-- prints 12345678910 | |
local table_concat = table.concat | |
table.concat = function( t, sep, i, j ) | |
if (i == nil and j == nil and type(sep) == "table") then | |
return strZip( t, sep ) | |
else | |
return table_concat( t, sep, i, j ) | |
end | |
end | |
-- extends the table.indexOf to work with display groups | |
local _indexof = table.indexOf | |
table.indexOf = function( tbl, element ) | |
if (tbl.numChildren == nil) then | |
return _indexof( tbl, element ) | |
else | |
for i=1, tbl.numChildren do | |
if (tbl[i] == element) then | |
return i | |
end | |
end | |
return nil | |
end | |
end | |
-- returns shallow copy of table elements from index for count of size (or to end if size is nil) | |
table.range = function( tbl, index, size ) | |
if (index == nil or index < 1) then return nil end | |
size = size or #tbl-index+1 | |
local output = {} | |
for i=index, index+size-1 do | |
output[i] = tbl[i] | |
dump(tbl[i]) | |
dump(output[i]) | |
end | |
return output | |
end | |
-- extends table.remove to remove objects directly, without requiring table.indexOf | |
local _remove = table.remove | |
table.remove = function( t, pos ) | |
if (type(pos) == "number") then | |
return _remove( t, pos ) | |
else | |
pos = table.indexOf( t, pos ) | |
return _remove( t, pos ) | |
end | |
end | |
-- replaces entries of old with new | |
-- returns number of entries replaced | |
table.replace = function( tbl, old, new ) | |
local index = table.indexOf( tbl, old ) | |
local count = 0 | |
while (index) do | |
count = count + 1 | |
tbl[index] = new | |
index = table.indexOf( tbl, old ) | |
end | |
return count | |
end | |
-- implements #+1 for speed when no index is passed | |
local _insert = table.insert | |
table.insert = function( t, pos, value ) | |
if (value == nil) then | |
-- for speed | |
t[ #t+1 ] = pos | |
else | |
-- original slow insert (good for middle inserts) | |
_insert( t, pos, value ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment