Zig utility library
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

I forgot ArrayList reallocations can invalidate pointers: In BinaryTree and Queue use ArrayLits of pointers instead of the actual nodes

IamPyu 20c04009 208e9aac

+27 -11
+11 -8
src/BinaryTree.zig
··· 18 18 return Node{ .data = x }; 19 19 } 20 20 21 - pub fn initAlloc(allocator: Allocator, x: T) !*Node { 22 - var node = try allocator.create(Node); 21 + pub fn initAlloc(gpa: Allocator, x: T) !*Node { 22 + var node = try gpa.create(Node); 23 23 node.data = x; 24 24 return node; 25 25 } ··· 91 91 return struct { 92 92 const Self = @This(); 93 93 const BT = BinaryTree(T); 94 - const NodeList = ArrayList(BT.Node); 94 + const NodeList = ArrayList(*BT.Node); 95 95 96 96 allocator: Allocator, 97 97 binary_tree: BT, 98 98 nodes: NodeList, 99 99 100 - pub fn init(allocator: Allocator) !Self { 100 + pub fn init(gpa: Allocator) !Self { 101 101 return Self{ 102 - .allocator = allocator, 102 + .allocator = gpa, 103 103 .binary_tree = BT.init(), 104 - .nodes = try NodeList.initCapacity(allocator, ARRAY_LIST_CAPACITY), 104 + .nodes = try NodeList.initCapacity(gpa, ARRAY_LIST_CAPACITY), 105 105 }; 106 106 } 107 107 108 108 pub fn deinit(self: *Self) void { 109 + for (self.nodes.items) |node| { 110 + self.allocator.destroy(node); 111 + } 109 112 self.nodes.deinit(self.allocator); 110 113 } 111 114 112 115 pub fn insert(self: *Self, value: T) !*BT.Node { 113 - try self.nodes.append(self.allocator, BT.Node.init(value)); 114 - const node = &self.nodes.items[self.nodes.items.len - 1]; 116 + try self.nodes.append(self.allocator, try BT.Node.initAlloc(self.allocator, value)); 117 + const node = self.nodes.items[self.nodes.items.len - 1]; 115 118 return self.binary_tree.insert(node); 116 119 } 117 120 };
+16 -3
src/Queue.zig
··· 15 15 pub fn init(value: T) Node { 16 16 return Node{ .value = value }; 17 17 } 18 + 19 + pub fn initAlloc(gpa: Allocator, value: T) !*Node { 20 + const ptr = try gpa.create(Node); 21 + ptr.* = Node.init(value); 22 + return ptr; 23 + } 18 24 }; 19 25 20 26 first: ?*Node = null, ··· 90 96 return struct { 91 97 const Self = @This(); 92 98 const IQueue = Queue(T); 93 - const NodeList = std.ArrayList(IQueue.Node); 99 + const NodeList = std.ArrayList(*IQueue.Node); 94 100 95 101 allocator: Allocator, 96 102 queue: IQueue, ··· 107 113 108 114 /// Deinitialize the queue 109 115 pub fn deinit(self: *Self) void { 116 + for (self.nodes.items) |node| { 117 + self.allocator.destroy(node); 118 + } 110 119 self.nodes.deinit(self.allocator); 111 120 } 112 121 113 122 /// Enqueue a value 114 123 pub fn enqueue(self: *Self, value: T) !void { 115 - try self.nodes.append(self.allocator, IQueue.Node.init(value)); 116 - const node = &self.nodes.items[self.nodes.items.len - 1]; 124 + try self.nodes.append(self.allocator, try IQueue.Node.initAlloc(self.allocator, value)); 125 + const node = self.nodes.items[self.nodes.items.len - 1]; 117 126 self.queue.enqueue(node); 118 127 } 119 128 ··· 132 141 try queue.enqueue(2); 133 142 _ = queue.dequeue(); 134 143 try queue.enqueue(3); 144 + 145 + for (0..50) |i| { 146 + try queue.enqueue(@intCast(i)); 147 + } 135 148 136 149 try std.testing.expectEqual(2, queue.dequeue().?.value); 137 150 try std.testing.expectEqual(3, queue.dequeue().?.value);