Add iterator to hashtable and hashtable unit tests

This commit is contained in:
Bartosz Podrygajlo
2024-10-24 09:43:35 +02:00
parent 2c07f0081e
commit e7f858f6b2
5 changed files with 144 additions and 2 deletions
+21
View File
@@ -305,3 +305,24 @@ hashtable_rc_t hashtable_get(const hash_table_t *const hashtblP, const hash_key_
*dataP = NULL;
return HASH_TABLE_KEY_NOT_EXISTS;
}
hash_table_iterator_s hashtable_get_iterator(const hash_table_t *const hashtbl)
{
return (hash_table_iterator_s){.index = 0, .node = hashtbl->nodes[0], .hashtbl = hashtbl};
}
bool hashtable_iterator_getnext(hash_table_iterator_s *iterator, void **dataP)
{
// Iterate over table indexes
while (iterator->node == NULL) {
iterator->index++;
if (iterator->index >= iterator->hashtbl->size) {
*dataP = NULL;
return false;
}
iterator->node = iterator->hashtbl->nodes[iterator->index];
}
*dataP = iterator->node->data;
iterator->node = iterator->node->next;
return true;
}