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

Add rz_vector_assign_at() #4373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions librz/include/rz_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ RZ_API RZ_BORROW void **rz_pvector_find(RZ_NONNULL const RzPVector *vec, RZ_NONN
// join two pvector into one, pvec1 should free the joined element in pvec2
RZ_API bool rz_pvector_join(RZ_NONNULL RzPVector *pvec1, RZ_NONNULL RzPVector *pvec2);

RZ_API void *rz_pvector_assign_at(RZ_BORROW RZ_NONNULL RzPVector *vec, size_t index, RZ_OWN RZ_NONNULL void *ptr);

// removes and returns the pointer at the given index. Does not call free.
RZ_API void *rz_pvector_remove_at(RzPVector *vec, size_t index);

Expand Down
20 changes: 20 additions & 0 deletions librz/util/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,26 @@ RZ_API bool rz_pvector_join(RZ_NONNULL RzPVector *pvec1, RZ_NONNULL RzPVector *p
return true;
}

/**
* \brief Assign the pointer \p ptr at \p index in the pvector.
*
* \param vec The pvector to assign to.
* \param index The index to assign the pointer to.
* \param ptr The pointer to assign.
*
* \return The pointer stored at \p index before. Or NULL in case of failure.
*/
RZ_API void *rz_pvector_assign_at(RZ_BORROW RZ_NONNULL RzPVector *vec, size_t index, RZ_OWN RZ_NONNULL void *ptr) {
Copy link
Member

Choose a reason for hiding this comment

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

why you need this when rz_pvector_set is available?

Copy link
Member Author

Choose a reason for hiding this comment

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

rz_pvector_set is private. My thinking was, that it is better to have an equivalent function to the normal rz_vector_assign_at. So we don't mix up names with function between RzVector and RzPVector.

Copy link
Member

Choose a reason for hiding this comment

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

well, it's an inline within the headers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Something is wrong with this week -.-
Yeah, it is not necessary to add it than. rz_pvector_get and rz_pvector_set is good enough. Let me close this one.

rz_return_val_if_fail(vec && ptr, NULL);
void **p = rz_vector_index_ptr(&vec->v, index);
if (!p) {
return NULL;
}
void *prev = *p;
rz_vector_assign_at(&vec->v, index, ptr);
return prev;
}

RZ_API void *rz_pvector_remove_at(RzPVector *vec, size_t index) {
rz_return_val_if_fail(vec, NULL);
void *r = rz_pvector_at(vec, index);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,24 @@ static bool test_pvector_contains(void) {
mu_end;
}

static bool test_pvector_assign_at(void) {
RzPVector v;
init_test_pvector(&v, 5, 0);
ut32 *x = malloc(sizeof(ut32));
*x = 123467890;
ut32 *e = rz_pvector_assign_at(&v, 3, &x);
mu_assert_eq(*e, 3, "assign_at ret");
free(e);
mu_assert_eq(v.v.len, 5UL, "assign_at => len");
mu_assert_eq(*((ut32 **)v.v.a)[0], 0, "assign_at => content at 0");
mu_assert_eq(*((ut32 **)v.v.a)[1], 1, "assign_at => content at 1");
mu_assert_eq(*((ut32 **)v.v.a)[2], 2, "assign_at => content at 2");
mu_assert_eq(*((ut32 **)v.v.a)[3], 123467890, "assign_at => content at 3");
mu_assert_eq(*((ut32 **)v.v.a)[4], 4, "assign_at => content at 4");
rz_pvector_clear(&v);
mu_end;
}

static bool test_pvector_remove_at(void) {
RzPVector v;
init_test_pvector(&v, 5, 0);
Expand Down Expand Up @@ -1438,6 +1456,7 @@ static int all_tests(void) {
mu_run_test(test_pvector_join);
mu_run_test(test_pvector_contains);
mu_run_test(test_pvector_remove_at);
mu_run_test(test_pvector_assign_at);
mu_run_test(test_pvector_insert);
mu_run_test(test_pvector_insert_range);
mu_run_test(test_pvector_pop);
Expand Down
Loading