Debian Codegraph @

texmf-dist/tex/lualatex/luacas/_lib/luacas-table.lua

raw
1 -- Checks if two arrays are equal, starting at index i
2 function ArrayEqual(a1, a2, i)
3 i = i or 1
4 while i <= math.max(#a1, #a2) do
5 if a1[i] ~= a2[i] then
6 return false
7 end
8 i = i + 1
9 end
10 return true
11 end
12
13 -- Checks if two arrays are equal, the first starting at index i, the second starting at index j
14 function FancyArrayEqual(a1, a2, i, j)
15 i = i or 1
16 j = j or 1
17 while i <= math.max(#a1, #a2) or j <= math.max(#a1, #a2) do
18 if a1[i] ~= a2[j] then
19 return false
20 end
21 i = i + 1
22 j = j + 1
23 end
24 return true
25 end
26
27 -- Creates a copy of a table
28 function Copy(orig)
29 local orig_type = type(orig)
30 local copy
31 if orig_type == 'table' then
32 copy = {}
33 for orig_key, orig_value in pairs(orig) do
34 copy[orig_key] = orig_value
35 end
36 copy = setmetatable(copy, getmetatable(orig))
37 else -- number, string, boolean, etc
38 copy = orig
39 end
40 return copy
41 end
42
43 -- Joins two arrays
44 function JoinArrays(a1, a2)
45 local a = Copy(a1)
46 for index, value in ipairs(a2) do
47 a[index + #a1] = value
48 end
49 return a
50 end
51
52 -- Joins two arrays indexed from zero
53 function JoinZeroArrays(a1, a2)
54 local a = Copy(a1)
55 a[#a1 + 1] = a2[0]
56 for index, value in ipairs(a2) do
57 a[index + #a1 + 1] = value
58 end
59 return a
60 end
61
62 -- Join two tables, using the second entry if a key appears in both tables
63 function JoinTables(t1, t2)
64 local t = Copy(t1) or {}
65 for key, value in pairs(t2) do
66 t[key] = value
67 end
68 return t
69 end
70
71 -- Given an array, removes all occurances of that element from the array
72 function Remove(a, e)
73 local r = Copy(a)
74 local found = false
75 for index, value in ipairs(r) do
76 if e == value then
77 found = true
78 end
79 if found then
80 r[index] = r[index + 1]
81 end
82 end
83 return r
84 end
85
86 -- Given an array, removes all elements in the second array from the first
87 function RemoveAll(a1, a2)
88 local r = Copy(a1)
89 local removed = 0
90 for index, value in ipairs(r) do
91 if a2[value] then
92 removed = removed + 1
93 end
94 if removed then
95 r[index] = r[index + removed]
96 end
97 end
98 return r
99 end
100
101 -- Given an array of arrays, returns only the arrays that have no elements in common with the second array
102 function RemoveAny(aa, a)
103 local toremove = {}
104 for _, v in ipairs(aa) do
105 for _, v1 in ipairs(v) do
106 for _, v2 in ipairs(a) do
107 if v1 == v2 then
108 toremove[#toremove+1] = v
109 goto endcheck
110 end
111 end
112 end
113 ::endcheck::
114 end
115 return RemoveAll(aa, toremove)
116 end
117
118 -- Converts an array to a string recursively
119 function ToStringArray(t)
120 if string.sub(tostring(t), 1, 6) == "table:" then
121 local out = "{"
122 for index, value in ipairs(t) do
123 out = out .. ToStringArray(value)
124 if t[index + 1] then
125 out = out .. ", "
126 end
127 end
128 return out .. "}"
129 end
130 return tostring(t)
131 end
132
133 -- Converts a table to a string recursively
134 function ToStringTable(t)
135 if string.sub(tostring(t), 1, 6) == "table:" then
136 local out = "{"
137 for index, value in pairs(t) do
138 out = out .. ToStringTable(index) .. " : " .. ToStringTable(value)
139 if t[index + 1] then
140 out = out .. ", "
141 end
142 end
143 return out .. "}"
144 end
145 return tostring(t)
146 end
147
148 -- Check if a table contains an element, and returns the index of that element if it does
149 function Contains(t, e)
150 for index, value in pairs(t) do
151 if value == e then
152 return index
153 end
154 end
155 return false
156 end
157
158 -- Given an array a of unique elements, returns an array of the n-element subarrays of a
159 function Subarrays(a, m)
160 local aout = {}
161 local l = 1
162 local newmax = {}
163
164 if(m <= 0) then
165 return {{}}, {0}
166 end
167
168 local rec, max = Subarrays(a, m - 1)
169
170 for recindex, set in pairs(rec) do
171 for index, element in pairs(a) do
172 if not set[element] and index > max[recindex] then
173 local new = Copy(set)
174 new[#new+1] = element
175 aout[l] = new
176 if not newmax[l] or index > newmax[l] then
177 newmax[l] = index
178 end
179 l = l + 1
180 end
181 end
182 end
183
184 return aout, newmax
185 end