forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableUtils.lua
147 lines (130 loc) · 4.15 KB
/
tableUtils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- @module tableUtils
-- A collection of table utilility functions
local tableUtils = {}
-- returns a new table where the value of each pair is replaced by the return value of running it through the supplied function
function tableUtils.map(tab, func)
local mappedTable = {}
for key, val in pairs(tab) do
mappedTable[key] = func(val)
end
return mappedTable
end
-- returns the number of elements in the table
--
-- unlike #tab, this also works for dictionaries and arrays starting at index 0 and that have "gaps" inbetween indices
function tableUtils.length(tab)
local count = 0
for _ in pairs(tab) do
count = count + 1
end
return count
end
-- returns all elements in a new table that fulfill the filter condition
function tableUtils.filter(tab, filter)
local filteredTable = {}
local preserveKeys = #tab == 0
for key, value in pairs(tab) do
if filter(value) then
if not preserveKeys then
key = #filteredTable+1
end
filteredTable[key] = value
end
end
return filteredTable
end
-- returns true if the table contains at least one value that fulfills the condition, otherwise false
function tableUtils.trueForAny(tab, condition)
for _, value in pairs(tab) do
if condition(value) then
return true
end
end
return false
end
-- returns true if all value elements of the table fulfill the condition, otherwise false
function tableUtils.trueForAll(tab, condition)
for _, value in pairs(tab) do
if not condition(value) then
return false
end
end
return true
end
-- appends all entries of tab to the end of list
function tableUtils.appendToList(list, tab)
for i = 1, #tab do
list[#list+1] = tab[i]
end
end
-- inserts all entries of tab starting at the specified position of list
function tableUtils.insertListAt(list, position, tab)
for i = #tab, 1, -1 do
table.insert(list, position, tab[i])
end
end
-- returns true if the table contains the given element, otherwise false
function tableUtils.contains(tab, element)
return tableUtils.trueForAny(
tab,
function(tabElement)
return deep_content_equal(tabElement, element)
end
)
end
-- appends an element to a table only if it does not contain the element yet
--
-- use this when you want to pretend that your table is a hashset
function tableUtils.appendIfNotExists(tab, element)
if not tableUtils.contains(tab, element) then
table.insert(tab, #tab + 1, element)
end
end
-- Randomly grabs a value from the table
function tableUtils.getRandomElement(tab)
if #tab > 0 then
return tab[math.random(#tab)]
else
-- pairs already returns in an arbitrary order but I'm not sure if it's truly random
local rolledIndex = math.random(tableUtils.length(tab))
local index = 0
for _, value in pairs(tab) do
index = index + 1
if index == rolledIndex then
return value
end
end
end
end
-- returns all keys of a table, sorted using the standard comparator to account for sequence based tables
function tableUtils.getKeys(tab)
local keys = {}
for key, _ in pairs(tab) do
table.insert(keys, key)
end
table.sort(keys)
return keys
end
-- returns the key for the given value, key is random if value occurs multiple times
function tableUtils.indexOf(tab, element)
for key, value in pairs(tab) do
if value == element then
return key
end
end
return nil
end
-- inserts all values from tab into a newly created table using continuous integer keys in a non-deterministic order
-- previous keys and order will be lost!
-- use this if you need #tab to yield a reliable result and you don't care about keys/order
-- avoid during update process as it throws away a table reference on every call!
function tableUtils.toContinuouslyIndexedTable(tab)
local continuouslyIndexedTable = {}
local i = 1
for _, v in pairs(tab) do
continuouslyIndexedTable[i] = v
i = i + 1
end
return continuouslyIndexedTable
end
return tableUtils