mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2026-02-18 21:17:04 +01:00
make Iterator(Inner).toOwnedSlice and Iterator(Inner).collectInto take pointer to self
This commit is contained in:
parent
7ec0bf20dc
commit
b9098aea59
1 changed files with 9 additions and 10 deletions
19
src/root.zig
19
src/root.zig
|
|
@ -51,14 +51,14 @@ test iter {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const items = try iter(i32)
|
var it = iter(i32)
|
||||||
.once(-100)
|
.once(-100)
|
||||||
.chain(
|
.chain(
|
||||||
iter(i32).rangeInclusive(1, 100)
|
iter(i32).rangeInclusive(1, 100)
|
||||||
.filter(arith.is_square)
|
.filter(arith.is_square)
|
||||||
.chain(iter(i32).range(0, 10)),
|
.chain(iter(i32).range(0, 10)),
|
||||||
)
|
);
|
||||||
.toOwnedSlice(allocator);
|
const items = try it.toOwnedSlice(allocator);
|
||||||
defer allocator.free(items);
|
defer allocator.free(items);
|
||||||
|
|
||||||
try std.testing.expectEqualSlices(
|
try std.testing.expectEqualSlices(
|
||||||
|
|
@ -196,7 +196,7 @@ pub fn Iterator(comptime Inner: type) type {
|
||||||
try expectEqualDeep(null, it.next());
|
try expectEqualDeep(null, it.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toOwnedSlice(self: Self, allocator: std.mem.Allocator) ![]const Item {
|
pub fn toOwnedSlice(self: *Self, allocator: std.mem.Allocator) ![]const Item {
|
||||||
var list: std.ArrayList(Item) = try .initCapacity(allocator, self.sizeHint());
|
var list: std.ArrayList(Item) = try .initCapacity(allocator, self.sizeHint());
|
||||||
errdefer list.deinit(allocator);
|
errdefer list.deinit(allocator);
|
||||||
|
|
||||||
|
|
@ -205,9 +205,8 @@ pub fn Iterator(comptime Inner: type) type {
|
||||||
return try list.toOwnedSlice(allocator);
|
return try list.toOwnedSlice(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collectInto(self: Self, list: *std.ArrayList(Item), allocator: std.mem.Allocator) !void {
|
pub fn collectInto(self: *Self, list: *std.ArrayList(Item), allocator: std.mem.Allocator) !void {
|
||||||
var mutSelf = self;
|
while (self.next()) |val| {
|
||||||
while (mutSelf.next()) |val| {
|
|
||||||
try list.append(allocator, val);
|
try list.append(allocator, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue