Skip to content

Commit

Permalink
To correct some grammar issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinnan123 committed Jun 28, 2023
1 parent 6442141 commit ec953f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
20 changes: 12 additions & 8 deletions native/hashmap_get.c
Expand Up @@ -2,19 +2,23 @@
#define XXH_IMPLEMENTATION /* access definitions */

#include <string.h>
#include "native.h"
#include "xxhash.h"

// Get hashmap, return the matching ID, and if not found, return -1.
int64_t field_hashmap_get(FieldHashMap *fmap, const GoString* key){
XXH64_hash_t seed = 123456789;
size_t len = fmap->N;
uint64_t hash = XXH64(key->name, len, seed) % len;
int64_t id = -1;
uint64_t hash = XXH64(key->buf, key->len, seed);
uint64_t index = hash % fmap->N;

if(fmap->bucket[hash] != NULL \
&& fmap->bucket[hash]->name->len == key->len \
&& memcmp(fmap->bucket[hash]->name->buf, key->buf, key->len)){
id = fmap->bucket[hash]->id;
while (fmap->bucket[index].hash != 0) {
if(fmap->bucket[index].hash == hash \
&& fmap->bucket[index].name.len == key->len \
&& memcmp(fmap->bucket[index].name.buf, key->buf, key->len) == 0){ // memcmp 返回值是0 时才说明字符串相等
return fmap->bucket[index].id;
} else {
index = (index + 1) % fmap->N;
}
}
return id;
return -1;
}
20 changes: 14 additions & 6 deletions native/unittest/test_hashget.c
Expand Up @@ -7,20 +7,27 @@
#include "../native.h"
#include "../hashmap_get.c"

// Set hashmap.
void field_hashmap_set(FieldHashMap *fmap, const GoString* key, int64_t id){
XXH64_hash_t seed = 123456789;
size_t len = fmap->N;
uint64_t hash = XXH64(key->name, len, seed) % len;
fmap->bucket[hash]->name = key;
fmap->bucket[hash]->hash = hash;
fmap->bucket[hash]->id = id;
uint64_t hash = XXH64(key->buf, key->len, seed);
uint64_t index = hash % fmap->N;

while (fmap->bucket[index].hash != 0) {
index = (index + 1) % fmap->N;
}

fmap->bucket[index].name = (*key);
fmap->bucket[index].hash = hash;
fmap->bucket[index].id = id;
return;
}

int main() {
FieldHashMap map;
map.N = 10000;
map.bucket = NULL;
map.bucket = (FieldEntry*)malloc(sizeof(FieldEntry) * map.N);
memset(map.bucket, 0, sizeof(sizeof(FieldEntry) * map.N));

GoString key1;
GoString key2;
Expand Down Expand Up @@ -49,4 +56,5 @@ int main() {
field_hashmap_set(&map, &key3, 3);
value3 = field_hashmap_get(&map, &key3);
printf("The value3 is: %" PRId64 "\n", value3);
free(map.bucket);
}

0 comments on commit ec953f0

Please sign in to comment.