rizin icon indicating copy to clipboard operation
rizin copied to clipboard

Extend `SetU`

Open Rot127 opened this issue 1 year ago • 9 comments

Your checklist for this pull request

  • [x] I've read the guidelines for contributing to this repository
  • [x] 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.
  • [x] 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

Did not add the doxygen yet. Wanted to get some feedback first.

  • Add a getter for length of SetU.
  • Add a foreach macro.

Test plan

Added

Closing issues

...

Rot127 avatar Feb 18 '24 10:02 Rot127

How about we change this PR to re-implement the sets properly? SetU works fine, if it uses HtUU (not in this implementation thoug, push some commits later). And we can implement SetP by simply using SetU but casting the pointers to ut64 before.

Rot127 avatar Feb 19 '24 04:02 Rot127

Also I couldn't find any unit tests specifically for the hash tables. Which should be definitely added.

Rot127 avatar Feb 19 '24 04:02 Rot127

Also I couldn't find any unit tests specifically for the hash tables. Which should be definitely added.

There are some in test/unit/test_sdb_hash.c (that's becase Ht was originally in SDB, then we moved the code to the main rizin repo but some stuff are still tied to SDB :( )

ret2libc avatar Feb 19 '24 08:02 ret2libc

@ret2libc Any opinion on the foreach macro construction? I found the implemented foreach loop via callbacks really unpractical. But It looks a little wonky. But if you in general approve it, we could have something like this for hash tables as well.

Rot127 avatar Feb 19 '24 10:02 Rot127

 test/unit/test_util.c:128:28: error: code should be clang-formatted [-Wclang-format-violations]
        set_u_foreach(set_u, it) {
                                  ^
test/unit/test_util.c:129:8: error: code should be clang-formatted [-Wclang-format-violations]
                        x++;
                            ^

XVilka avatar Apr 19 '24 11:04 XVilka

Ideally foreach macro should be implemented for HT and be adapted for sets then. But it might be a tough task to implement universal macro considering custom HTs like SdbHt. So for now it's ok to have foreach for sets only. And yet we also need the same for SetS.

pelijah avatar Apr 21 '24 12:04 pelijah

@Rot127 clang-format:

 Formatting [1762/1785] test/unit/test_util.c
test/unit/test_util.c:88:15: error: code should be clang-formatted [-Wclang-format-violations]
        set_u_foreach(set_u, it) {
                     ^
test/unit/test_util.c:100:15: error: code should be clang-formatted [-Wclang-format-violations]
        set_u_foreach(set_u, it) {
                     ^
test/unit/test_util.c:106:15: error: code should be clang-formatted [-Wclang-format-violations]
        set_u_foreach(set_u, it) {
                     ^
test/unit/test_util.c:125:15: error: code should be clang-formatted [-Wclang-format-violations]
        set_u_foreach(set_u, it) {
                     ^

Please also rebase on latest dev

XVilka avatar Apr 21 '24 16:04 XVilka

Then let me extend the PR to include more foreach macros:

So the todo list would be:

  • [ ] Use @pelijah suggested changes
  • Add foreach for:
    • [ ] HtUU (and in extend SetU)
    • [ ] HtUS (and in extend SetS)
    • [ ] HtSS, HtSU
    • [ ] Testing
  • [ ] Value should be always be a pointer, so writes are possible.

Rot127 avatar Apr 22 '24 07:04 Rot127

If you are going to implement foreach for HTs, it would be better to rename ht_xx_foreach APIs to avoid code refactoring. Here an idea of iterator type for ht_inc.h based on yours SetU iterator:

typedef struct Ht_(iter_t) {
	st64 ti; ///< Table index
	ut64 bi; ///< Bucket index
	HT_(Kv) *kv; ///< Current KV-pair.  Stop iteration if kv is NULL. 
} HT(Iter);

Also you need to provide in ht_inc.c some sort of Ht_(iter_init) and Ht_(iter_next) so you can just use the expression below in foreach macros:

for (Ht_(iter_init)(&iter); iter.kv != NULL && ... ; Ht_(iter_next)(&iter))

pelijah avatar Apr 22 '24 08:04 pelijah

Implemented by https://github.com/rizinorg/rizin/pull/4500

Rot127 avatar May 18 '24 12:05 Rot127