-
-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement one line foreach
macros for hash tables.
#4500
Conversation
The file only contains tests for the hash table. Hence the name change.
@Rot127 one bindgen linter error:
|
/** | ||
* \brief Foreach iterator over a RzSetS. The set elements can be accessed via \p it->kv.key. | ||
*/ | ||
#define rz_set_s_foreach(s, it) ht_foreach (sp, s, it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing elements of sets via it->kv.key
is not user friendly. User is not supposed to know about internals of RzSetS
.
const char *elem;
rz_set_s_foreach(s, it, elem) { ... }
You will have to use modified ht_foreach
body to implement this.
|
||
#define ht_foreach(type, ht, iter) \ | ||
if (ht && ht_##type##_size(ht) > 0) \ | ||
for (iter.ti = 0, iter.bi = 0, iter.kv = NULL, ht_##type##_advance_iter(ht, &iter); iter.kv != NULL; ht_##type##_advance_iter(ht, &iter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it would be preferable to have more standard iterator API with first()/next()
For example loop statement could look like:
for (iter = Ht_(iter_first)(ht) /*As I see it is ok to return struct from API*/; iter.kv != NULL; Ht_(iter_next)(ht, &iter))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, makes sense. Check also:
librz/util/iterator.c
librz/include/rz_util/rz_iterator.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point we could also just add a RzIterator *ht_xx_as_iter();
, right?
Otherwise, we just duplicate the API from iterators to hash tables.
Please rebase. |
Superseded by #4639 |
Your checklist for this pull request
Detailed description
Adds one line
foreach
macros for hash tables and in extend sets.Additionally:
for each
mechanism toht_foreach_cb
(cb
forcall back
), so they can be distinguished.ht_xx_size()
function for simpler access to theht->count
member.test_sdb_hash.c
totest_ht.c
. Because it was hard to find before with a search.Test plan
Tests added.
Closing issues
...