Util: Add size counting to Table

This commit is contained in:
Jeffrey Pfau 2016-10-31 23:45:14 -07:00
parent 0870c98c6c
commit bb7d85698b
3 changed files with 15 additions and 0 deletions

View File

@ -39,6 +39,7 @@ Misc:
- Debugger: Make building with debugging aspects optional
- GBA Memory: Support for Mo Jie Qi Bing by Vast Fame (taizou)
- GBA Memory: Support reading/writing POSTFLG
- Util: Add size counting to Table
0.5.1: (2016-10-05)
Bugfixes:

View File

@ -54,6 +54,7 @@ static struct TableList* _resizeAsNeeded(struct Table* table, struct TableList*
static void _removeItemFromList(struct Table* table, struct TableList* list, size_t item) {
--list->nEntries;
--table->size;
free(list->list[item].stringKey);
if (table->deinitializer) {
table->deinitializer(list->list[item].value);
@ -119,6 +120,7 @@ void TableInsert(struct Table* table, uint32_t key, void* value) {
list->list[list->nEntries].stringKey = 0;
list->list[list->nEntries].value = value;
++list->nEntries;
++table->size;
}
void TableRemove(struct Table* table, uint32_t key) {
@ -156,6 +158,10 @@ void TableEnumerate(const struct Table* table, void (handler(uint32_t key, void*
}
}
size_t TableSize(const struct Table* table) {
return table->size;
}
void* HashTableLookup(const struct Table* table, const char* key) {
uint32_t hash = hash32(key, strlen(key), 0);
const struct TableList* list;
@ -181,6 +187,7 @@ void HashTableInsert(struct Table* table, const char* key, void* value) {
list->list[list->nEntries].keylen = strlen(key);
list->list[list->nEntries].value = value;
++list->nEntries;
++table->size;
}
void HashTableRemove(struct Table* table, const char* key) {
@ -219,3 +226,7 @@ void HashTableEnumerate(const struct Table* table, void (handler(const char* key
}
}
}
size_t HashTableSize(const struct Table* table) {
return table->size;
}

View File

@ -13,6 +13,7 @@ struct TableList;
struct Table {
struct TableList* table;
size_t tableSize;
size_t size;
void (*deinitializer)(void*);
};
@ -26,6 +27,7 @@ void TableRemove(struct Table*, uint32_t key);
void TableClear(struct Table*);
void TableEnumerate(const struct Table*, void (handler(uint32_t key, void* value, void* user)), void* user);
size_t TableSize(const struct Table*);
static inline void HashTableInit(struct Table* table, size_t initialSize, void (deinitializer(void*))) {
TableInit(table, initialSize, deinitializer);
@ -42,5 +44,6 @@ void HashTableRemove(struct Table*, const char* key);
void HashTableClear(struct Table*);
void HashTableEnumerate(const struct Table*, void (handler(const char* key, void* value, void* user)), void* user);
size_t HashTableSize(const struct Table*);
#endif