Skip to content
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

Closed
wants to merge 10 commits into from

Conversation

Rot127
Copy link
Member

@Rot127 Rot127 commented May 18, 2024

Your checklist for this pull request

  • I've read the guidelines for contributing to this repository
  • I made sure to follow the project's coding style
  • I've documented or updated the documentation of every function and struct this PR changes. If not so I've explained why.
  • I've added tests that prove my fix is effective or that my feature works (if possible)
  • I've updated the rizin book with the relevant information (if needed)

Detailed description

Adds one line foreach macros for hash tables and in extend sets.

Additionally:

  • Renames the already existing for each mechanism to ht_foreach_cb (cb for call back), so they can be distinguished.
  • it adds a ht_xx_size() function for simpler access to the ht->count member.
  • Renames file for hash table unit tests from test_sdb_hash.c to test_ht.c. Because it was hard to find before with a search.

Test plan

Tests added.

Closing issues

...

@Rot127 Rot127 requested a review from pelijah May 18, 2024 12:50
@Rot127 Rot127 mentioned this pull request May 18, 2024
5 tasks
@XVilka
Copy link
Member

XVilka commented May 18, 2024

@Rot127 one bindgen linter error:

Mismatched function annotation for rz_set_s_size at <rizin/librz/include/rz_util/rz_set.h:23:13> : RZ_NONNULL (was RZ_NULLABLE at <rizin/librz/util/set.c:75:13>

librz/include/rz_util/rz_set.h Outdated Show resolved Hide resolved
.clang-format Outdated Show resolved Hide resolved
librz/include/rz_util/rz_set.h Outdated Show resolved Hide resolved
librz/include/rz_util/ht_inc.h Outdated Show resolved Hide resolved
librz/include/rz_util/ht_inc.h Show resolved Hide resolved
/**
* \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)
Copy link
Contributor

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))
Copy link
Contributor

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))

@XVilka @kazarmy @wargio @ret2libc your opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! 👍

Copy link
Member

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

Copy link
Member Author

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.

@XVilka XVilka requested a review from imbillow May 21, 2024 15:24
@XVilka
Copy link
Member

XVilka commented Sep 17, 2024

Please rebase.

@Rot127
Copy link
Member Author

Rot127 commented Sep 18, 2024

Superseded by #4639

@Rot127 Rot127 closed this Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants