| ... | @@ -30,13 +30,15 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy) | ... | @@ -30,13 +30,15 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy) |
| 30 | .DeepRecursive => hash(hasher, key.*, .DeepRecursive), | 30 | .DeepRecursive => hash(hasher, key.*, .DeepRecursive), |
| 31 | }, | 31 | }, |
| 32 | | 32 | |
| 33 | .Slice => switch (strat) { | 33 | .Slice => { |
| 34 | .Shallow => { | 34 | switch (strat) { |
| 35 | hashPointer(hasher, key.ptr, .Shallow); | 35 | .Shallow => { |
| 36 | hash(hasher, key.len, .Shallow); | 36 | hashPointer(hasher, key.ptr, .Shallow); |
| 37 | }, | 37 | }, |
| 38 | .Deep => hashArray(hasher, key, .Shallow), | 38 | .Deep => hashArray(hasher, key, .Shallow), |
| 39 | .DeepRecursive => hashArray(hasher, key, .DeepRecursive), | 39 | .DeepRecursive => hashArray(hasher, key, .DeepRecursive), |
| | 40 | } |
| | 41 | hash(hasher, key.len, .Shallow); |
| 40 | }, | 42 | }, |
| 41 | | 43 | |
| 42 | .Many, | 44 | .Many, |
| ... | @@ -53,17 +55,8 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy) | ... | @@ -53,17 +55,8 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy) |
| 53 | | 55 | |
| 54 | /// Helper function to hash a set of contiguous objects, from an array or slice. | 56 | /// Helper function to hash a set of contiguous objects, from an array or slice. |
| 55 | pub fn hashArray(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { | 57 | pub fn hashArray(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { |
| 56 | switch (strat) { | 58 | for (key) |element| { |
| 57 | .Shallow => { | 59 | hash(hasher, element, strat); |
| 58 | for (key) |element| { | | |
| 59 | hash(hasher, element, .Shallow); | | |
| 60 | } | | |
| 61 | }, | | |
| 62 | else => { | | |
| 63 | for (key) |element| { | | |
| 64 | hash(hasher, element, strat); | | |
| 65 | } | | |
| 66 | }, | | |
| 67 | } | 60 | } |
| 68 | } | 61 | } |
| 69 | | 62 | |
| ... | @@ -359,6 +352,12 @@ test "testHash array" { | ... | @@ -359,6 +352,12 @@ test "testHash array" { |
| 359 | try testing.expectEqual(h, hasher.final()); | 352 | try testing.expectEqual(h, hasher.final()); |
| 360 | } | 353 | } |
| 361 | | 354 | |
| | 355 | test "testHash multi-dimensional array" { |
| | 356 | const a = [_][]const u32{ &.{ 1, 2, 3 }, &.{ 4, 5 } }; |
| | 357 | const b = [_][]const u32{ &.{ 1, 2 }, &.{ 3, 4, 5 } }; |
| | 358 | try testing.expect(testHash(a) != testHash(b)); |
| | 359 | } |
| | 360 | |
| 362 | test "testHash struct" { | 361 | test "testHash struct" { |
| 363 | const Foo = struct { | 362 | const Foo = struct { |
| 364 | a: u32 = 1, | 363 | a: u32 = 1, |