From 22158379a7c7de7719eb4cf8e72d48c5b95d2964 Mon Sep 17 00:00:00 2001 From: fjebaker Date: Mon, 12 Aug 2024 18:22:24 +0100 Subject: [PATCH] feat: expose maximum haystack / needle query functions --- src/root.zig | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/root.zig b/src/root.zig index 2a88835..2740083 100644 --- a/src/root.zig +++ b/src/root.zig @@ -116,6 +116,9 @@ pub fn AlgorithmType( traceback_buffer: []usize, + max_haystack: usize, + max_needle: usize, + allocator: std.mem.Allocator, pub fn deinit(self: *Self) void { @@ -165,6 +168,10 @@ pub fn AlgorithmType( .bonus_buffer = bonus_buffer, .first_match_buffer = first_match_buffer, .traceback_buffer = traceback_buffer, + + .max_haystack = max_haystack, + .max_needle = max_needle, + .allocator = allocator, }; } @@ -198,6 +205,9 @@ pub fn AlgorithmType( try self.m.resizeAlloc(new_rows, new_cols); try self.x.resizeAlloc(new_rows, new_cols); try self.m_skip.resizeAlloc(new_rows, new_cols); + + self.max_haystack = max_haystack; + self.max_needle = max_needle; } /// Compute matching score @@ -213,6 +223,18 @@ pub fn AlgorithmType( return info.score; } + /// Return the maximum (`haystack.len <= MAXIMUM`) haystack length that + /// the algorithm has allocated memory for. To increase, use `resize` + pub fn maximumHaystackLen(self: *const Self) usize { + return self.max_haystack; + } + + /// Return the maximum (`needle.len <= MAXIMUM`) needle length that + /// the algorithm has allocated memory for. To increase, use `resize` + pub fn maximumNeedleLen(self: *const Self) usize { + return self.max_needle; + } + const ScoreInfo = struct { score: ScoreT, col_max: usize = 0, @@ -230,8 +252,8 @@ pub fn AlgorithmType( .score = 0, }; - std.debug.assert(haystack.len < self.traceback_buffer.len); - std.debug.assert(needle.len < self.first_match_buffer.len); + std.debug.assert(haystack.len <= self.maximumHaystackLen()); + std.debug.assert(needle.len < self.maximumNeedleLen()); const rows = needle.len; const cols = haystack.len;