From ad8ec070be7be23ba1720102cfcadd275c2bad3b Mon Sep 17 00:00:00 2001 From: Matheus Date: Sat, 24 Sep 2022 17:12:31 -0300 Subject: [PATCH] hashtable: get entry by offset --- src/include/hashtable.h | 2 ++ src/include/hashtable/hashtable.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/include/hashtable.h b/src/include/hashtable.h index 7f165aa1..f48f85eb 100644 --- a/src/include/hashtable.h +++ b/src/include/hashtable.h @@ -3,6 +3,7 @@ #include #include +#include typedef struct{ void* next; @@ -16,6 +17,7 @@ typedef struct{ hashtableentry* entries; } hashtable; +void* hashtable_fromoffset(hashtable*, uintmax_t, char*); bool hashtable_init(hashtable*, size_t); bool hashtable_insert(hashtable*, char*, void*); bool hashtable_remove(hashtable*, char*); diff --git a/src/include/hashtable/hashtable.c b/src/include/hashtable/hashtable.c index 58725544..9307ee8d 100644 --- a/src/include/hashtable/hashtable.c +++ b/src/include/hashtable/hashtable.c @@ -27,6 +27,27 @@ static inline hashtableentry* getentry(hashtable* table, char* key){ return &table->entries[entryn]; } +void* hashtable_fromoffset(hashtable* table, uintmax_t offset, char* retkey){ + + for(uintmax_t i = 0; i < table->size; ++i){ + hashtableentry* entry = &table->entries[i]; + if(!entry->val) + continue; + while(entry){ + if(offset-- == 0){ + if(retkey) + strcpy(retkey, entry->key); + return entry->val; + } + + entry = entry->next; + } + } + + return NULL; + +} + bool hashtable_insert(hashtable* table, char* key, void* val){