expose native shell sections

This commit is contained in:
2022-03-05 06:08:01 +02:00
committed by Motiejus Jakštys
parent 869f3f3a65
commit e17b2c9641
3 changed files with 45 additions and 54 deletions

View File

@@ -54,7 +54,7 @@ pub const ShellWriter = struct {
score: u64,
};
const ShellSections = struct {
pub const ShellSections = struct {
index: BoundedArray(ShellIndex, max_shells),
blob: BoundedArray(u8, max_shells * max_shell_len),
indices: StringHashMap(u6),
@@ -71,15 +71,16 @@ pub const ShellWriter = struct {
.blob = try BoundedArray(u8, max_shells * max_shell_len).init(0),
.indices = StringHashMap(u6).init(allocator),
};
errdefer self.indices.deinit();
var fullOffset: u12 = 0;
var idx: u6 = 0;
while (idx < shells.len) : (idx += 1) {
const len = @intCast(u6, shells.get(idx).len);
const len = try std.math.cast(u6, shells.get(idx).len);
try self.blob.appendSlice(shells.get(idx));
const ourShell = self.blob.constSlice()[fullOffset .. fullOffset + len];
try self.indices.put(ourShell, idx);
self.index.set(idx, ShellIndex{
.offset = @intCast(u10, fullOffset >> 2),
.offset = try std.math.cast(u10, fullOffset >> 2),
.len = len - 1,
});
@@ -118,26 +119,23 @@ pub const ShellWriter = struct {
pub fn deinit(self: *ShellWriter) void {
var it = self.counts.keyIterator();
while (it.next()) |key_ptr| {
while (it.next()) |key_ptr|
self.counts.allocator.free(key_ptr.*);
}
self.counts.deinit();
self.* = undefined;
}
pub fn put(self: *ShellWriter, shell: []const u8) !void {
const res = try self.counts.getOrPutAdapted(shell, self.counts.ctx);
if (res.found_existing) {
res.value_ptr.* += 1;
} else {
if (!res.found_existing) {
res.key_ptr.* = try self.allocator.dupe(u8, shell);
res.value_ptr.* = 1;
} else {
res.value_ptr.* += 1;
}
}
fn cmpShells(context: void, a: KV, b: KV) std.math.Order {
_ = context;
fn cmpShells(_: void, a: KV, b: KV) std.math.Order {
return std.math.order(a.score, b.score);
}
@@ -161,10 +159,8 @@ pub const ShellWriter = struct {
var topShells = try BoundedArray([]const u8, max_shells).init(total);
var i: u32 = 0;
while (i < total) : (i += 1) {
const elem = deque.removeMax().shell;
topShells.set(i, elem);
}
while (i < total) : (i += 1)
topShells.set(i, deque.removeMax().shell);
const result = ShellSections.init(self.allocator, topShells);
const allocator = self.allocator;
@@ -178,7 +174,6 @@ const testing = std.testing;
test "basic shellpopcon" {
var popcon = ShellWriter.init(testing.allocator);
defer popcon.deinit();
const bash = "/bin/bash"; // 9 chars
const zsh = "/bin/zsh"; // 8 chars