Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-stage-tables.lua

raw
1 -- lua-uni-stage-tables.lua
2 -- Copyright 2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 -- This is mainly an internal module used in lua-uni-data to efficiently store
17 -- the data entries.
18
19
20 -- The source is a 0-based table with integer values fitting into data_bytes bytes.
21 -- The first return value is a 0-based table with values being 1-based indexes into the second table or 0.
22 local function compress(compress_bits, data_bytes, signed, source, upper_limit)
23 local compress_mask = (1 << compress_bits) - 1
24 local default_lower_level = lua.newtable(compress_mask + 1, 0)
25 local pattern = (signed and '>i' or '>I') .. data_bytes
26 local zero_value = string.pack(pattern, 0)
27 for i=1, compress_mask + 1 do
28 default_lower_level[i] = zero_value
29 end
30 local first_stage = upper_limit and lua.newtable((upper_limit >> compress_bits), 1) or {}
31 for k, v in next, source do
32 if v and v ~= 0 and type(k) == 'number' then
33 local high, low = k >> compress_bits, (k & compress_mask) + 1
34 local subtable = first_stage[high]
35 if not subtable then
36 subtable = table.move(default_lower_level, 1, compress_mask + 1, 1, {})
37 first_stage[high] = subtable
38 end
39 subtable[low] = string.pack(pattern, v)
40 end
41 end
42 local default_key = table.concat(default_lower_level)
43 local second_stage_lookup = {[default_key] = 0}
44 local second_stage = {[0] = default_key}
45 for k, v in next, first_stage do
46 local key = table.concat(v)
47 local index = second_stage_lookup[key]
48 if not index then
49 index = #second_stage + 1
50 second_stage_lookup[key] = index
51 second_stage[index] = key
52 end
53 if index ~= 0 then
54 first_stage[k] = index
55 end
56 end
57 if upper_limit then
58 for i = 0, upper_limit >> compress_bits do
59 first_stage[i] = first_stage[i] or 0
60 end
61 end
62 return first_stage, second_stage
63 end
64
65 local function lookup_identity(value) return value end
66 local function lookup_table(t) return function(value) return t[value] end end
67 local function lookup_offset(value, codepoint) return codepoint + value end
68
69 local readers = {
70 [true] = {
71 [1] = sio.readinteger1,
72 [2] = sio.readinteger2,
73 [3] = sio.readinteger3,
74 [4] = sio.readinteger4,
75 },
76 [false] = {
77 [1] = sio.readcardinal1,
78 [2] = sio.readcardinal2,
79 [3] = sio.readcardinal3,
80 [4] = sio.readcardinal4,
81 },
82 }
83
84 local function two_stage_metatable(first_stage, second_stage, lookup, bits, bytes)
85 local lookup_function, signed
86 if lookup == nil then
87 signed = false
88 lookup_function = lookup_identity
89 elseif lookup == 'offset' then
90 signed = true
91 lookup_function = lookup_offset
92 else
93 signed = false
94 lookup_function = lookup_table(lookup)
95 end
96 local reader = assert(readers[signed][bytes])
97 local block_size = 1 << bits
98 local mask = block_size - 1
99 local stride = bytes * block_size
100 return setmetatable({
101 __first_stage = first_stage,
102 __second_stage = second_stage,
103 __lookup = lookup,
104 __bytes = bytes,
105 __bits = bits,
106 }, {
107 __index = function(_, key)
108 if type(key) ~= 'number' then return nil end
109 local high, low = key >> bits, key & mask
110 local second_index = first_stage[high] or 0
111 local value
112 if second_index ~= 0 then
113 value = reader(second_stage, stride * (second_index - 1) + bytes * low + 1)
114 else
115 value = 0
116 end
117 return lookup_function(value, key)
118 end
119 })
120 end
121
122 local function one_stage_metatable(buffer, lookup, bytes)
123 local lookup_function, signed
124 if lookup == nil then
125 signed = false
126 lookup_function = lookup_identity
127 elseif lookup == 'offset' then
128 signed = true
129 lookup_function = lookup_offset
130 else
131 signed = false
132 lookup_function = lookup_table(lookup)
133 end
134 local reader = assert(readers[signed][bytes])
135 return setmetatable({
136 __buffer = buffer,
137 __lookup = lookup,
138 __bytes = bytes,
139 }, {
140 __index = function(_, key)
141 local value = reader(buffer, bytes * key + 1)
142 return lookup_function(value, key)
143 end
144 })
145 end
146
147 local function to_two_stage_lookup(source, lookup, bits, bytes)
148 local first_stage, second_stage = compress(bits, bytes, source)
149 return two_stage_metatable(first_stage, table.concat(second_stage), lookup, codepoint_block_bits, bytes)
150 end
151
152 local function to_three_stage_lookup(source, lookup, bits1, bits2, bytes)
153 local intermediate_stage, third_stage = compress(bits2, bytes, lookup == 'offset', source)
154 local needed_bytes = math.floor(math.log(#third_stage, 256)) + 1
155 local first_stage, second_stage = compress(bits1, needed_bytes, false, intermediate_stage, (0x10FFFF >> bits2) + 1)
156 return two_stage_metatable(two_stage_metatable(first_stage, table.concat(second_stage), nil, bits1, needed_bytes), table.concat(third_stage), lookup, bits2, bytes)
157 end
158
159 -- Cache file format (everything in little endian):
160 -- Header:
161 -- u32: Version (0x00010000)
162 -- u8: Stages (supported: 2 or 3)
163 -- u8: Kind (enum: 0x00 Identity table, 0x01 Delta encoding (signed), 0x02 Mapping table)
164 -- Mapping table: (only present if kind == 0x02
165 -- <Data size in bytes>: Entry count without default
166 -- <u8 size prefixed string>: Default
167 -- <Entry count> * <u8 size prefixed string>: Entries
168 -- Outer table:
169 -- <u32> Number of entries
170 -- <u8> Size of one entry in bytes
171 -- <data>
172 -- Inner tables: (repeated `stages - 1` times)
173 -- <previous entry size bytes> Number of entries
174 -- <u8> Size of underlying value in bytes
175 -- <u8> Bits representing page size (each entry is 2^bits * bytes fields big)
176 -- <data>
177 --
178 local function serialize(nested_table)
179 local buffer = {}
180 local nesting_depth do
181 local current_table = nested_table
182 nesting_depth = 0
183 repeat
184 nesting_depth = nesting_depth + 1
185 current_table = current_table.__first_stage
186 until current_table == nil
187 end
188 local lookup = nested_table.__lookup
189 local kind = lookup == nil and 0 or lookup == 'offset' and 1 or 2
190 buffer[1] = string.pack('>I4BB', 0x00010000, nesting_depth, kind)
191 if kind == 2 then
192 buffer[2] = string.pack('>I4', #lookup)
193 for i=0, #lookup do
194 buffer[3 + i] = string.pack('s1', lookup[i])
195 end
196 end
197 local function serialize_stage(stage, prev)
198 local inner_stage = stage.__first_stage
199 if inner_stage then
200 local nested_bytes = serialize_stage(inner_stage, stage)
201 buffer[#buffer + 1] = string.pack(string.format('>I%sBB', nested_bytes), #stage.__second_stage // (stage.__bytes * (1 << stage.__bits)), stage.__bytes, stage.__bits)
202 buffer[#buffer + 1] = stage.__second_stage
203 return stage.__bytes
204 else
205 local index_bytes = math.floor(math.log(#prev.__second_stage // ((1 << prev.__bits) * prev.__bytes), 256)) + 1
206 buffer[#buffer + 1] = string.pack('>I4B', #stage + 1, index_bytes)
207 buffer[#buffer + 1] = string.pack('>' .. string.rep('I' .. index_bytes, #stage + 1), table.unpack(stage, 0))
208 return index_bytes
209 end
210 end
211 serialize_stage(nested_table)
212 return table.concat(buffer)
213 end
214
215 local function deserialize(data)
216 local offset = 1
217 local version, nesting_depth, kind
218 version, nesting_depth, kind, offset = string.unpack('>I4BB', data, offset)
219 if version ~= 0x00010000 then error'Invalid version' end
220 local lookup
221 if kind == 0 then
222 lookup = nil
223 elseif kind == 1 then
224 lookup = 'offset'
225 elseif kind == 2 then
226 -- TODO
227 local lookup_count
228 lookup_count, offset = string.unpack('>I4', data, offset)
229 lookup = lua.newtable(lookup_count, 1)
230 for i=0, lookup_count do
231 lookup[i], offset = string.unpack('s1', data, offset)
232 end
233 else
234 error'Unsupported type'
235 end
236
237 local nested_table, previous_bytes
238 do
239 local entries, bytes
240 entries, bytes, offset = string.unpack('>I4B', data, offset)
241 local stage_size = entries * bytes
242 local stage = data:sub(offset, offset + stage_size - 1)
243 offset = offset + stage_size
244 nested_table = one_stage_metatable(stage, level == nesting_depth and lookup or nil, bytes)
245 previous_bytes = bytes
246 end
247
248 for level = 2, nesting_depth do
249 local entries, bytes, bits
250 entries, bytes, bits, offset = string.unpack(string.format('>I%sBB', previous_bytes), data, offset) --, #stage.__second_stage // (stage.__bytes * (1 << stage.__bits)), stage.__bytes, stage.__bits)
251 local stage_size = entries * bytes * (1 << bits)
252 local stage = data:sub(offset, offset + stage_size - 1)
253 offset = offset + stage_size
254 nested_table = two_stage_metatable(nested_table, stage, level == nesting_depth and lookup or nil, bits, bytes)
255 previous_bytes = bytes
256 end
257
258 assert(offset == #data + 1)
259 return nested_table
260 end
261
262 return {
263 compress = compress,
264 to_two_stage_lookup = to_two_stage_lookup,
265 to_three_stage_lookup = to_three_stage_lookup,
266 serialize = serialize,
267 deserialize = deserialize,
268 }