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
|
pub fn TableSection(comptime Entry: type) type {
return struct {
entries: std.ArrayListUnmanaged(Entry) = .{},
free_list: std.ArrayListUnmanaged(Index) = .{},
lookup: std.AutoHashMapUnmanaged(Entry, Index) = .{},
pub fn deinit(self: *Self, allocator: Allocator) void {
self.entries.deinit(allocator);
self.free_list.deinit(allocator);
self.lookup.deinit(allocator);
}
pub fn allocateEntry(self: *Self, allocator: Allocator, entry: Entry) Allocator.Error!Index {
try self.entries.ensureUnusedCapacity(allocator, 1);
const index = blk: {
if (self.free_list.popOrNull()) |index| {
log.debug(" (reusing entry index {d})", .{index});
break :blk index;
} else {
log.debug(" (allocating entry at index {d})", .{self.entries.items.len});
const index = @as(u32, @intCast(self.entries.items.len));
_ = self.entries.addOneAssumeCapacity();
break :blk index;
}
};
self.entries.items[index] = entry;
try self.lookup.putNoClobber(allocator, entry, index);
return index;
}
pub fn freeEntry(self: *Self, allocator: Allocator, entry: Entry) void {
const index = self.lookup.get(entry) orelse return;
self.free_list.append(allocator, index) catch {};
self.entries.items[index] = undefined;
_ = self.lookup.remove(entry);
}
pub fn count(self: Self) usize {
return self.entries.items.len;
}
pub fn format(
self: Self,
comptime unused_format_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
comptime assert(unused_format_string.len == 0);
try writer.writeAll("TableSection:\n");
for (self.entries.items, 0..) |entry, i| {
try writer.print(" {d} => {}\n", .{ i, entry });
}
}
const Self = @This();
pub const Index = u32;
};
}
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.link);
const Allocator = std.mem.Allocator;
|