fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize self: Selfkeyctx966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000 fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize {
if (self.size == 0) {
// We use cold instead of unlikely to force a jump to this case,
// no matter the weight of the opposing side.
@branchHint(.cold);
return null;
}
// If you get a compile error on this line, it means that your generic hash
// function is invalid for these parameters.
const hash: Hash = ctx.hash(key);
const mask = self.capacity() - 1;
const fingerprint = Metadata.takeFingerprint(hash);
// Don't loop indefinitely when there are no empty slots.
var limit = self.capacity();
var idx = @as(usize, @truncate(hash & mask));
var metadata = self.metadata.? + idx;
while (!metadata[0].isFree() and limit != 0) {
if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
const test_key = &self.keys()[idx];
if (ctx.eql(key, test_key.*)) {
return idx;
}
}
limit -= 1;
idx = (idx + 1) & mask;
metadata = self.metadata.? + idx;
}
return null;
}
Find the index containing the data for the given key.