-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[sui-framework] Added swap and swap_remove function to the table_vec.move #13644
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
3 Ignored Deployments
|
After testing, I found the So, I implemented the other function |
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 think you're missing the remove function here but swap
looks mostly there! Can you also add some tests for these new functions?
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.
+1 to @amnn's comment. And could you add tests in table_vec_tests.move
? Thanks!
There is no remove function because I think it can be done with swap with i th and last, then pop_back. For example, if I want to remove i th element from TableVec v:
|
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.
Thanks @WayneAl, could you add a couple more tests? I've noted them in the inline comments.
Also, regarding remove
, we actually have an API called swap_remove
on vector:
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element { |
Which is analogous to what you're trying to do here, so it wouldn't be a bad idea to include that, but that's not technically a blocker for this PR (as long as we rename it to mention swap
rather than remove
).
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.
Some more test cases to add here:
i == j
(succeeds)i == j > length
(aborts)
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.
Added !
And I also added the swap_remove
and its test.
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.
Looks great, thanks @WayneAl ! The PR needs some tests fixed, leave it with me, and I'll prepare and land it over the next couple of days.
Description
Added
swap_remove
function totable_vec.move
since we have a situation to remove an certain element in theTableVec
and bothtable
andvector
have similar functionality implemented.Test Plan
New unit tests for
sui::table_vec
.If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process.
Type of Change (Check all that apply)
Release notes
Introduces Protocol Version 25, which adds
sui::table_vec::swap
andsui::table_vec::swap_remove
to system packages. Use these functions to swap two positions in a singleTableVec<T>
or swap an element to the end and remove it inO(1)
time, analogous tostd::vector::swap
andstd::vector::swap_remove
.