-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (iterator): Added a hashamp iterator and the
th_len
function
- Loading branch information
Showing
14 changed files
with
391 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,4 @@ build | |
result | ||
/debug.c | ||
a.py | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <stdlib.h> | ||
|
||
#include "iterator.h" | ||
|
||
static void th_iterator_init(th_iterator_t *it, | ||
th_generic_table_t generic_table, | ||
th_iterator_next_func_t next) { | ||
it->index = 0; | ||
it->key = NULL; | ||
it->current = NULL; | ||
it->value = NULL; | ||
it->generic_table = generic_table; | ||
it->next = next; | ||
} | ||
|
||
th_iterator_t *th_iterator_create(th_generic_table_t generic_table, | ||
th_iterator_next_func_t next) { | ||
th_iterator_t *it = malloc(sizeof(th_iterator_t)); | ||
|
||
if (it == NULL) { | ||
return NULL; | ||
} | ||
|
||
th_iterator_init(it, generic_table, next); | ||
|
||
return it; | ||
} | ||
|
||
void th_iterator_free(th_iterator_t *it) { | ||
th_iterator_init(it, NULL, NULL); | ||
|
||
free(it); | ||
} | ||
|
||
bool th_iterator_next(th_iterator_t **ptr) { | ||
th_iterator_t *it = *ptr; | ||
|
||
if (it->next == NULL) { | ||
return false; | ||
} | ||
|
||
bool ret = it->next(ptr); | ||
|
||
if (ret == false) { | ||
th_iterator_free(it); | ||
} | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef __TINYHASH_COMMON_ITERATOR_H__ | ||
#define __TINYHASH_COMMON_ITERATOR_H__ | ||
|
||
#include "key.h" | ||
#include "types.h" | ||
|
||
/** | ||
* @brief Represents an iterator that allow to iterate over a generic table. | ||
* | ||
*/ | ||
typedef struct th_iterator_s { | ||
int index; | ||
th_any_t current; | ||
th_key_t *key; | ||
th_any_t value; | ||
th_generic_table_t generic_table; | ||
bool (*next)(struct th_iterator_s **); | ||
} th_iterator_t; | ||
|
||
/** | ||
* @brief Pointer on function that get the next element. | ||
* | ||
*/ | ||
typedef bool (*th_iterator_next_func_t)(th_iterator_t **); | ||
|
||
/** | ||
* @brief Allocate then init a new iterator. | ||
* | ||
* @param generic_table | ||
* @param next | ||
* @return th_iterator_t* | ||
*/ | ||
th_iterator_t *th_iterator_create(th_generic_table_t generic_table, | ||
th_iterator_next_func_t next); | ||
|
||
/** | ||
* @brief Free an iterator. | ||
* | ||
* @param it | ||
*/ | ||
void th_iterator_free(th_iterator_t *it); | ||
|
||
/** | ||
* @brief Try to get the next element. | ||
* Free the iterator if it reachs the end. | ||
* | ||
* @param ptr | ||
* @return true | ||
* @return false | ||
*/ | ||
bool th_iterator_next(th_iterator_t **ptr); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.