authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-09-01 13:38:11-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-09-01 13:38:11-07:00
log285c8f82b4d32760005a1167e0f78c16f8230d06
treed7b84d8c225a0799d178b6dd690f1573a17ff462
parent6fb9f29c318b731eb213f56d7b14898be8b668cf
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add indexOfAggregate


3 files changed, 39 insertions(+), 6 deletions(-)

src/containsAggregate.zig+2-6
...@@ -1,14 +1,10 @@...@@ -1,14 +1,10 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const indexOfAggregate = extras.indexOfAggregate;
45
5pub fn containsAggregate(comptime T: type, haystack: []const T, needle: T) bool {6pub fn containsAggregate(comptime T: type, haystack: []const T, needle: T) bool {
6 for (haystack) |item| {7 return indexOfAggregate(T, haystack, needle) != null;
7 if (T.eql(item, needle)) {
8 return true;
9 }
10 }
11 return false;
12}8}
139
14test {10test {
src/indexOfAggregate.zig created+35
...@@ -0,0 +1,35 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn indexOfAggregate(comptime T: type, haystack: []const T, needle: T) ?usize {
6 for (haystack, 0..) |item, i| {
7 if (T.eql(item, needle)) {
8 return i;
9 }
10 }
11 return null;
12}
13
14test {
15 const S = struct {
16 a: u8,
17
18 fn eql(this: @This(), other: @This()) bool {
19 return this.a == other.a;
20 }
21 };
22 const data = [_]S{
23 .{ .a = 8 },
24 .{ .a = 6 },
25 .{ .a = 7 },
26 .{ .a = 2 },
27 .{ .a = 4 },
28 .{ .a = 1 },
29 .{ .a = 9 },
30 .{ .a = 3 },
31 .{ .a = 5 },
32 };
33 try std.testing.expect(indexOfAggregate(S, &data, .{ .a = 4 }) == 4);
34 try std.testing.expect(indexOfAggregate(S, &data, .{ .a = 0 }) == null);
35}
src/lib.zig+2
...@@ -88,6 +88,7 @@ pub const Pointee = @import("./Pointee.zig").Pointee;...@@ -88,6 +88,7 @@ pub const Pointee = @import("./Pointee.zig").Pointee;
88pub const compareFnSlice = @import("./compareFnSlice.zig").compareFnSlice;88pub const compareFnSlice = @import("./compareFnSlice.zig").compareFnSlice;
89pub const sumLen = @import("./sumLen.zig").sumLen;89pub const sumLen = @import("./sumLen.zig").sumLen;
90pub const splitScalarN = @import("./splitScalarN.zig").splitScalarN;90pub const splitScalarN = @import("./splitScalarN.zig").splitScalarN;
91pub const indexOfAggregate = @import("./indexOfAggregate.zig").indexOfAggregate;
9192
92test {93test {
93 _ = @import("reduceNumber.zig");94 _ = @import("reduceNumber.zig");
...@@ -165,4 +166,5 @@ test {...@@ -165,4 +166,5 @@ test {
165 _ = @import("compareFnSlice.zig");166 _ = @import("compareFnSlice.zig");
166 _ = @import("sumLen.zig");167 _ = @import("sumLen.zig");
167 _ = @import("splitScalarN.zig");168 _ = @import("splitScalarN.zig");
169 _ = @import("indexOfAggregate.zig");
168}170}