| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
function table.clear(_table) |
| 11 |
for k in pairs(_table) do |
| 12 |
_table[k] = nil |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
|
| 17 |
function table.count(T) |
| 18 |
if not T or not next(t) then return 0 end |
| 19 |
|
| 20 |
local count = 0 |
| 21 |
for _ in pairs(T) do count = count + 1 end |
| 22 |
return count |
| 23 |
end |
| 24 |
|
| 25 |
|
| 26 |
function table.slice(values, offset, length) |
| 27 |
local res = {} |
| 28 |
local n = #values |
| 29 |
|
| 30 |
offset = offset or 0 |
| 31 |
if offset < 0 or offset > n then |
| 32 |
return {} |
| 33 |
end |
| 34 |
length = length or n-offset |
| 35 |
if length < 1 then |
| 36 |
length = 1 |
| 37 |
elseif length > n-offset then |
| 38 |
length = n-offset |
| 39 |
end |
| 40 |
|
| 41 |
for i = offset,offset+length-1 do |
| 42 |
res[i-offset] = values[i] |
| 43 |
end |
| 44 |
return res |
| 45 |
end |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
function table.dump(t, ident) |
| 51 |
local output = "" |
| 52 |
if not indent then indent = 0 end |
| 53 |
|
| 54 |
|
| 55 |
if not (t[0] == nil) then |
| 56 |
for k=0, #t do |
| 57 |
local formatting = "\n" .. string.rep(" ", indent) .. k .. ": " |
| 58 |
local v = t[k] |
| 59 |
if type(v) == "table" then |
| 60 |
v = "[" .. table.print(v, indent+1) .. "]" |
| 61 |
elseif type(v) == 'boolean' then |
| 62 |
v = tostring(v) |
| 63 |
end |
| 64 |
|
| 65 |
output = output .. formatting .. v |
| 66 |
end |
| 67 |
else |
| 68 |
for k,v in pairs(t) do |
| 69 |
local formatting = "\n" .. k .. ": " |
| 70 |
local v = t[k] |
| 71 |
if type(v) == "table" then |
| 72 |
v = "[" .. table.print(v, indent+1) .. "]" |
| 73 |
elseif type(v) == 'boolean' then |
| 74 |
v = tostring(v) |
| 75 |
end |
| 76 |
|
| 77 |
output = output .. formatting .. v |
| 78 |
end |
| 79 |
end |
| 80 |
return output |
| 81 |
end |
| 82 |
|
| 83 |
|
| 84 |
function get_key_for_value( t, value ) |
| 85 |
for k,v in pairs(t) do |
| 86 |
if v==value then return k end |
| 87 |
end |
| 88 |
return nil |
| 89 |
end |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
do |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
local function exportstring( s ) |
| 122 |
return string.format("%q", s) |
| 123 |
end |
| 124 |
|
| 125 |
|
| 126 |
function table.save( tbl,filename ) |
| 127 |
local charS,charE = " ","\n" |
| 128 |
local file,err = io.open( filename, "wb" ) |
| 129 |
if err then return err end |
| 130 |
|
| 131 |
|
| 132 |
local tables,lookup = { tbl },{ [tbl] = 1 } |
| 133 |
file:write( "return {"..charE ) |
| 134 |
|
| 135 |
for idx,t in ipairs( tables ) do |
| 136 |
file:write( "-- Table: {"..idx.."}"..charE ) |
| 137 |
file:write( "{"..charE ) |
| 138 |
local thandled = {} |
| 139 |
|
| 140 |
for i,v in ipairs( t ) do |
| 141 |
thandled[i] = true |
| 142 |
local stype = type( v ) |
| 143 |
|
| 144 |
if stype == "table" then |
| 145 |
if not lookup[v] then |
| 146 |
table.insert( tables, v ) |
| 147 |
lookup[v] = #tables |
| 148 |
end |
| 149 |
file:write( charS.."{"..lookup[v].."},"..charE ) |
| 150 |
elseif stype == "string" then |
| 151 |
file:write( charS..exportstring( v )..","..charE ) |
| 152 |
elseif stype == "number" then |
| 153 |
file:write( charS..tostring( v )..","..charE ) |
| 154 |
end |
| 155 |
end |
| 156 |
|
| 157 |
for i,v in pairs( t ) do |
| 158 |
|
| 159 |
if (not thandled[i]) then |
| 160 |
|
| 161 |
local str = "" |
| 162 |
local stype = type( i ) |
| 163 |
|
| 164 |
if stype == "table" then |
| 165 |
if not lookup[i] then |
| 166 |
table.insert( tables,i ) |
| 167 |
lookup[i] = #tables |
| 168 |
end |
| 169 |
str = charS.."[{"..lookup[i].."}]=" |
| 170 |
elseif stype == "string" then |
| 171 |
str = charS.."["..exportstring( i ).."]=" |
| 172 |
elseif stype == "number" then |
| 173 |
str = charS.."["..tostring( i ).."]=" |
| 174 |
end |
| 175 |
|
| 176 |
if str ~= "" then |
| 177 |
stype = type( v ) |
| 178 |
|
| 179 |
if stype == "table" then |
| 180 |
if not lookup[v] then |
| 181 |
table.insert( tables,v ) |
| 182 |
lookup[v] = #tables |
| 183 |
end |
| 184 |
file:write( str.."{"..lookup[v].."},"..charE ) |
| 185 |
elseif stype == "string" then |
| 186 |
file:write( str..exportstring( v )..","..charE ) |
| 187 |
elseif stype == "number" then |
| 188 |
file:write( str..tostring( v )..","..charE ) |
| 189 |
end |
| 190 |
end |
| 191 |
end |
| 192 |
end |
| 193 |
file:write( "},"..charE ) |
| 194 |
end |
| 195 |
file:write( "}" ) |
| 196 |
file:close() |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
function table.load( sfile ) |
| 201 |
local ftables,err = loadfile( sfile ) |
| 202 |
if err or not ftables() then return _,err end |
| 203 |
local tables = ftables() |
| 204 |
|
| 205 |
for idx = 1,#tables do |
| 206 |
local tolinki = {} |
| 207 |
for i,v in pairs( tables[idx] ) do |
| 208 |
if type( v ) == "table" then |
| 209 |
tables[idx][i] = tables[v[1]] |
| 210 |
end |
| 211 |
if type( i ) == "table" and tables[i[1]] then |
| 212 |
table.insert( tolinki,{ i,tables[i[1]] } ) |
| 213 |
end |
| 214 |
end |
| 215 |
|
| 216 |
for _,v in ipairs( tolinki ) do |
| 217 |
tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil |
| 218 |
end |
| 219 |
end |
| 220 |
return tables[1] |
| 221 |
end |
| 222 |
|
| 223 |
end |
| 224 |
|
| 225 |
|