Skip to content

Implement an Iterator API #5

@andrewthecodertx

Description

@andrewthecodertx

The current hash table API provides essential functions for inserting, looking up, and deleting key-value pairs. However, it lacks a mechanism for iterating over the elements currently in the table.

Iteration is a common and necessary feature for many use cases, such as:

  • Dumping all key-value pairs for debugging.
  • Serializing the contents of the table.
  • Performing an operation on every element.

Suggestion:
Implement an iterator API. A common C pattern for this is to define an iterator struct and a set of functions to control it:

// Opaque iterator struct
typedef struct HashTableIterator HashTableIterator;

// Creates a new iterator for the table
HashTableIterator* hash_table_iterator_create(HashTable* table);

// Advances the iterator and returns the next key-value pair
// Returns false when there are no more items.
bool hash_table_iterator_next(HashTableIterator* iter, void** key, void** value);

// Destroys the iterator
void hash_table_iterator_destroy(HashTableIterator* iter);

This would significantly enhance the utility of the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions