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

[sui framework] add vec_map::remove_entry_by_idx #4193

Merged
merged 4 commits into from
Sep 9, 2022
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: common_costs
---
{
"MergeCoin": {
"computation_cost": 464,
"computation_cost": 465,
"storage_cost": 32,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 522,
"computation_cost": 523,
"storage_cost": 83,
"storage_rebate": 0
},
Expand All @@ -29,7 +29,7 @@ expression: common_costs
"storage_rebate": 15
},
"SplitCoin": {
"computation_cost": 574,
"computation_cost": 575,
"storage_cost": 80,
"storage_rebate": 0
},
Expand Down
8 changes: 8 additions & 0 deletions crates/sui-framework/sources/vec_map.move
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,12 @@ module sui::vec_map {
let entry = vector::borrow_mut(&mut self.contents, idx);
(&entry.key, &mut entry.value)
}

/// Remove the entry at index `idx` from self.
/// Aborts if `idx` is greater than or equal to `size(self)`
public fun remove_entry_by_idx<K: copy, V>(self: &mut VecMap<K, V>, idx: u64): (K, V) {
assert!(idx < size(self), EIndexOutOfBounds);
let Entry { key, value } = vector::remove(&mut self.contents, idx);
(key, value)
}
}
33 changes: 29 additions & 4 deletions crates/sui-framework/tests/vec_map_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,37 @@ module sui::vec_map_tests {
}

#[test]
#[expected_failure(abort_code = 1)]
fun nonexistent_key_get_entry_by_idx() {
#[expected_failure(abort_code = 3)]
fun out_of_bounds_get_entry_by_idx() {
let m = vec_map::empty();
vec_map::insert(&mut m, 1, true);
let k = 2;
let _idx = vec_map::get_idx(&m, &k);
let idx = 1;
let (_key, _val) = vec_map::get_entry_by_idx(&m, idx);
}

#[test]
#[expected_failure(abort_code = 3)]
fun out_of_bounds_remove_entry_by_idx() {
let m = vec_map::empty();
vec_map::insert(&mut m, 10, true);
let idx = 1;
let (_key, _val) = vec_map::remove_entry_by_idx(&mut m, idx);
}

#[test]
fun remove_entry_by_idx() {
let m = vec_map::empty();
vec_map::insert(&mut m, 5, 50);
vec_map::insert(&mut m, 6, 60);
vec_map::insert(&mut m, 7, 70);

let (key, val) = vec_map::remove_entry_by_idx(&mut m, 0);
assert!(key == 5 && val == 50, 0);
assert!(vec_map::size(&m) == 2, 0);

let (key, val) = vec_map::remove_entry_by_idx(&mut m, 1);
assert!(key == 7 && val == 70, 0);
assert!(vec_map::size(&m) == 1, 0);
}

#[test]
Expand Down