Skip to content

Commit

Permalink
feat: expose maximum haystack / needle query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fjebaker committed Aug 12, 2024
1 parent 967df94 commit 2215837
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 2215837

Please sign in to comment.