| ... | ... | @@ -1236,7 +1236,7 @@ pub fn HashMapUnmanaged( |
| 1236 | 1236 | metadata[0].fill(fingerprint); |
| 1237 | 1237 | const new_key = &self.keys()[idx]; |
| 1238 | 1238 | const new_value = &self.values()[idx]; |
| 1239 | | new_key.* = key; |
| 1239 | new_key.* = undefined; |
| 1240 | 1240 | new_value.* = undefined; |
| 1241 | 1241 | self.size += 1; |
| 1242 | 1242 | |
| ... | ... | @@ -1884,6 +1884,52 @@ test "std.hash_map clone" { |
| 1884 | 1884 | } |
| 1885 | 1885 | } |
| 1886 | 1886 | |
| 1887 | test "std.hash_map getOrPutAdapted" { |
| 1888 | const AdaptedContext = struct { |
| 1889 | fn eql(self: @This(), adapted_key: []const u8, test_key: u64) bool { |
| 1890 | return std.fmt.parseInt(u64, adapted_key, 10) catch unreachable == test_key; |
| 1891 | } |
| 1892 | fn hash(self: @This(), adapted_key: []const u8) u64 { |
| 1893 | const key = std.fmt.parseInt(u64, adapted_key, 10) catch unreachable; |
| 1894 | return (AutoContext(u64){}).hash(key); |
| 1895 | } |
| 1896 | }; |
| 1897 | var map = AutoHashMap(u64, u64).init(testing.allocator); |
| 1898 | defer map.deinit(); |
| 1899 | |
| 1900 | const keys = [_][]const u8{ |
| 1901 | "1231", |
| 1902 | "4564", |
| 1903 | "7894", |
| 1904 | "1132", |
| 1905 | "65235", |
| 1906 | "95462", |
| 1907 | "0112305", |
| 1908 | "00658", |
| 1909 | "0", |
| 1910 | "2", |
| 1911 | }; |
| 1912 | |
| 1913 | var real_keys: [keys.len]u64 = undefined; |
| 1914 | |
| 1915 | inline for (keys) |key_str, i| { |
| 1916 | const result = try map.getOrPutAdapted(key_str, AdaptedContext{}); |
| 1917 | try testing.expect(!result.found_existing); |
| 1918 | real_keys[i] = std.fmt.parseInt(u64, key_str, 10) catch unreachable; |
| 1919 | result.key_ptr.* = real_keys[i]; |
| 1920 | result.value_ptr.* = i * 2; |
| 1921 | } |
| 1922 | |
| 1923 | try testing.expectEqual(map.count(), keys.len); |
| 1924 | |
| 1925 | inline for (keys) |key_str, i| { |
| 1926 | const result = try map.getOrPutAdapted(key_str, AdaptedContext{}); |
| 1927 | try testing.expect(result.found_existing); |
| 1928 | try testing.expectEqual(real_keys[i], result.key_ptr.*); |
| 1929 | try testing.expectEqual(@as(u64, i) * 2, result.value_ptr.*); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1887 | 1933 | test "compile everything" { |
| 1888 | 1934 | std.testing.refAllDecls(AutoHashMap(i32, i32)); |
| 1889 | 1935 | std.testing.refAllDecls(StringHashMap([]const u8)); |