aboutsummaryrefslogtreecommitdiff
path: root/src/stack.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-05-05 22:28:07 +0200
committerJulien Dessaux2022-05-05 22:28:07 +0200
commit582bfc893c01cccec0aae7ea7eec18bcf97cd836 (patch)
tree3822ac4b2490fc3249093e3866d1615e20eaf470 /src/stack.zig
parentFinished implementing the funge field (diff)
downloadzigfunge98-582bfc893c01cccec0aae7ea7eec18bcf97cd836.tar.gz
zigfunge98-582bfc893c01cccec0aae7ea7eec18bcf97cd836.tar.bz2
zigfunge98-582bfc893c01cccec0aae7ea7eec18bcf97cd836.zip
Fixed a misunderstanding about allocations (I did too much go and nim!)
Diffstat (limited to 'src/stack.zig')
-rw-r--r--src/stack.zig17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/stack.zig b/src/stack.zig
index 6ba587b..ce70df0 100644
--- a/src/stack.zig
+++ b/src/stack.zig
@@ -2,12 +2,14 @@ const std = @import("std");
const vector = std.meta.Vector(2, i64);
pub const Stack = struct {
+ allocator: std.mem.Allocator,
data: std.ArrayList(i64),
pub fn clear(self: *Stack) void {
self.data.clearRetainingCapacity();
}
pub fn deinit(self: *Stack) void {
self.data.deinit();
+ self.allocator.destroy(self);
}
pub fn duplicate(self: *Stack) !void {
if (self.data.items.len > 0) {
@@ -15,7 +17,7 @@ pub const Stack = struct {
}
}
test "duplicate" {
- var s = Stack.init(std.testing.allocator);
+ var s = try Stack.init(std.testing.allocator);
defer s.deinit();
try s.duplicate();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
@@ -25,10 +27,11 @@ pub const Stack = struct {
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 2 }));
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 2 }));
}
- pub fn init(allocator: std.mem.Allocator) Stack {
- return Stack{
- .data = std.ArrayList(i64).init(allocator),
- };
+ pub fn init(allocator: std.mem.Allocator) !*Stack {
+ var s = try allocator.create(Stack);
+ s.allocator = allocator;
+ s.data = std.ArrayList(i64).init(allocator);
+ return s;
}
pub fn pop(self: *Stack) i64 {
return if (self.data.popOrNull()) |v| v else 0;
@@ -45,7 +48,7 @@ pub const Stack = struct {
try self.data.appendSlice(v[0..]);
}
test "pushVector" {
- var s = Stack.init(std.testing.allocator);
+ var s = try Stack.init(std.testing.allocator);
defer s.deinit();
try s.pushVector([2]i64{ 1, -1 });
try s.pushVector([2]i64{ 2, -2 });
@@ -60,7 +63,7 @@ pub const Stack = struct {
try self.pushVector([2]i64{ v[1], v[0] });
}
test "swap" {
- var s = Stack.init(std.testing.allocator);
+ var s = try Stack.init(std.testing.allocator);
defer s.deinit();
try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));