| ... | @@ -183,21 +183,9 @@ pub fn TailQueue(comptime T: type) type { | ... | @@ -183,21 +183,9 @@ pub fn TailQueue(comptime T: type) type { |
| 183 | } | 183 | } |
| 184 | }; | 184 | }; |
| 185 | | 185 | |
| 186 | first: ?*Node, | 186 | first: ?*Node = null, |
| 187 | last: ?*Node, | 187 | last: ?*Node = null, |
| 188 | len: usize, | 188 | len: usize = 0, |
| 189 | | | |
| 190 | /// Initialize a linked list. | | |
| 191 | /// | | |
| 192 | /// Returns: | | |
| 193 | /// An empty linked list. | | |
| 194 | pub fn init() Self { | | |
| 195 | return Self{ | | |
| 196 | .first = null, | | |
| 197 | .last = null, | | |
| 198 | .len = 0, | | |
| 199 | }; | | |
| 200 | } | | |
| 201 | | 189 | |
| 202 | /// Insert a new node after an existing one. | 190 | /// Insert a new node after an existing one. |
| 203 | /// | 191 | /// |
| ... | @@ -340,65 +328,24 @@ pub fn TailQueue(comptime T: type) type { | ... | @@ -340,65 +328,24 @@ pub fn TailQueue(comptime T: type) type { |
| 340 | list.remove(first); | 328 | list.remove(first); |
| 341 | return first; | 329 | return first; |
| 342 | } | 330 | } |
| 343 | | | |
| 344 | /// Allocate a new node. | | |
| 345 | /// | | |
| 346 | /// Arguments: | | |
| 347 | /// allocator: Dynamic memory allocator. | | |
| 348 | /// | | |
| 349 | /// Returns: | | |
| 350 | /// A pointer to the new node. | | |
| 351 | pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node { | | |
| 352 | return allocator.create(Node); | | |
| 353 | } | | |
| 354 | | | |
| 355 | /// Deallocate a node. | | |
| 356 | /// | | |
| 357 | /// Arguments: | | |
| 358 | /// node: Pointer to the node to deallocate. | | |
| 359 | /// allocator: Dynamic memory allocator. | | |
| 360 | pub fn destroyNode(list: *Self, node: *Node, allocator: *Allocator) void { | | |
| 361 | allocator.destroy(node); | | |
| 362 | } | | |
| 363 | | | |
| 364 | /// Allocate and initialize a node and its data. | | |
| 365 | /// | | |
| 366 | /// Arguments: | | |
| 367 | /// data: The data to put inside the node. | | |
| 368 | /// allocator: Dynamic memory allocator. | | |
| 369 | /// | | |
| 370 | /// Returns: | | |
| 371 | /// A pointer to the new node. | | |
| 372 | pub fn createNode(list: *Self, data: T, allocator: *Allocator) !*Node { | | |
| 373 | var node = try list.allocateNode(allocator); | | |
| 374 | node.* = Node.init(data); | | |
| 375 | return node; | | |
| 376 | } | | |
| 377 | }; | 331 | }; |
| 378 | } | 332 | } |
| 379 | | 333 | |
| 380 | test "basic TailQueue test" { | 334 | test "basic TailQueue test" { |
| 381 | const allocator = testing.allocator; | 335 | const L = TailQueue(u32); |
| 382 | var list = TailQueue(u32).init(); | 336 | var list = L{}; |
| 383 | | 337 | |
| 384 | var one = try list.createNode(1, allocator); | 338 | var one = L.Node{ .data = 1 }; |
| 385 | var two = try list.createNode(2, allocator); | 339 | var two = L.Node{ .data = 2 }; |
| 386 | var three = try list.createNode(3, allocator); | 340 | var three = L.Node{ .data = 3 }; |
| 387 | var four = try list.createNode(4, allocator); | 341 | var four = L.Node{ .data = 4 }; |
| 388 | var five = try list.createNode(5, allocator); | 342 | var five = L.Node{ .data = 5 }; |
| 389 | defer { | | |
| 390 | list.destroyNode(one, allocator); | | |
| 391 | list.destroyNode(two, allocator); | | |
| 392 | list.destroyNode(three, allocator); | | |
| 393 | list.destroyNode(four, allocator); | | |
| 394 | list.destroyNode(five, allocator); | | |
| 395 | } | | |
| 396 | | 343 | |
| 397 | list.append(two); // {2} | 344 | list.append(&two); // {2} |
| 398 | list.append(five); // {2, 5} | 345 | list.append(&five); // {2, 5} |
| 399 | list.prepend(one); // {1, 2, 5} | 346 | list.prepend(&one); // {1, 2, 5} |
| 400 | list.insertBefore(five, four); // {1, 2, 4, 5} | 347 | list.insertBefore(&five, &four); // {1, 2, 4, 5} |
| 401 | list.insertAfter(two, three); // {1, 2, 3, 4, 5} | 348 | list.insertAfter(&two, &three); // {1, 2, 3, 4, 5} |
| 402 | | 349 | |
| 403 | // Traverse forwards. | 350 | // Traverse forwards. |
| 404 | { | 351 | { |
| ... | @@ -422,7 +369,7 @@ test "basic TailQueue test" { | ... | @@ -422,7 +369,7 @@ test "basic TailQueue test" { |
| 422 | | 369 | |
| 423 | var first = list.popFirst(); // {2, 3, 4, 5} | 370 | var first = list.popFirst(); // {2, 3, 4, 5} |
| 424 | var last = list.pop(); // {2, 3, 4} | 371 | var last = list.pop(); // {2, 3, 4} |
| 425 | list.remove(three); // {2, 4} | 372 | list.remove(&three); // {2, 4} |
| 426 | | 373 | |
| 427 | testing.expect(list.first.?.data == 2); | 374 | testing.expect(list.first.?.data == 2); |
| 428 | testing.expect(list.last.?.data == 4); | 375 | testing.expect(list.last.?.data == 4); |
| ... | @@ -430,30 +377,25 @@ test "basic TailQueue test" { | ... | @@ -430,30 +377,25 @@ test "basic TailQueue test" { |
| 430 | } | 377 | } |
| 431 | | 378 | |
| 432 | test "TailQueue concatenation" { | 379 | test "TailQueue concatenation" { |
| 433 | const allocator = testing.allocator; | 380 | const L = TailQueue(u32); |
| 434 | var list1 = TailQueue(u32).init(); | 381 | var list1 = L{}; |
| 435 | var list2 = TailQueue(u32).init(); | 382 | var list2 = L{}; |
| 436 | | 383 | |
| 437 | var one = try list1.createNode(1, allocator); | 384 | var one = L.Node{ .data = 1 }; |
| 438 | defer list1.destroyNode(one, allocator); | 385 | var two = L.Node{ .data = 2 }; |
| 439 | var two = try list1.createNode(2, allocator); | 386 | var three = L.Node{ .data = 3 }; |
| 440 | defer list1.destroyNode(two, allocator); | 387 | var four = L.Node{ .data = 4 }; |
| 441 | var three = try list1.createNode(3, allocator); | 388 | var five = L.Node{ .data = 5 }; |
| 442 | defer list1.destroyNode(three, allocator); | 389 | |
| 443 | var four = try list1.createNode(4, allocator); | 390 | list1.append(&one); |
| 444 | defer list1.destroyNode(four, allocator); | 391 | list1.append(&two); |
| 445 | var five = try list1.createNode(5, allocator); | 392 | list2.append(&three); |
| 446 | defer list1.destroyNode(five, allocator); | 393 | list2.append(&four); |
| 447 | | 394 | list2.append(&five); |
| 448 | list1.append(one); | | |
| 449 | list1.append(two); | | |
| 450 | list2.append(three); | | |
| 451 | list2.append(four); | | |
| 452 | list2.append(five); | | |
| 453 | | 395 | |
| 454 | list1.concatByMoving(&list2); | 396 | list1.concatByMoving(&list2); |
| 455 | | 397 | |
| 456 | testing.expect(list1.last == five); | 398 | testing.expect(list1.last == &five); |
| 457 | testing.expect(list1.len == 5); | 399 | testing.expect(list1.len == 5); |
| 458 | testing.expect(list2.first == null); | 400 | testing.expect(list2.first == null); |
| 459 | testing.expect(list2.last == null); | 401 | testing.expect(list2.last == null); |