@@ -174,7 +174,7 @@ test "basics" {
174
174
pub fn Simple (T : type ) type {
175
175
return struct {
176
176
const SimpleLinkedList = @This ();
177
- first : ? * Node = null ,
177
+ wrapped : SinglyLinkedList = .{} ,
178
178
179
179
pub const Payload = struct {
180
180
data : T ,
@@ -190,27 +190,17 @@ pub fn Simple(T: type) type {
190
190
};
191
191
192
192
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 );
195
194
}
196
195
197
196
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 );
207
198
}
208
199
209
200
/// Remove and return the first node in the list.
210
201
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 );
214
204
}
215
205
216
206
/// Given a Simple list, returns the payload at position <index>.
@@ -219,7 +209,7 @@ pub fn Simple(T: type) type {
219
209
/// This is a linear search through the list, consider avoiding this
220
210
/// operation, except for index == 0
221
211
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 ;
223
213
var ctr : usize = index ;
224
214
while (ctr > 0 ) : (ctr -= 1 ) {
225
215
thisnode = thisnode .next orelse return null ;
@@ -232,11 +222,7 @@ pub fn Simple(T: type) type {
232
222
/// This operation is O(N). Consider tracking the length separately rather than
233
223
/// computing it.
234
224
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 ();
240
226
}
241
227
};
242
228
}
@@ -277,11 +263,10 @@ test "Simple singly linked list" {
277
263
278
264
// Traverse forwards.
279
265
{
280
- var it = list .first ;
266
+ var it = list .at ( 0 ) ;
281
267
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 );
285
270
index += 1 ;
286
271
}
287
272
}
@@ -290,13 +275,19 @@ test "Simple singly linked list" {
290
275
_ = list .remove (& five ); // {2, 3, 4}
291
276
_ = two .node .removeNext (); // {2, 4}
292
277
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 );
302
293
}
0 commit comments