main.zig 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const std = @import("std");
  2. fn Queue(comptime T: type) type {
  3. const QueueNode = struct {
  4. const Self = @This();
  5. value: T,
  6. next: ?*Self,
  7. };
  8. return struct {
  9. const Self = @This();
  10. head: ?*QueueNode,
  11. tail: ?*QueueNode,
  12. alloc: std.mem.Allocator,
  13. fn new(alloc: std.mem.Allocator) Self {
  14. return .{
  15. .alloc = alloc,
  16. .head = null,
  17. .tail = null,
  18. };
  19. }
  20. fn enqueue(self: *Self, value: T) !void {
  21. var node = try self.alloc.create(QueueNode);
  22. node.value = value;
  23. node.next = null;
  24. if (self.head == null) {
  25. self.head = node;
  26. self.tail = node;
  27. }
  28. if (self.tail) |t_node| {
  29. var next = t_node;
  30. next.next = node;
  31. }
  32. self.tail = node;
  33. }
  34. fn dequeue(self: *Self) ?T {
  35. if (self.head) |t_head| {
  36. self.head = t_head.next;
  37. const value = t_head.value;
  38. self.alloc.destroy(t_head);
  39. return value;
  40. }
  41. return null;
  42. }
  43. fn print(self: *Self) void {
  44. var curr = self.head;
  45. std.log.info("queue", .{});
  46. while (curr) |node| {
  47. std.log.info("-> {d}", .{node.value});
  48. curr = node.next;
  49. }
  50. }
  51. };
  52. }
  53. const IntQueue = Queue(i32);
  54. const Data = struct { val: usize };
  55. const StructQueue = Queue(*Data);
  56. pub fn main() !void {
  57. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  58. defer arena.deinit();
  59. const allocator = arena.allocator();
  60. var q = IntQueue.new(allocator);
  61. try q.enqueue(32);
  62. try q.enqueue(45);
  63. try q.enqueue(33);
  64. q.print();
  65. std.log.info("-----", .{});
  66. const x = q.dequeue();
  67. _ = x;
  68. q.print();
  69. }
  70. test "filled queue" {
  71. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  72. defer arena.deinit();
  73. const allocator = arena.allocator();
  74. var q = IntQueue.new(allocator);
  75. try q.enqueue(20);
  76. try q.enqueue(30);
  77. if (q.dequeue()) |value| {
  78. std.testing.expect(value == 20) catch {
  79. std.debug.print("filled queue: got error: 20=={}", .{value});
  80. };
  81. }
  82. }
  83. test "struct* queue" {
  84. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  85. defer arena.deinit();
  86. const allocator = arena.allocator();
  87. var q = StructQueue.new(allocator);
  88. for (0..5) |idx| {
  89. var d = Data{ .val = idx };
  90. try q.enqueue(&d);
  91. }
  92. if (q.dequeue()) |value| {
  93. std.testing.expect(value.*.val == 0) catch {
  94. std.debug.print("struct* queue: got error: 0=={}", .{value.*.val});
  95. };
  96. }
  97. }
  98. test "empty queue" {
  99. const arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  100. defer arena.deinit();
  101. const allocator = arena.allocator();
  102. var q = IntQueue.new(allocator);
  103. const x = q.dequeue();
  104. try std.testing.expect(x == null);
  105. }