diff --git a/primitives.zig b/primitives.zig index 1197a54..6e4e3a0 100644 --- a/primitives.zig +++ b/primitives.zig @@ -32,15 +32,18 @@ pub fn LinkedList() type { head: ?*Node, length: u32, allocator: std.mem.Allocator, + current: ?*Node, pub fn new(allocator: std.mem.Allocator) Self { return .{ .head = null, + .current = null, .length = 0, .allocator = allocator, }; } - pub fn add(self: *Self, allocator: std.mem.Allocator, point: Point) !void { + pub fn add(self: *Self, point: Point) !void { + var allocator = self.allocator; var newNode = allocator.create(Node) catch |err| { std.debug.print("error allocation, {}", .{err}); return err; @@ -52,6 +55,18 @@ pub fn LinkedList() type { self.head = newNode; self.length += 1; } + pub fn next(self: *Self) Point { + var current: ?*Node = undefined; + if (self.current != null) { + current = self.current; + self.current = self.current.?.next; + } else { + current = self.head; + self.current = self.head; + } + + return current.?.point; + } pub fn dump(self: *Self) ![]Point { const allocator = self.allocator; var node = self.head; @@ -66,23 +81,23 @@ pub fn LinkedList() type { point[i] = node.?.point; node = node.?.next; } - //TODO: free memory but how to?, when out of scope or dont use slices? or copy them, or outsource allocation? return point; } }; } + test "test linked list" { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); const allocator = arena.allocator(); defer arena.deinit(); var polygon = LinkedList().new(allocator); - try polygon.add(allocator, .{ .x = 10, .y = 11, .z = 0 }); - try polygon.add(allocator, .{ .x = 11, .y = 12, .z = 0 }); - try polygon.add(allocator, .{ .x = 12, .y = 13, .z = 0 }); - try polygon.add(allocator, .{ .x = 12, .y = 14, .z = 0 }); - try polygon.add(allocator, .{ .x = 12, .y = 15, .z = 0 }); - try polygon.add(allocator, .{ .x = 12, .y = 16, .z = 0 }); + try polygon.add(.{ .x = 10, .y = 11, .z = 0 }); + try polygon.add(.{ .x = 11, .y = 12, .z = 0 }); + try polygon.add(.{ .x = 12, .y = 13, .z = 0 }); + try polygon.add(.{ .x = 12, .y = 14, .z = 0 }); + try polygon.add(.{ .x = 12, .y = 15, .z = 0 }); + try polygon.add(.{ .x = 12, .y = 16, .z = 0 }); const points: []Point = try polygon.dump(); for (points) |point| { @@ -90,6 +105,21 @@ test "test linked list" { std.debug.print("y:{}\n", .{point.y}); std.debug.print("z:{}\n", .{point.z}); } + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); + std.debug.print("next:{}\n", .{polygon.next()}); } pub fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {