| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub 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 | |
| 14 | test { |
| 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 | } |