Skip to content

Commit ff61c42

Browse files
jayschwaandrewrk
authored andcommitted
std: Rename TailQueue to DoublyLinkedList
`TailQueue` was implemented as a doubly-linked list, but named after an abstract data type. This was inconsistent with `SinglyLinkedList`, which can be used to implement an abstract data type, but is still named after the implementation. Renaming `TailQueue` to `DoublyLinkedList` improves consistency between the two type names, and should help discoverability. `TailQueue` is now a deprecated alias of `DoublyLinkedList`. Related to issues #1629 and #8233.
1 parent 750998e commit ff61c42

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

lib/std/atomic/queue.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn Queue(comptime T: type) type {
1414
mutex: std.Thread.Mutex,
1515

1616
pub const Self = @This();
17-
pub const Node = std.TailQueue(T).Node;
17+
pub const Node = std.DoublyLinkedList(T).Node;
1818

1919
/// Initializes a new queue. The queue does not provide a `deinit()`
2020
/// function, so the user must take care of cleaning up the queue elements.

lib/std/http/Client.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub const ConnectionPool = struct {
3636
is_tls: bool,
3737
};
3838

39-
const Queue = std.TailQueue(Connection);
39+
const Queue = std.DoublyLinkedList(Connection);
4040
pub const Node = Queue.Node;
4141

4242
mutex: std.Thread.Mutex = .{},

lib/std/linked_list.zig

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = debug.assert;
44
const testing = std.testing;
55

66
/// A singly-linked list is headed by a single forward pointer. The elements
7-
/// are singly linked for minimum space and pointer manipulation overhead at
7+
/// are singly-linked for minimum space and pointer manipulation overhead at
88
/// the expense of O(n) removal for arbitrary elements. New elements can be
99
/// added to the list after an existing element or at the head of the list.
1010
/// A singly-linked list may only be traversed in the forward direction.
@@ -171,13 +171,20 @@ test "basic SinglyLinkedList test" {
171171
try testing.expect(list.first.?.next.?.next == null);
172172
}
173173

174-
/// A tail queue is headed by a pair of pointers, one to the head of the
175-
/// list and the other to the tail of the list. The elements are doubly
176-
/// linked so that an arbitrary element can be removed without a need to
177-
/// traverse the list. New elements can be added to the list before or
178-
/// after an existing element, at the head of the list, or at the end of
179-
/// the list. A tail queue may be traversed in either direction.
180-
pub fn TailQueue(comptime T: type) type {
174+
/// deprecated: use `DoublyLinkedList`.
175+
pub const TailQueue = DoublyLinkedList;
176+
177+
/// A doubly-linked list has a pair of pointers to both the head and
178+
/// tail of the list. List elements have pointers to both the previous
179+
/// and next elements in the sequence. The list can be traversed both
180+
/// forward and backward. Some operations that take linear O(n) time
181+
/// with a singly-linked list can be done without traversal in constant
182+
/// O(1) time with a doubly-linked list:
183+
///
184+
/// - Removing an element.
185+
/// - Inserting a new element before an existing element.
186+
/// - Pushing or popping an element from the end of the list.
187+
pub fn DoublyLinkedList(comptime T: type) type {
181188
return struct {
182189
const Self = @This();
183190

@@ -336,8 +343,8 @@ pub fn TailQueue(comptime T: type) type {
336343
};
337344
}
338345

339-
test "basic TailQueue test" {
340-
const L = TailQueue(u32);
346+
test "basic DoublyLinkedList test" {
347+
const L = DoublyLinkedList(u32);
341348
var list = L{};
342349

343350
var one = L.Node{ .data = 1 };
@@ -381,8 +388,8 @@ test "basic TailQueue test" {
381388
try testing.expect(list.len == 2);
382389
}
383390

384-
test "TailQueue concatenation" {
385-
const L = TailQueue(u32);
391+
test "DoublyLinkedList concatenation" {
392+
const L = DoublyLinkedList(u32);
386393
var list1 = L{};
387394
var list2 = L{};
388395

lib/std/std.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub const StringHashMap = hash_map.StringHashMap;
4343
pub const StringHashMapUnmanaged = hash_map.StringHashMapUnmanaged;
4444
pub const StringArrayHashMap = array_hash_map.StringArrayHashMap;
4545
pub const StringArrayHashMapUnmanaged = array_hash_map.StringArrayHashMapUnmanaged;
46-
pub const TailQueue = @import("linked_list.zig").TailQueue;
46+
pub const DoublyLinkedList = @import("linked_list.zig").DoublyLinkedList;
4747
pub const Target = @import("target.zig").Target;
4848
pub const Thread = @import("Thread.zig");
4949
pub const Treap = @import("treap.zig").Treap;

src/Package.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ pub fn add(pkg: *Package, gpa: Allocator, name: []const u8, package: *Package) !
130130
/// package. It should only be used for error output.
131131
pub fn getName(target: *const Package, gpa: Allocator, mod: Module) ![]const u8 {
132132
// we'll do a breadth-first search from the root module to try and find a short name for this
133-
// module, using a TailQueue of module/parent pairs. note that the "parent" there is just the
134-
// first-found shortest path - a module may be children of arbitrarily many other modules.
135-
// also, this path may vary between executions due to hashmap iteration order, but that doesn't
136-
// matter too much.
133+
// module, using a DoublyLinkedList of module/parent pairs. note that the "parent" there is
134+
// just the first-found shortest path - a module may be children of arbitrarily many other
135+
// modules. This path may vary between executions due to hashmap iteration order, but that
136+
// doesn't matter too much.
137137
var node_arena = std.heap.ArenaAllocator.init(gpa);
138138
defer node_arena.deinit();
139139
const Parented = struct {
140140
parent: ?*const @This(),
141141
mod: *const Package,
142142
};
143-
const Queue = std.TailQueue(Parented);
143+
const Queue = std.DoublyLinkedList(Parented);
144144
var to_check: Queue = .{};
145145

146146
{

test/behavior/bugs/1735.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const builtin = @import("builtin");
44
const mystruct = struct {
55
pending: ?listofstructs,
66
};
7-
pub fn TailQueue(comptime T: type) type {
7+
pub fn DoublyLinkedList(comptime T: type) type {
88
return struct {
99
const Self = @This();
1010

@@ -27,7 +27,7 @@ pub fn TailQueue(comptime T: type) type {
2727
}
2828
};
2929
}
30-
const listofstructs = TailQueue(mystruct);
30+
const listofstructs = DoublyLinkedList(mystruct);
3131

3232
const a = struct {
3333
const Self = @This();

0 commit comments

Comments
 (0)