1
0
Fork 0

07: C: Hash entries

This commit is contained in:
Mia Herkt 2022-12-07 22:28:25 +01:00
parent 59bf88cb6b
commit 6060b590d3
Signed by: mia
GPG key ID: 72E154B8622EC191

View file

@ -1,9 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
uint32_t fnv1a_32(const char *buf, size_t len)
{
uint32_t h = 0x811c9dc5U;
for (; len > 0; len--)
h = 0x01000193U * (h ^ *buf++);
return h;
}
typedef struct entry {
char *name;
uint32_t id;
int isdir;
size_t size;
size_t n_entries;
@ -13,9 +23,10 @@ typedef struct entry {
} entry;
entry *findent(entry *e, int isdir, const char *name) {
uint32_t id = fnv1a_32(name, strlen(name));
for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->isdir == isdir)
if (!strcmp(e->entries[i]->name, name))
if (e->entries[i]->id == id)
return e->entries[i];
}
@ -33,9 +44,8 @@ entry *addent(entry *parent, int isdir, const char *name) {
parent->n_entries++;
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, strlen(name));
e->isdir = isdir;
e->name = malloc(strlen(name) + 1);
strcpy(e->name, name);
e->parent = parent;
return e;