| ... | ... | @@ -183,21 +183,9 @@ pub fn TailQueue(comptime T: type) type { |
| 183 | 183 | } |
| 184 | 184 | }; |
| 185 | 185 | |
| 186 | | first: ?*Node, |
| 187 | | last: ?*Node, |
| 188 | | len: usize, |
| 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 | | } |
| 186 | first: ?*Node = null, |
| 187 | last: ?*Node = null, |
| 188 | len: usize = 0, |
| 201 | 189 | |
| 202 | 190 | /// Insert a new node after an existing one. |
| 203 | 191 | /// |
| ... | ... | @@ -340,65 +328,24 @@ pub fn TailQueue(comptime T: type) type { |
| 340 | 328 | list.remove(first); |
| 341 | 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 | 334 | test "basic TailQueue test" { |
| 381 | | const allocator = testing.allocator; |
| 382 | | var list = TailQueue(u32).init(); |
| 383 | | |
| 384 | | var one = try list.createNode(1, allocator); |
| 385 | | var two = try list.createNode(2, allocator); |
| 386 | | var three = try list.createNode(3, allocator); |
| 387 | | var four = try list.createNode(4, allocator); |
| 388 | | var five = try list.createNode(5, allocator); |
| 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 | | } |
| 335 | const L = TailQueue(u32); |
| 336 | var list = L{}; |
| 337 | |
| 338 | var one = L.Node{ .data = 1 }; |
| 339 | var two = L.Node{ .data = 2 }; |
| 340 | var three = L.Node{ .data = 3 }; |
| 341 | var four = L.Node{ .data = 4 }; |
| 342 | var five = L.Node{ .data = 5 }; |
| 396 | 343 | |
| 397 | | list.append(two); // {2} |
| 398 | | list.append(five); // {2, 5} |
| 399 | | list.prepend(one); // {1, 2, 5} |
| 400 | | list.insertBefore(five, four); // {1, 2, 4, 5} |
| 401 | | list.insertAfter(two, three); // {1, 2, 3, 4, 5} |
| 344 | list.append(&two); // {2} |
| 345 | list.append(&five); // {2, 5} |
| 346 | list.prepend(&one); // {1, 2, 5} |
| 347 | list.insertBefore(&five, &four); // {1, 2, 4, 5} |
| 348 | list.insertAfter(&two, &three); // {1, 2, 3, 4, 5} |
| 402 | 349 | |
| 403 | 350 | // Traverse forwards. |
| 404 | 351 | { |
| ... | ... | @@ -422,7 +369,7 @@ test "basic TailQueue test" { |
| 422 | 369 | |
| 423 | 370 | var first = list.popFirst(); // {2, 3, 4, 5} |
| 424 | 371 | var last = list.pop(); // {2, 3, 4} |
| 425 | | list.remove(three); // {2, 4} |
| 372 | list.remove(&three); // {2, 4} |
| 426 | 373 | |
| 427 | 374 | testing.expect(list.first.?.data == 2); |
| 428 | 375 | testing.expect(list.last.?.data == 4); |
| ... | ... | @@ -430,30 +377,25 @@ test "basic TailQueue test" { |
| 430 | 377 | } |
| 431 | 378 | |
| 432 | 379 | test "TailQueue concatenation" { |
| 433 | | const allocator = testing.allocator; |
| 434 | | var list1 = TailQueue(u32).init(); |
| 435 | | var list2 = TailQueue(u32).init(); |
| 436 | | |
| 437 | | var one = try list1.createNode(1, allocator); |
| 438 | | defer list1.destroyNode(one, allocator); |
| 439 | | var two = try list1.createNode(2, allocator); |
| 440 | | defer list1.destroyNode(two, allocator); |
| 441 | | var three = try list1.createNode(3, allocator); |
| 442 | | defer list1.destroyNode(three, allocator); |
| 443 | | var four = try list1.createNode(4, allocator); |
| 444 | | defer list1.destroyNode(four, allocator); |
| 445 | | var five = try list1.createNode(5, allocator); |
| 446 | | defer list1.destroyNode(five, allocator); |
| 447 | | |
| 448 | | list1.append(one); |
| 449 | | list1.append(two); |
| 450 | | list2.append(three); |
| 451 | | list2.append(four); |
| 452 | | list2.append(five); |
| 380 | const L = TailQueue(u32); |
| 381 | var list1 = L{}; |
| 382 | var list2 = L{}; |
| 383 | |
| 384 | var one = L.Node{ .data = 1 }; |
| 385 | var two = L.Node{ .data = 2 }; |
| 386 | var three = L.Node{ .data = 3 }; |
| 387 | var four = L.Node{ .data = 4 }; |
| 388 | var five = L.Node{ .data = 5 }; |
| 389 | |
| 390 | list1.append(&one); |
| 391 | list1.append(&two); |
| 392 | list2.append(&three); |
| 393 | list2.append(&four); |
| 394 | list2.append(&five); |
| 453 | 395 | |
| 454 | 396 | list1.concatByMoving(&list2); |
| 455 | 397 | |
| 456 | | testing.expect(list1.last == five); |
| 398 | testing.expect(list1.last == &five); |
| 457 | 399 | testing.expect(list1.len == 5); |
| 458 | 400 | testing.expect(list2.first == null); |
| 459 | 401 | testing.expect(list2.last == null); |