Skip to content

Commit 38a9f6b

Browse files
committed
fixes to wrap instead of reimplement
1 parent 3413b1a commit 38a9f6b

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

lib/std/SinglyLinkedList.zig

+25-34
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ test "basics" {
174174
pub fn Simple(T: type) type {
175175
return struct {
176176
const SimpleLinkedList = @This();
177-
first: ?*Node = null,
177+
wrapped: SinglyLinkedList = .{},
178178

179179
pub const Payload = struct {
180180
data: T,
@@ -190,27 +190,17 @@ pub fn Simple(T: type) type {
190190
};
191191

192192
pub fn prepend(list: *SimpleLinkedList, new_payload: *Payload) void {
193-
new_payload.node.next = list.first;
194-
list.first = &new_payload.node;
193+
list.wrapped.prepend(&new_payload.node);
195194
}
196195

197196
pub fn remove(list: *SimpleLinkedList, payload: *Payload) void {
198-
if (list.first == &payload.node) {
199-
list.first = payload.node.next;
200-
} else {
201-
var current_elm = list.first.?;
202-
while (current_elm.next != &payload.node) {
203-
current_elm = current_elm.next.?;
204-
}
205-
current_elm.next = payload.node.next;
206-
}
197+
list.wrapped.remove(&payload.node);
207198
}
208199

209200
/// Remove and return the first node in the list.
210201
pub fn popFirst(list: *SimpleLinkedList) ?*Payload {
211-
const first = list.first orelse return null;
212-
list.first = first.next;
213-
return @fieldParentPtr("node", first);
202+
const poppednode = (list.wrapped.popFirst()) orelse return null;
203+
return @fieldParentPtr("node", poppednode);
214204
}
215205

216206
/// Given a Simple list, returns the payload at position <index>.
@@ -219,7 +209,7 @@ pub fn Simple(T: type) type {
219209
/// This is a linear search through the list, consider avoiding this
220210
/// operation, except for index == 0
221211
pub fn at(list: *SimpleLinkedList, index: usize) ?*Payload {
222-
var thisnode = list.first orelse return null;
212+
var thisnode = list.wrapped.first orelse return null;
223213
var ctr: usize = index;
224214
while (ctr > 0) : (ctr -= 1) {
225215
thisnode = thisnode.next orelse return null;
@@ -232,11 +222,7 @@ pub fn Simple(T: type) type {
232222
/// This operation is O(N). Consider tracking the length separately rather than
233223
/// computing it.
234224
pub fn len(list: SimpleLinkedList) usize {
235-
if (list.first) |n| {
236-
return 1 + n.countChildren();
237-
} else {
238-
return 0;
239-
}
225+
return list.wrapped.len();
240226
}
241227
};
242228
}
@@ -277,11 +263,10 @@ test "Simple singly linked list" {
277263

278264
// Traverse forwards.
279265
{
280-
var it = list.first;
266+
var it = list.at(0);
281267
var index: u32 = 1;
282-
while (it) |node| : (it = node.next) {
283-
const l: *L = @fieldParentPtr("node", node);
284-
try testing.expect(l.data == index);
268+
while (it) |payload| : (it = payload.next()) {
269+
try testing.expect(payload.data == index);
285270
index += 1;
286271
}
287272
}
@@ -290,13 +275,19 @@ test "Simple singly linked list" {
290275
_ = list.remove(&five); // {2, 3, 4}
291276
_ = two.node.removeNext(); // {2, 4}
292277

293-
try testing.expect(@as(*L, @fieldParentPtr("node", list.first.?)).data == 2);
294-
try testing.expect(@as(*L, @fieldParentPtr("node", list.first.?.next.?)).data == 4);
295-
try testing.expect(list.first.?.next.?.next == null);
296-
297-
SinglyLinkedList.Node.reverse(&list.first);
298-
299-
try testing.expect(@as(*L, @fieldParentPtr("node", list.first.?)).data == 4);
300-
try testing.expect(@as(*L, @fieldParentPtr("node", list.first.?.next.?)).data == 2);
301-
try testing.expect(list.first.?.next.?.next == null);
278+
try testing.expect(@as(*L, @fieldParentPtr("node", list.wrapped.first.?)).data == 2);
279+
try testing.expect(list.at(0).?.data == 2);
280+
try testing.expect(@as(*L, @fieldParentPtr("node", list.wrapped.first.?.next.?)).data == 4);
281+
try testing.expect(list.at(1).?.data == 4);
282+
283+
try testing.expect(list.wrapped.first.?.next.?.next == null);
284+
285+
SinglyLinkedList.Node.reverse(&list.wrapped.first);
286+
287+
try testing.expect(@as(*L, @fieldParentPtr("node", list.wrapped.first.?)).data == 4);
288+
try testing.expect(list.at(0).?.data == 4);
289+
try testing.expect(@as(*L, @fieldParentPtr("node", list.wrapped.first.?.next.?)).data == 2);
290+
try testing.expect(list.at(1).?.data == 2);
291+
try testing.expect(list.wrapped.first.?.next.?.next == null);
292+
try testing.expect(list.at(2) == null);
302293
}

0 commit comments

Comments
 (0)