authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2022-10-11 21:36:49+03:30
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-13 13:57:20+02:00
log0eb3b8fa44f6dcafad8671695688d13ba8daba9a
treebebdf7e67d68094a0c16f88122de168ff10d7e6d
parentea23217751c474140c7b46cbffa796a2f93f3ffb

std.SegmentedList: fix compilation error


1 files changed, 23 insertions(+), 12 deletions(-)

lib/std/segmented_list.zig+23-12
......@@ -157,13 +157,13 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
157157
158158 /// Invalidates all element pointers.
159159 pub fn clearRetainingCapacity(self: *Self) void {
160 self.items.len = 0;
160 self.len = 0;
161161 }
162162
163163 /// Invalidates all element pointers.
164164 pub fn clearAndFree(self: *Self, allocator: Allocator) void {
165165 self.setCapacity(allocator, 0) catch unreachable;
166 self.items.len = 0;
166 self.len = 0;
167167 }
168168
169169 /// Grows or shrinks capacity to match usage.
......@@ -403,15 +403,13 @@ test "SegmentedList basic usage" {
403403}
404404
405405fn testSegmentedList(comptime prealloc: usize) !void {
406 const gpa = std.testing.allocator;
407
408 var list: SegmentedList(i32, prealloc) = .{};
409 defer list.deinit(gpa);
406 var list = SegmentedList(i32, prealloc){};
407 defer list.deinit(testing.allocator);
410408
411409 {
412410 var i: usize = 0;
413411 while (i < 100) : (i += 1) {
414 try list.append(gpa, @intCast(i32, i + 1));
412 try list.append(testing.allocator, @intCast(i32, i + 1));
415413 try testing.expect(list.len == i + 1);
416414 }
417415 }
......@@ -454,21 +452,21 @@ fn testSegmentedList(comptime prealloc: usize) !void {
454452 try testing.expect(list.pop().? == 100);
455453 try testing.expect(list.len == 99);
456454
457 try list.appendSlice(gpa, &[_]i32{ 1, 2, 3 });
455 try list.appendSlice(testing.allocator, &[_]i32{ 1, 2, 3 });
458456 try testing.expect(list.len == 102);
459457 try testing.expect(list.pop().? == 3);
460458 try testing.expect(list.pop().? == 2);
461459 try testing.expect(list.pop().? == 1);
462460 try testing.expect(list.len == 99);
463461
464 try list.appendSlice(gpa, &[_]i32{});
462 try list.appendSlice(testing.allocator, &[_]i32{});
465463 try testing.expect(list.len == 99);
466464
467465 {
468466 var i: i32 = 99;
469467 while (list.pop()) |item| : (i -= 1) {
470468 try testing.expect(item == i);
471 list.shrinkCapacity(gpa, list.len);
469 list.shrinkCapacity(testing.allocator, list.len);
472470 }
473471 }
474472
......@@ -478,7 +476,7 @@ fn testSegmentedList(comptime prealloc: usize) !void {
478476
479477 var i: i32 = 0;
480478 while (i < 100) : (i += 1) {
481 try list.append(gpa, i + 1);
479 try list.append(testing.allocator, i + 1);
482480 control[@intCast(usize, i)] = i + 1;
483481 }
484482
......@@ -491,7 +489,20 @@ fn testSegmentedList(comptime prealloc: usize) !void {
491489 try testing.expect(std.mem.eql(i32, control[50..], dest[50..]));
492490 }
493491
494 try list.setCapacity(gpa, 0);
492 try list.setCapacity(testing.allocator, 0);
493}
494
495test "std.segmented_list clearRetainingCapacity" {
496 var list = SegmentedList(i32, 1){};
497 defer list.deinit(testing.allocator);
498
499 try list.appendSlice(testing.allocator, &[_]i32{ 4, 5 });
500 list.clearRetainingCapacity();
501 try list.append(testing.allocator, 6);
502 try testing.expect(list.at(0).* == 6);
503 try testing.expect(list.len == 1);
504 list.clearRetainingCapacity();
505 try testing.expect(list.len == 0);
495506}
496507
497508/// TODO look into why this std.math function was changed in