mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 23:18:21 +00:00
hashtable: get entry by offset
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
void* next;
|
void* next;
|
||||||
@@ -16,6 +17,7 @@ typedef struct{
|
|||||||
hashtableentry* entries;
|
hashtableentry* entries;
|
||||||
} hashtable;
|
} hashtable;
|
||||||
|
|
||||||
|
void* hashtable_fromoffset(hashtable*, uintmax_t, char*);
|
||||||
bool hashtable_init(hashtable*, size_t);
|
bool hashtable_init(hashtable*, size_t);
|
||||||
bool hashtable_insert(hashtable*, char*, void*);
|
bool hashtable_insert(hashtable*, char*, void*);
|
||||||
bool hashtable_remove(hashtable*, char*);
|
bool hashtable_remove(hashtable*, char*);
|
||||||
|
|||||||
@@ -27,6 +27,27 @@ static inline hashtableentry* getentry(hashtable* table, char* key){
|
|||||||
return &table->entries[entryn];
|
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){
|
bool hashtable_insert(hashtable* table, char* key, void* val){
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user