From f54f1771b91c44a750d8bf257e094c12beedb1d7 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sat, 12 Jun 2021 10:12:55 -0700 Subject: [PATCH] [maps] queues instead of stacks to keep things in order --- source/md.h | 11 +++++++++-- source/md_impl.c | 27 +++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/source/md.h b/source/md.h index b109b78..f1a5438 100644 --- a/source/md.h +++ b/source/md.h @@ -398,11 +398,18 @@ struct MD_MapSlot void *val; }; +typedef struct MD_MapBucket MD_MapBucket; +struct MD_MapBucket +{ + MD_MapSlot *first; + MD_MapSlot *last; +}; + typedef struct MD_Map MD_Map; struct MD_Map { - MD_u64 table_size; - MD_MapSlot **table; + MD_MapBucket *buckets; + MD_u64 bucket_count; }; //~ Token kinds. diff --git a/source/md_impl.c b/source/md_impl.c index 25f0047..983bf56 100644 --- a/source/md_impl.c +++ b/source/md_impl.c @@ -943,10 +943,10 @@ MD_MapMakeBucketCount(MD_u64 bucket_count){ // TODO(allen): permanent arena? scratch arena? -- would really // make most sense with a parameter MD_Map result = {0}; - result.table_size = bucket_count; + result.bucket_count = bucket_count; // TODO(allen): push array zero - result.table = MD_PushArray(MD_MapSlot*, bucket_count); - memset(result.table, 0, sizeof(*result.table)*bucket_count); + result.buckets = MD_PushArray(MD_MapBucket, bucket_count); + memset(result.buckets, 0, sizeof(*result.buckets)*bucket_count); return(result); } @@ -983,9 +983,9 @@ MD_MapKeyPtr(void *ptr){ MD_FUNCTION_IMPL MD_MapSlot* MD_MapLookup(MD_Map *map, MD_MapKey key){ MD_MapSlot *result = 0; - if (map->table_size > 0){ - MD_u64 index = key.hash%map->table_size; - result = MD_MapScan(map->table[index], key); + if (map->bucket_count > 0){ + MD_u64 index = key.hash%map->bucket_count; + result = MD_MapScan(map->buckets[index].first, key); } return(result); } @@ -1022,14 +1022,21 @@ MD_MapScan(MD_MapSlot *first_slot, MD_MapKey key){ MD_FUNCTION_IMPL MD_MapSlot* MD_MapInsert(MD_Map *map, MD_MapKey key, void *val){ MD_MapSlot *result = 0; - if (map->table_size > 0){ - MD_u64 index = key.hash%map->table_size; + if (map->bucket_count > 0){ + MD_u64 index = key.hash%map->bucket_count; // TODO(allen): again, memory? permanent arena? scratch arena? // should definitely match the table's memory "object" MD_MapSlot *slot = MD_PushArray(MD_MapSlot, 1); // TODO(allen): queue push - slot->next = map->table[index]; - map->table[index] = slot; + MD_MapBucket *bucket = &map->buckets[index]; + if (bucket->first == 0){ + bucket->first = slot; + } + else{ + bucket->last->next = slot; + } + bucket->last = slot; + slot->next = 0; slot->key = key; slot->val = val; result = slot;