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 {
183183 }
184184 };
185185
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,
201189
202190 /// Insert a new node after an existing one.
203191 ///
......@@ -340,65 +328,24 @@ pub fn TailQueue(comptime T: type) type {
340328 list.remove(first);
341329 return first;
342330 }
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 }
377331 };
378332}
379333
380334test "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 };
396343
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}
402349
403350 // Traverse forwards.
404351 {
......@@ -422,7 +369,7 @@ test "basic TailQueue test" {
422369
423370 var first = list.popFirst(); // {2, 3, 4, 5}
424371 var last = list.pop(); // {2, 3, 4}
425 list.remove(three); // {2, 4}
372 list.remove(&three); // {2, 4}
426373
427374 testing.expect(list.first.?.data == 2);
428375 testing.expect(list.last.?.data == 4);
......@@ -430,30 +377,25 @@ test "basic TailQueue test" {
430377}
431378
432379test "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);
453395
454396 list1.concatByMoving(&list2);
455397
456 testing.expect(list1.last == five);
398 testing.expect(list1.last == &five);
457399 testing.expect(list1.len == 5);
458400 testing.expect(list2.first == null);
459401 testing.expect(list2.last == null);