-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.zig
354 lines (296 loc) · 12.2 KB
/
generate.zig
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const std = @import("std");
const Allocator = std.mem.Allocator;
// Note: Not very much effort was put into minimizing memory use here, since the
// number of named character references is small enough for the memory usage
// during construction not to be a huge concern.
//
// Much of this implemenation is based on http://stevehanov.ca/blog/?id=115
const DafsaBuilder = struct {
root: *Node,
arena: std.heap.ArenaAllocator.State,
allocator: Allocator,
unchecked_nodes: std.ArrayListUnmanaged(UncheckedNode),
minimized_nodes: std.HashMapUnmanaged(*Node, *Node, Node.DuplicateContext, std.hash_map.default_max_load_percentage),
previous_word_buf: [64]u8 = undefined,
previous_word: []u8 = &[_]u8{},
const UncheckedNode = struct {
parent: *Node,
char: u8,
child: *Node,
};
pub fn init(allocator: Allocator) !DafsaBuilder {
var arena = std.heap.ArenaAllocator.init(allocator);
errdefer arena.deinit();
var root = try arena.allocator().create(Node);
root.* = .{};
return DafsaBuilder{
.root = root,
.allocator = allocator,
.arena = arena.state,
.unchecked_nodes = .{},
.minimized_nodes = .{},
};
}
pub fn deinit(self: *DafsaBuilder) void {
self.arena.promote(self.allocator).deinit();
self.unchecked_nodes.deinit(self.allocator);
self.minimized_nodes.deinit(self.allocator);
self.* = undefined;
}
const Node = struct {
children: [256]?*Node = [_]?*Node{null} ** 256,
is_terminal: bool = false,
number: u12 = 0,
const DuplicateContext = struct {
pub fn hash(ctx: @This(), key: *Node) u64 {
_ = ctx;
var hasher = std.hash.Wyhash.init(0);
std.hash.autoHash(&hasher, key.children);
std.hash.autoHash(&hasher, key.is_terminal);
return hasher.final();
}
pub fn eql(ctx: @This(), a: *Node, b: *Node) bool {
_ = ctx;
return a.is_terminal == b.is_terminal and std.mem.eql(?*Node, &a.children, &b.children);
}
};
pub fn calcNumbers(self: *Node) void {
if (self.number != 0) return;
for (self.children) |maybe_child| {
const child = maybe_child orelse continue;
// A node's number is the sum of the
// numbers of its immediate child nodes.
child.calcNumbers();
if (child.is_terminal) self.number += 1;
self.number += child.number;
}
}
pub fn numDirectChildren(self: *const Node) u8 {
var num: u8 = 0;
for (self.children) |child| {
if (child != null) num += 1;
}
return num;
}
};
pub fn insert(self: *DafsaBuilder, str: []const u8) !void {
if (std.mem.order(u8, str, self.previous_word) == .lt) {
@panic("insertion order must be sorted");
}
var common_prefix_len: usize = 0;
for (0..@min(str.len, self.previous_word.len)) |i| {
if (str[i] != self.previous_word[i]) break;
common_prefix_len += 1;
}
try self.minimize(common_prefix_len);
var node = if (self.unchecked_nodes.items.len == 0)
self.root
else
self.unchecked_nodes.getLast().child;
for (str[common_prefix_len..]) |c| {
std.debug.assert(node.children[c] == null);
var arena = self.arena.promote(self.allocator);
var child = try arena.allocator().create(Node);
self.arena = arena.state;
child.* = .{};
node.children[c] = child;
try self.unchecked_nodes.append(self.allocator, .{
.parent = node,
.char = c,
.child = child,
});
node = node.children[c].?;
}
node.is_terminal = true;
self.previous_word = self.previous_word_buf[0..str.len];
@memcpy(self.previous_word, str);
}
pub fn minimize(self: *DafsaBuilder, down_to: usize) !void {
if (self.unchecked_nodes.items.len == 0) return;
while (self.unchecked_nodes.items.len > down_to) {
const unchecked_node = self.unchecked_nodes.pop();
if (self.minimized_nodes.getPtr(unchecked_node.child)) |child| {
unchecked_node.parent.children[unchecked_node.char] = child.*;
} else {
try self.minimized_nodes.put(self.allocator, unchecked_node.child, unchecked_node.child);
}
}
}
pub fn finish(self: *DafsaBuilder) !void {
try self.minimize(0);
}
fn nodeCount(self: *const DafsaBuilder) usize {
return self.minimized_nodes.count();
}
fn edgeCount(self: *const DafsaBuilder) usize {
var count: usize = 0;
var it = self.minimized_nodes.iterator();
while (it.next()) |entry| {
for (entry.key_ptr.*.children) |child| {
if (child != null) count += 1;
}
}
return count;
}
fn contains(self: *const DafsaBuilder, str: []const u8) bool {
var node = self.root;
for (str) |c| {
node = node.children[c] orelse return false;
}
return node.is_terminal;
}
fn calcNumbers(self: *const DafsaBuilder) void {
self.root.calcNumbers();
}
fn getUniqueIndex(self: *const DafsaBuilder, str: []const u8) ?usize {
var index: usize = 0;
var node = self.root;
for (str) |c| {
const child = node.children[c] orelse return null;
for (node.children, 0..) |sibling, sibling_c| {
if (sibling == null) continue;
if (sibling_c < c) {
index += sibling.?.number;
if (sibling.?.is_terminal) index += 1;
}
}
node = child;
if (node.is_terminal) index += 1;
}
return index;
}
fn writeDafsa(self: *const DafsaBuilder, writer: anytype) !void {
try writer.writeAll("pub const dafsa = [_]Node {\n");
// write root
try writer.writeAll(" .{ .char = 0, .end_of_word = false, .end_of_list = true, .number = 0, .child_index = 1 },\n");
var queue = std.ArrayList(*Node).init(self.allocator);
defer queue.deinit();
var child_indexes = std.AutoHashMap(*Node, u12).init(self.allocator);
defer child_indexes.deinit();
try child_indexes.ensureTotalCapacity(@intCast(self.edgeCount()));
var first_available_index: u12 = self.root.numDirectChildren() + 1;
first_available_index = try writeDafsaChildren(self.root, writer, &queue, &child_indexes, first_available_index);
while (queue.items.len > 0) {
// TODO: something with better time complexity
const node = queue.orderedRemove(0);
first_available_index = try writeDafsaChildren(node, writer, &queue, &child_indexes, first_available_index);
}
try writer.writeAll("};\n");
}
fn writeDafsaChildren(
node: *Node,
writer: anytype,
queue: *std.ArrayList(*Node),
child_indexes: *std.AutoHashMap(*Node, u12),
first_available_index: u12,
) !u12 {
var cur_available_index = first_available_index;
const num_children = node.numDirectChildren();
var child_i: u12 = 0;
for (node.children, 0..) |maybe_child, c_usize| {
const child = maybe_child orelse continue;
const c: u8 = @intCast(c_usize);
const is_last_child = child_i == num_children - 1;
if (!child_indexes.contains(child)) {
const child_num_children = child.numDirectChildren();
if (child_num_children > 0) {
child_indexes.putAssumeCapacityNoClobber(child, cur_available_index);
cur_available_index += child_num_children;
}
try queue.append(child);
}
try writer.print(
" .{{ .char = '{c}', .end_of_word = {}, .end_of_list = {}, .number = {}, .child_index = {} }},\n",
.{ c, child.is_terminal, is_last_child, child.number, child_indexes.get(child) orelse 0 },
);
child_i += 1;
}
return cur_available_index;
}
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
var json_contents = try std.fs.cwd().readFileAlloc(allocator, "entities.json", std.math.maxInt(usize));
defer allocator.free(json_contents);
var parsed = try std.json.parseFromSlice(std.json.Value, allocator, json_contents, .{});
defer parsed.deinit();
var builder = try DafsaBuilder.init(allocator);
defer builder.deinit();
for (parsed.value.object.keys()) |str| {
std.debug.assert(str[0] == '&');
try builder.insert(str[1..]);
}
try builder.finish();
builder.calcNumbers();
// As a sanity check, confirm that the minimal perfect hashing doesn't
// have any collisions
{
var index_set = std.AutoHashMap(usize, void).init(allocator);
defer index_set.deinit();
for (parsed.value.object.keys()) |str| {
const index = builder.getUniqueIndex(str[1..]).?;
const result = try index_set.getOrPut(index);
if (result.found_existing) {
std.debug.print("clobbered {}\n", .{index});
return error.MinimalPerfectHashCollision;
}
}
}
const writer = std.io.getStdOut().writer();
try builder.writeDafsa(writer);
{
const Codepoints = struct {
first: u21,
second: ?u21,
};
var index_to_codepoints = try allocator.alloc(Codepoints, parsed.value.object.count());
defer allocator.free(index_to_codepoints);
var it = parsed.value.object.iterator();
while (it.next()) |entry| {
const codepoints = entry.value_ptr.object.get("codepoints").?.array;
const str = (entry.key_ptr.*)[1..];
const index = builder.getUniqueIndex(str).?;
const array_index = index - 1;
index_to_codepoints[array_index] = .{
.first = @intCast(codepoints.items[0].integer),
.second = if (codepoints.items.len > 1) @intCast(codepoints.items[1].integer) else null,
};
}
try writer.writeAll("pub const codepoints_lookup = [_]Codepoints {\n");
for (index_to_codepoints) |codepoints| {
if (codepoints.second == null) {
try writer.print(" .{{ .first = '\\u{{{X}}}' }},\n", .{codepoints.first});
} else {
const second_name = switch (codepoints.second.?) {
'\u{0338}' => "combining_long_solidus_overlay",
'\u{20D2}' => "combining_long_vertical_line_overlay",
'\u{200A}' => "hair_space",
'\u{0333}' => "combining_double_low_line",
'\u{20E5}' => "combining_reverse_solidus_overlay",
'\u{FE00}' => "variation_selector_1",
'\u{006A}' => "latin_small_letter_j",
'\u{0331}' => "combining_macron_below",
else => unreachable,
};
try writer.print(" .{{ .first = '\\u{{{X}}}', .second = .{s} }},\n", .{ codepoints.first, second_name });
}
}
try writer.writeAll("};\n");
}
}
test {
var builder = try DafsaBuilder.init(std.testing.allocator);
defer builder.deinit();
try builder.insert("hello");
try builder.insert("jealous");
try builder.insert("jello");
try builder.finish();
try std.testing.expect(builder.contains("hello"));
try std.testing.expect(builder.contains("jello"));
try std.testing.expect(builder.contains("jealous"));
try std.testing.expect(!builder.contains("jeal"));
try std.testing.expect(!builder.contains("jealousy"));
try std.testing.expect(!builder.contains("jell"));
}