use hashmap in constants buffer to speed up translation time with large files
This commit is contained in:
130
src/hashmap/hashmap.c
Normal file
130
src/hashmap/hashmap.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "hashmap.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
struct hashmap *createHashmap() {
|
||||
size_t size = 8;
|
||||
struct hashmap *t = (struct hashmap *)malloc(sizeof(struct hashmap));
|
||||
t->size = size;
|
||||
t->order = 1;
|
||||
t->list = (struct node **)malloc(sizeof(struct node *) * size);
|
||||
memset(t->list, 0, sizeof(struct node *) * size);
|
||||
return t;
|
||||
}
|
||||
|
||||
void hashmap_free(struct hashmap *t, free_val_func free_val) {
|
||||
if (!t) return;
|
||||
|
||||
for (size_t i = 0; i < t->size; i++) {
|
||||
struct node *current = t->list[i];
|
||||
while (current) {
|
||||
struct node *next = current->next;
|
||||
if (free_val && current->val) {
|
||||
free_val(current->val);
|
||||
}
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
free(t->list);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void resize_hashmap(struct hashmap *t) {
|
||||
int old_size = t->size;
|
||||
int new_size = old_size * 2;
|
||||
|
||||
struct node **old_list = t->list;
|
||||
|
||||
// Create new list
|
||||
t->list = (struct node **)malloc(sizeof(struct node *) * new_size);
|
||||
memset(t->list, 0, sizeof(struct node *) * new_size);
|
||||
|
||||
t->size = new_size;
|
||||
t->count = 0;
|
||||
|
||||
// Rehash old entries into new list
|
||||
for (int i = 0; i < old_size; i++) {
|
||||
struct node *temp = old_list[i];
|
||||
while (temp) {
|
||||
hashmap_insert(t, temp->hash, temp->key, temp->val,
|
||||
temp->order); // Will increment count
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash) { return hash % t->size; }
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
struct node *prev = NULL;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
t->list[pos] = temp->next;
|
||||
return 1;
|
||||
}
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
list = NULL;
|
||||
prev = NULL;
|
||||
temp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
void *val, size_t order) {
|
||||
if (!order) {
|
||||
order = t->order++;
|
||||
}
|
||||
if ((t->count + 1) > t->size * 0.75) {
|
||||
resize_hashmap(t);
|
||||
}
|
||||
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
|
||||
// Check if key exists → overwrite
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
temp->val = val;
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// Insert new node
|
||||
struct node *newNode = (struct node *)malloc(sizeof(struct node));
|
||||
newNode->hash = hash;
|
||||
newNode->key = key;
|
||||
newNode->val = val;
|
||||
newNode->order = order;
|
||||
newNode->next = list;
|
||||
t->list[pos] = newNode;
|
||||
t->count++;
|
||||
}
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
return temp->val;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
37
src/hashmap/hashmap.h
Normal file
37
src/hashmap/hashmap.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef HASHMAP_H
|
||||
#define HASHMAP_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
|
||||
typedef void (*free_val_func)(void *val);
|
||||
|
||||
struct node {
|
||||
uint64_t hash;
|
||||
void *key;
|
||||
void *val;
|
||||
size_t order;
|
||||
struct node *next;
|
||||
};
|
||||
struct hashmap {
|
||||
size_t size;
|
||||
size_t count;
|
||||
size_t order;
|
||||
struct node **list;
|
||||
};
|
||||
|
||||
struct hashmap *createHashmap();
|
||||
|
||||
void hashmap_free(struct hashmap *t, free_val_func free_val);
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash);
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash);
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
void *val, size_t order);
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash);
|
||||
|
||||
#endif // HASHMAP_H
|
||||
Reference in New Issue
Block a user