Test fnv hashing
This commit is contained in:
parent
3c946d0a54
commit
44c98773c0
32 changed files with 74 additions and 1 deletions
45
hmap.c
45
hmap.c
|
@ -55,6 +55,17 @@ uint64_t hash_fnv1a_64(uint8_t *key, size_t len) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
uint64_t hash_str_fnv1a_64(char * s) {
|
||||
uint64_t hash = FNV64_OFFSET_BASIS;
|
||||
|
||||
while (*s != '\0') {
|
||||
hash = hash ^ *s;
|
||||
hash = hash * FNV64_PRIME;
|
||||
s++;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
struct _map_node {
|
||||
struct _map_node *next;
|
||||
void * key;
|
||||
|
@ -194,7 +205,7 @@ void* hmap_insert(hmap *map, void *key, void *data) {
|
|||
} else {
|
||||
ret = hmap_node_attach(&map->buckets[idx], node, map->keysize);
|
||||
}
|
||||
map->len += 1;
|
||||
map->len++;
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
else
|
||||
|
@ -211,3 +222,35 @@ void* hmap_get(hmap *map, void *key) {
|
|||
ret = hmap_node_search(&map->buckets[idx], key, map->keysize);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* hmap_remove(hmap *map, void *key) {
|
||||
uint64_t hash;
|
||||
size_t idx;
|
||||
void *ret = NULL;
|
||||
hmap_node *previous = NULL;
|
||||
hmap_node *current;
|
||||
|
||||
hash = hash_fnv1a_64(key, map->keysize);
|
||||
idx = hash % map->capacity;
|
||||
current = &map->buckets[idx];
|
||||
if (current->key == NULL) return NULL;
|
||||
if (memcmp(current->key, key, map->keysize) == 0) {
|
||||
map->buckets[idx] = *current->next;
|
||||
}
|
||||
while (current->key != NULL) {
|
||||
if (memcmp(current->key, key, map->keysize) == 0) {
|
||||
ret = current->data;
|
||||
map->len--;
|
||||
if (previous != NULL) {
|
||||
previous->next = current;
|
||||
} else {
|
||||
map->buckets[idx] = *current->next;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -62,5 +62,6 @@ int haggis_store_filename(FILE *stream, haggis_filename *n);
|
|||
int haggis_load_filetype(FILE *stream, haggis_typeflag tag, haggis_filetype *file);
|
||||
int haggis_store_filetype(FILE *stream, haggis_filetype *filetype);
|
||||
uint64_t hash_fnv1a_64(uint8_t *key, size_t len);
|
||||
uint64_t hash_str_fnv1a_64(char *s);
|
||||
|
||||
#endif // !HAGGIS_PRIVATE_H
|
||||
|
|
|
@ -59,6 +59,9 @@ tests += store_file_sha1
|
|||
tests += load_file_sha1
|
||||
tests += store_file_sha256
|
||||
tests += load_file_sha256
|
||||
tests += fnv1a_hash_inode
|
||||
tests += fnv1a_hash_str
|
||||
|
||||
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
||||
|
||||
.PHONY: test
|
||||
|
|
BIN
test/check_header
Executable file
BIN
test/check_header
Executable file
Binary file not shown.
BIN
test/fnv1a_hash_inode
Executable file
BIN
test/fnv1a_hash_inode
Executable file
Binary file not shown.
15
test/fnv1a_hash_inode.c
Normal file
15
test/fnv1a_hash_inode.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "haggis_private.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int main() {
|
||||
uint64_t ret;
|
||||
union {
|
||||
ino_t val;
|
||||
u8 bytes[sizeof(ino_t)];
|
||||
} nod;
|
||||
nod.val = 4269;
|
||||
|
||||
ret = hash_fnv1a_64(&nod.bytes[0], sizeof(ino_t));
|
||||
assert(ret == 5664182581703653080);
|
||||
}
|
BIN
test/fnv1a_hash_str
Executable file
BIN
test/fnv1a_hash_str
Executable file
Binary file not shown.
11
test/fnv1a_hash_str.c
Normal file
11
test/fnv1a_hash_str.c
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "haggis_private.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int main() {
|
||||
uint64_t ret;
|
||||
char *key = "haggis";
|
||||
|
||||
ret = hash_str_fnv1a_64(key);
|
||||
assert(ret == 4320822873175422592);
|
||||
}
|
BIN
test/init_file_md5
Executable file
BIN
test/init_file_md5
Executable file
Binary file not shown.
BIN
test/init_file_sha1
Executable file
BIN
test/init_file_sha1
Executable file
Binary file not shown.
BIN
test/init_file_sha256
Executable file
BIN
test/init_file_sha256
Executable file
Binary file not shown.
BIN
test/load_device
Executable file
BIN
test/load_device
Executable file
Binary file not shown.
BIN
test/load_file_md5
Executable file
BIN
test/load_file_md5
Executable file
Binary file not shown.
BIN
test/load_file_sha1
Executable file
BIN
test/load_file_sha1
Executable file
Binary file not shown.
BIN
test/load_file_sha256
Executable file
BIN
test/load_file_sha256
Executable file
Binary file not shown.
BIN
test/load_md5
Executable file
BIN
test/load_md5
Executable file
Binary file not shown.
BIN
test/load_sha1
Executable file
BIN
test/load_sha1
Executable file
Binary file not shown.
BIN
test/load_sha256
Executable file
BIN
test/load_sha256
Executable file
Binary file not shown.
BIN
test/load_u16
Executable file
BIN
test/load_u16
Executable file
Binary file not shown.
BIN
test/load_u32
Executable file
BIN
test/load_u32
Executable file
Binary file not shown.
BIN
test/load_u64
Executable file
BIN
test/load_u64
Executable file
Binary file not shown.
BIN
test/store_device
Executable file
BIN
test/store_device
Executable file
Binary file not shown.
BIN
test/store_file_md5
Executable file
BIN
test/store_file_md5
Executable file
Binary file not shown.
BIN
test/store_file_sha1
Executable file
BIN
test/store_file_sha1
Executable file
Binary file not shown.
BIN
test/store_file_sha256
Executable file
BIN
test/store_file_sha256
Executable file
Binary file not shown.
BIN
test/store_header
Executable file
BIN
test/store_header
Executable file
Binary file not shown.
BIN
test/store_md5
Executable file
BIN
test/store_md5
Executable file
Binary file not shown.
BIN
test/store_sha1
Executable file
BIN
test/store_sha1
Executable file
Binary file not shown.
BIN
test/store_sha256
Executable file
BIN
test/store_sha256
Executable file
Binary file not shown.
BIN
test/store_u16
Executable file
BIN
test/store_u16
Executable file
Binary file not shown.
BIN
test/store_u32
Executable file
BIN
test/store_u32
Executable file
Binary file not shown.
BIN
test/store_u64
Executable file
BIN
test/store_u64
Executable file
Binary file not shown.
Loading…
Add table
Reference in a new issue