-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.lua
More file actions
68 lines (56 loc) · 1.26 KB
/
Array.lua
File metadata and controls
68 lines (56 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--- Realy simple array implementation.
-- Simple example
-- local Array = require("Array")
-- local ar = Array.new{1,2,3,4}
-- print(ar)
local Array = {}
local Debug = require("dbg")
dbg = Debug:new()
local mt = {}
function Array.new(l)
local arr = {}
setmetatable(arr, mt)
if l then
if type(l) == "table" then
for i, v in ipairs(l) do arr[i] = v end
elseif l then
arr[1] = l
end
end
return arr
end
function Array.concat(a,b)
local res = Array.new{}
for i, k in ipairs(a) do res[i] = k end
for i, k in ipairs(b) do
res[#res+1] = k
end
return res
end
function Array.configToString(to, tc, ts)
dbg.tableOpen = to
dbg.tableClose = tc
dbg.tableSep = ts
dbg.itemOpen = '"'
dbg.itemClose = '"'
dbg.keyOpen = '['
dbg.keyClose = ']'
end
function Array.tostring(arr)
local rsl = dbg.tableOpen
local sep = ''
for i, v in ipairs(arr) do
rsl = dbg:tostringTableLine(rsl, sep, i, v)
--print(dbg.keyOpen,"-",rsl)
sep = dbg.tableSep
end
return rsl .. dbg.tableClose
end
function Array.print(s)
print(Array.tostring(s))
end
mt.__add = Array.concat
--mt.__mul = Array.product
--mt.__sub = Array.diff
mt.__tostring = Array.tostring
return Array