aboutsummaryrefslogtreecommitdiff
path: root/src/identity_context.zig
blob: 5e0cfe987e4d804566f2b9de78ec9668170c7935 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub fn IdentityContext(comptime Key: type) type {
    return struct {
        pub fn hash(_: @This(), key: Key) u64 {
            return key;
        }

        pub fn eql(_: @This(), a: Key, b: Key) bool {
            return a == b;
        }
    };
}

/// When storing hashes as keys in a hash table, we don't want to hash the hashes or else we increase the chance of collisions. This is also marginally faster since it means hashing less stuff.
/// `ArrayIdentityContext` and `IdentityContext` are distinct because ArrayHashMap expects u32 hashes but HashMap expects u64 hashes.
pub const ArrayIdentityContext = struct {
    pub fn hash(_: @This(), key: u32) u32 {
        return key;
    }

    pub fn eql(_: @This(), a: u32, b: u32, _: usize) bool {
        return a == b;
    }

    pub const U64 = struct {
        pub fn hash(_: @This(), key: u64) u32 {
            return @truncate(key);
        }

        pub fn eql(_: @This(), a: u64, b: u64, _: usize) bool {
            return a == b;
        }
    };
};