font: descritor can hash using a hasher

This commit is contained in:
Mitchell Hashimoto
2024-04-01 11:54:41 -07:00
parent 5546469c37
commit 7fef1aa294
2 changed files with 21 additions and 17 deletions

View File

@ -53,7 +53,7 @@ const DescriptorCache = std.HashMapUnmanaged(
pub fn hash(ctx: @This(), k: KeyType) u64 {
_ = ctx;
return k.hash();
return k.hashcode();
}
pub fn eql(ctx: @This(), a: KeyType, b: KeyType) bool {

View File

@ -57,27 +57,31 @@ pub const Descriptor = struct {
/// will be preferred, but not guaranteed.
variations: []const Variation = &.{},
/// Returns a hash code that can be used to uniquely identify this
/// action.
pub fn hash(self: Descriptor) u64 {
/// Hash the descriptor with the given hasher.
pub fn hash(self: Descriptor, hasher: anytype) void {
const autoHash = std.hash.autoHash;
var hasher = std.hash.Wyhash.init(0);
autoHash(&hasher, self.family);
autoHash(&hasher, self.style);
autoHash(&hasher, self.codepoint);
autoHash(&hasher, self.size);
autoHash(&hasher, self.bold);
autoHash(&hasher, self.italic);
autoHash(&hasher, self.monospace);
autoHash(&hasher, self.variations.len);
autoHash(hasher, self.family);
autoHash(hasher, self.style);
autoHash(hasher, self.codepoint);
autoHash(hasher, self.size);
autoHash(hasher, self.bold);
autoHash(hasher, self.italic);
autoHash(hasher, self.monospace);
autoHash(hasher, self.variations.len);
for (self.variations) |variation| {
autoHash(&hasher, variation.id);
autoHash(hasher, variation.id);
// This is not correct, but we don't currently depend on the
// hash value being different based on decimal values of variations.
autoHash(&hasher, @as(u64, @intFromFloat(variation.value)));
autoHash(hasher, @as(u64, @intFromFloat(variation.value)));
}
}
/// Returns a hash code that can be used to uniquely identify this
/// action.
pub fn hashcode(self: Descriptor) u64 {
var hasher = std.hash.Wyhash.init(0);
self.hash(&hasher);
return hasher.final();
}
@ -552,7 +556,7 @@ test "descriptor hash" {
const testing = std.testing;
var d: Descriptor = .{};
try testing.expect(d.hash() != 0);
try testing.expect(d.hashcode() != 0);
}
test "descriptor hash familiy names" {
@ -560,7 +564,7 @@ test "descriptor hash familiy names" {
var d1: Descriptor = .{ .family = "A" };
var d2: Descriptor = .{ .family = "B" };
try testing.expect(d1.hash() != d2.hash());
try testing.expect(d1.hashcode() != d2.hashcode());
}
test "fontconfig" {