From c3263b88b95ad5cd655ab1b411744eb4c6a8d1ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Krzy=C5=BCanowski?= Date: Fri, 6 Sep 2024 07:48:49 +0200 Subject: [PATCH] Add eql method to Zig Matrix --- zig-matrix/src/main.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/zig-matrix/src/main.zig b/zig-matrix/src/main.zig index 083d601..6c3fd88 100644 --- a/zig-matrix/src/main.zig +++ b/zig-matrix/src/main.zig @@ -208,6 +208,13 @@ const Matrix = struct { ); } + pub fn eql(self: Matrix, other: Matrix) bool { + const dimEqual = (self.cols == other.cols) and (self.rows == other.rows); + const numEqual = std.mem.eql(i64, self.numbers, other.numbers); + + return dimEqual and numEqual; + } + pub fn deinit(self: Matrix) void { self.allocator.free(self.numbers); } @@ -402,3 +409,34 @@ test "matrix mul" { try std.testing.expect(try mat3.get(.{ 1, 0 }) == 49); try std.testing.expect(try mat3.get(.{ 1, 1 }) == 64); } + +test "matrix eql" { + const mat1 = try Matrix.init( + std.testing.allocator, + 2, + 3, + &.{ 1, 2, 3, 4, 5, 6 }, + ); + defer mat1.deinit(); + + const mat2 = try Matrix.init( + std.testing.allocator, + 2, + 3, + &.{ 1, 2, 3, 4, 5, 6 }, + ); + defer mat2.deinit(); + + const mat3 = try Matrix.init( + std.testing.allocator, + 3, + 2, + &.{ 1, 2, 3, 4, 5, 6 }, + ); + defer mat3.deinit(); + + try std.testing.expect(mat1.eql(mat1)); + try std.testing.expect(mat1.eql(mat2)); + try std.testing.expect(!mat1.eql(mat3)); + try std.testing.expect(!mat2.eql(mat3)); +}