forked from manistal/calmdownandgamble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDGUtils.lua
56 lines (47 loc) · 1.36 KB
/
CDGUtils.lua
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
-- Util Functions -- Lua doesnt provide alot of basic functionality
-- =======================================================================
function CalmDownandGamble:SplitString(str, pattern)
local ret_list = {}
local index = 1
for token in string.gmatch(str, pattern) do
ret_list[index] = token
index = index + 1
end
return ret_list
end
function CalmDownandGamble:CopyTable(T)
local u = { }
for k, v in pairs(T) do u[k] = v end
return setmetatable(u, getmetatable(T))
end
function CalmDownandGamble:TableLength(T)
if (T == nil) then return 0 end
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function CalmDownandGamble:PrintTable(T)
for k, v in pairs(T) do
CalmDownandGamble:Print(k.." "..v)
end
end
function CalmDownandGamble:sortedpairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end