From daccbe4f5f81a197d4809fe84c74a2c663bc02e1 Mon Sep 17 00:00:00 2001 From: PEZ Date: Wed, 18 Dec 2024 22:43:10 +0100 Subject: [PATCH 1/2] levenshtein: Zig: use memcopy and avoid using allocators --- levenshtein/zig/code.zig | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/levenshtein/zig/code.zig b/levenshtein/zig/code.zig index bb01c32..f45a4eb 100644 --- a/levenshtein/zig/code.zig +++ b/levenshtein/zig/code.zig @@ -3,12 +3,7 @@ const std = @import("std"); /// Calculates the Levenshtein distance between two strings using Wagner-Fischer algorithm /// Space Complexity: O(min(m,n)) - only uses two arrays instead of full matrix /// Time Complexity: O(m*n) where m and n are the lengths of the input strings -fn levenshteinDistance(allocator: std.mem.Allocator, s1: []const u8, s2: []const u8) !usize { - // Early termination checks - if (std.mem.eql(u8, s1, s2)) return 0; - if (s1.len == 0) return s2.len; - if (s2.len == 0) return s1.len; - +fn levenshteinDistance(s1: []const u8, s2: []const u8) usize { // Make s1 the shorter string for space optimization const str1 = if (s1.len > s2.len) s2 else s1; const str2 = if (s1.len > s2.len) s1 else s2; @@ -17,14 +12,12 @@ fn levenshteinDistance(allocator: std.mem.Allocator, s1: []const u8, s2: []const const n = str2.len; // Use two arrays instead of full matrix for space optimization - var prev_row = try allocator.alloc(usize, m + 1); - defer allocator.free(prev_row); - var curr_row = try allocator.alloc(usize, m + 1); - defer allocator.free(curr_row); + var prev_row: [256]usize = undefined; + var curr_row: [256]usize = undefined; // Initialize first row - for (prev_row, 0..) |*cell, i| { - cell.* = i; + for (0..m + 1) |i| { + prev_row[i] = i; } // Main computation loop @@ -47,9 +40,7 @@ fn levenshteinDistance(allocator: std.mem.Allocator, s1: []const u8, s2: []const } // Swap rows - const temp = prev_row; - prev_row = curr_row; - curr_row = temp; + @memcpy(prev_row[0..m + 1], curr_row[0..m + 1]); } return prev_row[m]; @@ -78,7 +69,7 @@ pub fn main() !void { var j: usize = 1; while (j < args.len) : (j += 1) { if (i != j) { - const distance = try levenshteinDistance(allocator, args[i], args[j]); + const distance = levenshteinDistance(args[i], args[j]); if (min_distance == -1 or distance < @as(usize, @intCast(min_distance))) { min_distance = @as(isize, @intCast(distance)); } From 4fbe62d87ea03a60cee8a76bb6b143fa76ee6de1 Mon Sep 17 00:00:00 2001 From: PEZ Date: Wed, 18 Dec 2024 22:50:14 +0100 Subject: [PATCH 2/2] levenshtein: Zig: reinstall early termination checks --- levenshtein/zig/code.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/levenshtein/zig/code.zig b/levenshtein/zig/code.zig index f45a4eb..a24fc7e 100644 --- a/levenshtein/zig/code.zig +++ b/levenshtein/zig/code.zig @@ -4,6 +4,11 @@ const std = @import("std"); /// Space Complexity: O(min(m,n)) - only uses two arrays instead of full matrix /// Time Complexity: O(m*n) where m and n are the lengths of the input strings fn levenshteinDistance(s1: []const u8, s2: []const u8) usize { + // Early termination checks + if (std.mem.eql(u8, s1, s2)) return 0; + if (s1.len == 0) return s2.len; + if (s2.len == 0) return s1.len; + // Make s1 the shorter string for space optimization const str1 = if (s1.len > s2.len) s2 else s1; const str2 = if (s1.len > s2.len) s1 else s2;