summaryrefslogtreecommitdiff
path: root/src/hash_table.c
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-01-05 21:02:26 -0800
committerPreston Pan <preston@nullring.xyz>2023-01-05 21:02:26 -0800
commit5ecf1f46aae4994662bd8e3df189f8d60b49a304 (patch)
tree91b6a15d65c9e8d7a5e7ae87712830b1bf4f4eca /src/hash_table.c
parent7ff373076c59af3ba166e5080b8ecead569d5a44 (diff)
bind works for real, I think this works now
Diffstat (limited to 'src/hash_table.c')
-rw-r--r--src/hash_table.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/src/hash_table.c b/src/hash_table.c
index c3c77a4..99508e9 100644
--- a/src/hash_table.c
+++ b/src/hash_table.c
@@ -36,14 +36,29 @@ sl_list_t *init_sl_list() {
/* TODO: fix segfault bug */
void sl_list_add(sl_list_t *l, char *key, ast_t *value) {
+ sl_node_t *cur = l->head;
+ bool modified = false;
if (l->head == NULL) {
l->head = init_sl_node(key, value);
l->size++;
return;
- } else {
- sl_node_t *cur = l->head;
- while (cur->next != NULL)
- cur = cur->next;
+ }
+
+ for (int i = 0; i < l->size - 1; i++) {
+ if (strcmp(cur->value->key, key) == 0) {
+ cur->value->value = value;
+ modified = true;
+ break;
+ }
+ cur = cur->next;
+ }
+
+ if (strcmp(cur->value->key, key) == 0) {
+ cur->value->value = value;
+ modified = true;
+ }
+
+ if (!modified) {
cur->next = init_sl_node(key, value);
l->size++;
}
@@ -51,8 +66,6 @@ void sl_list_add(sl_list_t *l, char *key, ast_t *value) {
ast_t *sl_list_get(sl_list_t *l, char *key) {
sl_node_t *cur = l->head;
- if (cur == NULL)
- return NULL;
for (int i = 0; i < l->size; i++) {
if (strcmp(cur->value->key, key) == 0)
return cur->value->value;
@@ -70,10 +83,10 @@ bool sl_list_exists(sl_list_t *l, char *key) {
void sl_list_free(sl_list_t *l) {
sl_node_t *cur = l->head;
sl_node_t *tmp;
- while (cur != NULL) {
+ for (int i = 0; i < l->size; i++) {
tmp = cur;
cur = cur->next;
- free(cur);
+ free(tmp);
}
free(l);
}
@@ -93,10 +106,6 @@ hash_table_t *init_hash_table(int size) {
}
void hash_table_add(hash_table_t *h, char *key, ast_t *value) {
- if (hash_table_exists(h, key)) {
- printf("BUG!\n");
- return;
- }
sl_list_t *l = h->buckets[hash(key, h->size)];
sl_list_add(l, key, value);
}