further along with popcon

This commit is contained in:
Motiejus Jakštys 2022-02-16 11:21:51 +02:00 committed by Motiejus Jakštys
parent f584642cca
commit b43ba9c672

View File

@ -17,11 +17,10 @@ const ShellIndex = struct {
// MaxShells is the maximum number of "popular" shells. // MaxShells is the maximum number of "popular" shells.
const MaxShells = 63; const MaxShells = 63;
const MaxShellLen = 64;
// ShellPopcon is a shell popularity contest: collect shells and return the // ShellPopcon is a shell popularity contest: collect shells and return the
// popular ones, sorted by score. score := len(shell) * number_of_shells. // popular ones, sorted by score. score := len(shell) * number_of_shells.
// String values are copied, the returned slice of shells is allocated
// using an allocator.
const ShellPopcon = struct { const ShellPopcon = struct {
counts: std.StringHashMap(u32), counts: std.StringHashMap(u32),
allocator: Allocator, allocator: Allocator,
@ -32,20 +31,50 @@ const ShellPopcon = struct {
}; };
const ShellSections = struct { const ShellSections = struct {
index: []ShellIndex, index: BoundedArray(ShellIndex, MaxShells),
blob: []const u8, blob: BoundedArray(u8, MaxShells * MaxShellLen),
offsets: StringHashMap(u10), offsets: StringHashMap(u10),
pub fn getOffset(self: *ShellSections, shell: []const u8) ?u10 { // initializes and populates shell sections. All strings are copied, nothing is owned.
return self.offsets.get(shell); pub fn init(allocator: Allocator, shells: BoundedArray([]const u8, MaxShells)) !ShellSections {
var self = ShellSections{
.index = try BoundedArray(ShellIndex, MaxShells).init(shells.len),
.blob = try BoundedArray(u8, MaxShells * MaxShellLen).init(0),
.offsets = StringHashMap(u10).init(allocator),
};
var offset: u10 = 0;
var idx: u10 = 0;
while (idx < shells.len) {
//const stderr = std.io.getStdErr().writer();
//try stderr.print("\n", .{});
const len = @intCast(u6, shells.get(idx).len);
try self.blob.appendSlice(shells.get(idx));
const ourShell = self.blob.constSlice()[offset .. offset + len];
try self.offsets.put(ourShell, idx);
self.index.set(idx, ShellIndex{
.offset = offset >> 2, // all shells are padded by 4
.len = len,
});
offset += len;
// if offset is not multiple by 4, make it so, and append the offset.
const padding = (offset + 3) & ~@intCast(u10, 3);
offset += padding;
try self.blob.appendNTimes(0, padding);
idx += 1;
}
return self;
} }
// initializes ShellSections. All strings are copied, nothing is owned. pub fn deinit(self: *ShellSections) void {
pub fn init(allocator: Allocator, shells: BoundedArray([]const u8, MaxShells)) ShellSections { self.offsets.deinit();
self.offsets = StringHashMap(u10).init(allocator); self.* = undefined;
_ = allocator; }
_ = shells;
pub fn getOffset(self: *ShellSections, shell: []const u8) ?u10 {
return self.offsets.get(shell);
} }
}; };
@ -82,10 +111,10 @@ const ShellPopcon = struct {
return std.math.order(a.score, b.score); return std.math.order(a.score, b.score);
} }
pub fn getSections(self: *Self, limit: u32) ShellSections { // toOwnedSections returns the analyzed ShellSections. Resets the shell
const stderr = std.io.getStdErr().writer(); // popularity contest. ShellSections memory is allocated by the ShellPopcon
_ = stderr; // allocator, and must be deInit'ed by the caller.
pub fn toOwnedSections(self: *Self, limit: u10) !ShellSections {
var deque = PriorityDequeue(KV, void, cmpShells).init(self.allocator, {}); var deque = PriorityDequeue(KV, void, cmpShells).init(self.allocator, {});
defer deque.deinit(); defer deque.deinit();
@ -99,55 +128,45 @@ const ShellPopcon = struct {
} }
const total = std.math.min(deque.count(), limit); const total = std.math.min(deque.count(), limit);
var strSlice = self.allocator.alloc([]u8, total); var topShells = try BoundedArray([]const u8, MaxShells).init(total);
defer strSlice.deinit();
var i: u32 = 0; var i: u32 = 0;
while (i < total) { while (i < total) {
strSlice[i] = deque.removeMax(); const elem: []const u8 = deque.removeMax().shell;
topShells.set(i, elem);
i += 1; i += 1;
} }
return ShellSections.init(self.allocator, strSlice); const result = ShellSections.init(self.allocator, topShells);
const allocator = self.allocator;
self.* = init(allocator);
return result;
} }
}; };
test "[]u8 comparison" { test "basic shellpopcon" {
var s1: []const u8 = "/bin/bash";
var s2: []const u8 = "/bin/bash";
try testing.expectEqual(s1, s2);
}
test "basic shellpop" {
var popcon = ShellPopcon.init(testing.allocator); var popcon = ShellPopcon.init(testing.allocator);
defer popcon.deinit(); defer popcon.deinit();
try popcon.put("/bin/bash"); const bash = "/bin/bash"; // 9 chars
try popcon.put("/bin/bash"); const zsh = "/bin/zsh"; // 8 chars
try popcon.put("/bin/bash"); const nobody = "/bin/nobody"; // only 1 instance, ought to ignore
try popcon.put("/bin/zsh"); const long = "/bin/very-long-shell-name-ought-to-be-first";
try popcon.put("/bin/zsh"); const input = [_][]const u8{
try popcon.put("/bin/zsh"); zsh, zsh, zsh, zsh, // zsh score 8*4=32
try popcon.put("/bin/zsh"); bash, bash, bash, nobody, // bash score 3*9=27
try popcon.put("/bin/nobody"); long, long, // long score 2*42=84
try popcon.put("/bin/very-long-shell-name-ought-to-be-first"); };
try popcon.put("/bin/very-long-shell-name-ought-to-be-first");
const stderr = std.io.getStdErr().writer(); for (input) |shell| {
try popcon.put(shell);
var topshells = try popcon.top(2); }
defer topshells.deinit();
var shellStrings = topshells.keys(); var sections = try popcon.toOwnedSections(MaxShells);
try testing.expectEqual(shellStrings.len, 2); defer sections.deinit();
try testing.expectEqual(sections.index.len, 3); // all but "nobody" qualify
try stderr.print("\n", .{});
try stderr.print("0th type: {s}\n", .{@typeName(@TypeOf(shellStrings[0]))}); try testing.expectEqual(sections.getOffset(long).?, 0);
try stderr.print("1st type: {s}\n", .{@typeName(@TypeOf(shellStrings[1]))}); try testing.expectEqual(sections.getOffset(zsh).?, 1);
try stderr.print("0th: {s}, len: {d}\n", .{ shellStrings[0], shellStrings[0].len }); try testing.expectEqual(sections.getOffset(bash).?, 2);
try stderr.print("0ww: /bin/very-long-shell-name-ought-to-be-first\n", .{});
try stderr.print("1st: {s}, len: {d}\n", .{ shellStrings[1], shellStrings[1].len });
try stderr.print("1ww: /bin/zsh\n", .{});
try testing.expectEqual(shellStrings[0], "/bin/very-long-shell-name-ought-to-be-first");
try testing.expectEqual(shellStrings[1], "/bin/zsh");
} }