MOS Source Code
Loading...
Searching...
No Matches
hashmap_common.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4#include <mos/types.h>
5#include <mos_string.h>
6
7static 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
21{
22 return string_hash((const char *) key, strlen((const char *) key));
23}
24
26{
27 return strcmp((const char *) key1, (const char *) key2) == 0;
28}
29
31{
32 return key1 == key2;
33}
34
36{
37 return (hash_t){ .hash = key };
38}
int __pure hashmap_simple_key_compare(uintn key1, uintn key2)
hash_t __pure hashmap_hash_string(uintn key)
hash_t hashmap_identity_hash(uintn key)
int __pure hashmap_compare_string(uintn key1, uintn key2)
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
static hash_t __pure string_hash(const char *s, const int n)
#define __pure
Definition mos_global.h:31
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
Definition types.h:97
size_t hash
Definition types.h:97
unsigned long uintn
Definition types.h:30