1
0
Fork 0

fixed the funge stack types

This commit is contained in:
Julien Dessaux 2022-02-18 01:07:14 +01:00
parent eb73b96d9b
commit a6d0b334d9
Signed by: adyxax
GPG key ID: F92E51B86E07177E

View file

@ -1,8 +1,8 @@
const std = @import("std"); const std = @import("std");
const vector = std.meta.Vector(2, u64); const vector = std.meta.Vector(2, i64);
pub const Stack = struct { pub const Stack = struct {
data: std.ArrayList(u64), data: std.ArrayList(i64),
pub fn clear(self: *Stack) void { pub fn clear(self: *Stack) void {
self.data.clearRetainingCapacity(); self.data.clearRetainingCapacity();
} }
@ -14,37 +14,41 @@ pub const Stack = struct {
try self.push(self.data.items[self.data.items.len - 1]); try self.push(self.data.items[self.data.items.len - 1]);
} }
} }
pub fn init(comptime allocator: std.mem.Allocator) Stack { pub fn init(allocator: std.mem.Allocator) Stack {
return Stack{ return Stack{
.data = std.ArrayList(u64).init(allocator), .data = std.ArrayList(i64).init(allocator),
}; };
} }
pub fn pop(self: *Stack) u64 { pub fn pop(self: *Stack) i64 {
return if (self.data.popOrNull()) |v| v else 0; return if (self.data.popOrNull()) |v| v else 0;
} }
pub fn popVector(self: *Stack) [2]u64 { pub fn popVector(self: *Stack) [2]i64 {
const b = if (self.data.popOrNull()) |v| v else 0; const b = if (self.data.popOrNull()) |v| v else 0;
const a = if (self.data.popOrNull()) |v| v else 0; const a = if (self.data.popOrNull()) |v| v else 0;
return [2]u64{ a, b }; return [2]i64{ a, b };
} }
pub fn push(self: *Stack, n: u64) !void { pub fn push(self: *Stack, n: i64) !void {
try self.data.append(n); try self.data.append(n);
} }
pub fn pushVector(self: *Stack, v: [2]u64) !void { pub fn pushVector(self: *Stack, v: [2]i64) !void {
try self.data.appendSlice(v[0..2]); try self.data.appendSlice(v[0..2]);
} }
pub fn swap(self: *Stack) !void { pub fn swap(self: *Stack) !void {
const v = self.popVector(); const v = self.popVector();
try self.pushVector([2]u64{ v[1], v[0] }); try self.pushVector([2]i64{ v[1], v[0] });
} }
}; };
test "all" {
std.testing.refAllDecls(@This());
}
test "clear" { test "clear" {
var s = Stack.init(std.testing.allocator); var s = Stack.init(std.testing.allocator);
defer s.deinit(); defer s.deinit();
s.clear(); s.clear();
try std.testing.expect(s.pop() == 0); try std.testing.expect(s.pop() == 0);
try s.pushVector([2]u64{ 1, 2 }); try s.pushVector([2]i64{ 1, 2 });
s.clear(); s.clear();
try std.testing.expect(s.pop() == 0); try std.testing.expect(s.pop() == 0);
} }
@ -52,34 +56,34 @@ test "duplicate" {
var s = Stack.init(std.testing.allocator); var s = Stack.init(std.testing.allocator);
defer s.deinit(); defer s.deinit();
try s.duplicate(); try s.duplicate();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
try s.pushVector([2]u64{ 1, 2 }); try s.pushVector([2]i64{ 1, 2 });
try s.duplicate(); try s.duplicate();
try s.duplicate(); try s.duplicate();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 2, 2 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 2 }));
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 1, 2 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 2 }));
} }
test "push and pop" { test "push and pop" {
var s = Stack.init(std.testing.allocator); var s = Stack.init(std.testing.allocator);
defer s.deinit(); defer s.deinit();
try std.testing.expect(s.pop() == 0); try std.testing.expect(s.pop() == 0);
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
try s.push(1); try s.push(1);
try std.testing.expect(s.pop() == 1); try std.testing.expect(s.pop() == 1);
try std.testing.expect(s.pop() == 0); try std.testing.expect(s.pop() == 0);
try s.pushVector([2]u64{ 2, 3 }); try s.pushVector([2]i64{ 2, 3 });
try std.testing.expect(s.pop() == 3); try std.testing.expect(s.pop() == 3);
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 2 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 2 }));
} }
test "swap" { test "swap" {
var s = Stack.init(std.testing.allocator); var s = Stack.init(std.testing.allocator);
defer s.deinit(); defer s.deinit();
try s.swap(); try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 0, 0 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
try s.push(1); try s.push(1);
try s.swap(); try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 1, 0 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 0 }));
try s.push(2); try s.push(2);
try s.swap(); try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]u64{ 2, 0 })); try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 0 }));
} }