1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
const std = @import("std");
const vector = std.meta.Vector(2, i64);
pub const Stack = struct {
data: std.ArrayList(i64),
pub fn clear(self: *Stack) void {
self.data.clearRetainingCapacity();
}
pub fn deinit(self: *Stack) void {
self.data.deinit();
}
pub fn duplicate(self: *Stack) !void {
if (self.data.items.len > 0) {
try self.push(self.data.items[self.data.items.len - 1]);
}
}
test "duplicate" {
var s = Stack.init(std.testing.allocator);
defer s.deinit();
try s.duplicate();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
try s.pushVector([2]i64{ 1, 2 });
try s.duplicate();
try s.duplicate();
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 pop(self: *Stack) i64 {
return if (self.data.popOrNull()) |v| v else 0;
}
pub fn popVector(self: *Stack) [2]i64 {
const b = if (self.data.popOrNull()) |v| v else 0;
const a = if (self.data.popOrNull()) |v| v else 0;
return [2]i64{ a, b };
}
pub fn push(self: *Stack, n: i64) !void {
try self.data.append(n);
}
pub fn pushVector(self: *Stack, v: [2]i64) !void {
try self.data.appendSlice(v[0..]);
}
test "pushVector" {
var s = Stack.init(std.testing.allocator);
defer s.deinit();
try s.pushVector([2]i64{ 1, -1 });
try s.pushVector([2]i64{ 2, -2 });
try std.testing.expectEqual(s.data.items.len, 4);
try std.testing.expectEqual(s.data.items[0], 1);
try std.testing.expectEqual(s.data.items[1], -1);
try std.testing.expectEqual(s.data.items[2], 2);
try std.testing.expectEqual(s.data.items[3], -2);
}
pub fn swap(self: *Stack) !void {
const v = self.popVector();
try self.pushVector([2]i64{ v[1], v[0] });
}
test "swap" {
var s = Stack.init(std.testing.allocator);
defer s.deinit();
try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 0, 0 }));
try s.push(1);
try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 1, 0 }));
try s.push(2);
try s.swap();
try std.testing.expectEqual(s.popVector(), @as(vector, [2]i64{ 2, 0 }));
}
//pub fn transfert(toss: *Stack, soss: *Stack, n: u64) !void {
//}
};
test "all" {
std.testing.refAllDecls(@This());
}
|