authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-08-21 17:54:01+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-21 15:59:50-04:00
log37ad9f38dc89b568abddb56a044bdfb36eca7d49
tree2f07478f068a2e433fd6430aab40259f10865245
parenta2f1f01e7793957177e9b3c39479549e2adc7f94

std: sync TailQueue with new SinglyLinkedList API

The API of SinglyLinkedList was changed in 93384f7, removing the init function as well as the redundant allocation helper functions. This commit makes parallel changes to the API of TailQueue in order to keep the standard library consistent.

1 files changed, 33 insertions(+), 91 deletions(-)

lib/std/linked_list.zig+33-91
...@@ -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 };
185185
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 }
201189
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}
379333
380test "basic TailQueue test" {334test "basic TailQueue test" {
381 const allocator = testing.allocator;335 const L = TailQueue(u32);
382 var list = TailQueue(u32).init();336 var list = L{};
383337
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 }
396343
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}
402349
403 // Traverse forwards.350 // Traverse forwards.
404 {351 {
...@@ -422,7 +369,7 @@ test "basic TailQueue test" {...@@ -422,7 +369,7 @@ test "basic TailQueue test" {
422369
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}
426373
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}
431378
432test "TailQueue concatenation" {379test "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{};
436383
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);
447394 list2.append(&five);
448 list1.append(one);
449 list1.append(two);
450 list2.append(three);
451 list2.append(four);
452 list2.append(five);
453395
454 list1.concatByMoving(&list2);396 list1.concatByMoving(&list2);
455397
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);