1 | // SPDX-License-Identifier: GPL-3.0-or-later |
---|---|
2 | |
3 | #include <mos/lib/structures/hashmap_common.h> |
4 | #include <mos/types.h> |
5 | #include <mos_string.h> |
6 | |
7 | static hash_t __pure string_hash(const char *s, const int n) |
8 | { |
9 | const int p = 31, m = 1e9 + 7; |
10 | hash_t h = { 0 }; |
11 | long p_pow = 1; |
12 | for (int i = 0; i < n; i++) |
13 | { |
14 | h.hash = (h.hash + (s[i] - 'a' + 1) * p_pow) % m; |
15 | p_pow = (p_pow * p) % m; |
16 | } |
17 | return h; |
18 | } |
19 | |
20 | hash_t __pure hashmap_hash_string(uintn key) |
21 | { |
22 | return string_hash(s: (const char *) key, n: strlen(str: (const char *) key)); |
23 | } |
24 | |
25 | int __pure hashmap_compare_string(uintn key1, uintn key2) |
26 | { |
27 | return strcmp(str1: (const char *) key1, str2: (const char *) key2) == 0; |
28 | } |
29 | |
30 | int __pure hashmap_simple_key_compare(uintn key1, uintn key2) |
31 | { |
32 | return key1 == key2; |
33 | } |
34 | |
35 | hash_t hashmap_identity_hash(uintn key) |
36 | { |
37 | return (hash_t){ .hash = key }; |
38 | } |
39 |