| author | |
| committer | |
| log | 285c8f82b4d32760005a1167e0f78c16f8230d06 |
| tree | d7b84d8c225a0799d178b6dd690f1573a17ff462 |
| parent | 6fb9f29c318b731eb213f56d7b14898be8b668cf |
| signature | Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw |
3 files changed, 39 insertions(+), 6 deletions(-)
src/containsAggregate.zig+2-6| ... | @@ -1,14 +1,10 @@ | ... | @@ -1,14 +1,10 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const indexOfAggregate = extras.indexOfAggregate; | ||
| 4 | 5 | ||
| 5 | pub fn containsAggregate(comptime T: type, haystack: []const T, needle: T) bool { | 6 | pub 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 | } |
| 13 | 9 | ||
| 14 | test { | 10 | test { |
src/indexOfAggregate.zig created+35| ... | @@ -0,0 +1,35 @@ | ||
| 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 | } | ||
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; |
| 88 | pub const compareFnSlice = @import("./compareFnSlice.zig").compareFnSlice; | 88 | pub const compareFnSlice = @import("./compareFnSlice.zig").compareFnSlice; |
| 89 | pub const sumLen = @import("./sumLen.zig").sumLen; | 89 | pub const sumLen = @import("./sumLen.zig").sumLen; |
| 90 | pub const splitScalarN = @import("./splitScalarN.zig").splitScalarN; | 90 | pub const splitScalarN = @import("./splitScalarN.zig").splitScalarN; |
| 91 | pub const indexOfAggregate = @import("./indexOfAggregate.zig").indexOfAggregate; | ||
| 91 | 92 | ||
| 92 | test { | 93 | test { |
| 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 | } |