Skip to content

Commit

Permalink
[move] Update some calls to is_empty in loops
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Oct 18, 2024
1 parent 3d9d5f2 commit d691f79
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions crates/sui-framework/docs/move-stdlib/vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Pushes all of the elements of the <code>other</code> vector into the <code>lhs</

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/vector.md#0x1_vector_append">append</a>&lt;Element&gt;(lhs: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;, <b>mut</b> other: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;) {
other.<a href="../move-stdlib/vector.md#0x1_vector_reverse">reverse</a>();
<b>while</b> (!other.<a href="../move-stdlib/vector.md#0x1_vector_is_empty">is_empty</a>()) lhs.<a href="../move-stdlib/vector.md#0x1_vector_push_back">push_back</a>(other.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>());
<b>while</b> (other.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() != 0) lhs.<a href="../move-stdlib/vector.md#0x1_vector_push_back">push_back</a>(other.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>());
other.<a href="../move-stdlib/vector.md#0x1_vector_destroy_empty">destroy_empty</a>();
}
</code></pre>
Expand Down Expand Up @@ -504,7 +504,7 @@ Aborts if <code>i</code> is out of bounds.


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/vector.md#0x1_vector_swap_remove">swap_remove</a>&lt;Element&gt;(v: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;, i: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>): Element {
<b>assert</b>!(!v.<a href="../move-stdlib/vector.md#0x1_vector_is_empty">is_empty</a>(), <a href="../move-stdlib/vector.md#0x1_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>);
<b>assert</b>!(v.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() != 0, <a href="../move-stdlib/vector.md#0x1_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>);
<b>let</b> last_idx = v.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() - 1;
v.<a href="../move-stdlib/vector.md#0x1_vector_swap">swap</a>(i, last_idx);
v.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>()
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/docs/sui-framework/vec_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Pop the most recently inserted entry from the map. Aborts if the map is empty.


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_pop">pop</a>&lt;K: <b>copy</b>, V&gt;(self: &<b>mut</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_VecMap">VecMap</a>&lt;K, V&gt;): (K, V) {
<b>assert</b>!(!self.contents.<a href="../sui-framework/vec_map.md#0x2_vec_map_is_empty">is_empty</a>(), <a href="../sui-framework/vec_map.md#0x2_vec_map_EMapEmpty">EMapEmpty</a>);
<b>assert</b>!(self.contents.length() != 0, <a href="../sui-framework/vec_map.md#0x2_vec_map_EMapEmpty">EMapEmpty</a>);
<b>let</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_Entry">Entry</a> { key, value } = self.contents.pop_back();
(key, value)
}
Expand Down Expand Up @@ -526,7 +526,7 @@ and are *not* sorted.
keys.reverse();
values.reverse();
<b>let</b> <b>mut</b> map = <a href="../sui-framework/vec_map.md#0x2_vec_map_empty">empty</a>();
<b>while</b> (!keys.<a href="../sui-framework/vec_map.md#0x2_vec_map_is_empty">is_empty</a>()) map.<a href="../sui-framework/vec_map.md#0x2_vec_map_insert">insert</a>(keys.pop_back(), values.pop_back());
<b>while</b> (keys.length() != 0) map.<a href="../sui-framework/vec_map.md#0x2_vec_map_insert">insert</a>(keys.pop_back(), values.pop_back());
keys.<a href="../sui-framework/vec_map.md#0x2_vec_map_destroy_empty">destroy_empty</a>();
values.<a href="../sui-framework/vec_map.md#0x2_vec_map_destroy_empty">destroy_empty</a>();
map
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-framework/docs/sui-framework/vec_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ and are *not* sorted.
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/vec_set.md#0x2_vec_set_from_keys">from_keys</a>&lt;K: <b>copy</b> + drop&gt;(<b>mut</b> keys: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;K&gt;): <a href="../sui-framework/vec_set.md#0x2_vec_set_VecSet">VecSet</a>&lt;K&gt; {
keys.reverse();
<b>let</b> <b>mut</b> set = <a href="../sui-framework/vec_set.md#0x2_vec_set_empty">empty</a>();
<b>while</b> (!keys.<a href="../sui-framework/vec_set.md#0x2_vec_set_is_empty">is_empty</a>()) set.<a href="../sui-framework/vec_set.md#0x2_vec_set_insert">insert</a>(keys.pop_back());
<b>while</b> (keys.length() != 0) set.<a href="../sui-framework/vec_set.md#0x2_vec_set_insert">insert</a>(keys.pop_back());
set
}
</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-framework/docs/sui-system/voting_power.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Update validators with the decided voting power.


<pre><code><b>fun</b> <a href="voting_power.md#0x3_voting_power_update_voting_power">update_voting_power</a>(validators: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Validator&gt;, <b>mut</b> info_list: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="voting_power.md#0x3_voting_power_VotingPowerInfoV2">VotingPowerInfoV2</a>&gt;) {
<b>while</b> (!info_list.is_empty()) {
<b>while</b> (info_list.length() != 0) {
<b>let</b> <a href="voting_power.md#0x3_voting_power_VotingPowerInfoV2">VotingPowerInfoV2</a> {
validator_index,
<a href="voting_power.md#0x3_voting_power">voting_power</a>,
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-framework/packages/move-stdlib/sources/vector.move
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public fun reverse<Element>(v: &mut vector<Element>) {
/// Pushes all of the elements of the `other` vector into the `lhs` vector.
public fun append<Element>(lhs: &mut vector<Element>, mut other: vector<Element>) {
other.reverse();
while (!other.is_empty()) lhs.push_back(other.pop_back());
while (other.length() != 0) lhs.push_back(other.pop_back());
other.destroy_empty();
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public fun insert<Element>(v: &mut vector<Element>, e: Element, mut i: u64) {
/// This is O(1), but does not preserve ordering of elements in the vector.
/// Aborts if `i` is out of bounds.
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element {
assert!(!v.is_empty(), EINDEX_OUT_OF_BOUNDS);
assert!(v.length() != 0, EINDEX_OUT_OF_BOUNDS);
let last_idx = v.length() - 1;
v.swap(i, last_idx);
v.pop_back()
Expand All @@ -176,7 +176,7 @@ public macro fun tabulate<$T>($n: u64, $f: |u64| -> $T): vector<$T> {
/// Does not preserve the order of elements in the vector (starts from the end of the vector).
public macro fun destroy<$T>($v: vector<$T>, $f: |$T|) {
let mut v = $v;
while (!v.is_empty()) $f(v.pop_back());
while (v.length() != 0) $f(v.pop_back());
v.destroy_empty();
}

Expand All @@ -185,7 +185,7 @@ public macro fun destroy<$T>($v: vector<$T>, $f: |$T|) {
public macro fun do<$T>($v: vector<$T>, $f: |$T|) {
let mut v = $v;
v.reverse();
while (!v.is_empty()) $f(v.pop_back());
while (v.length() != 0) $f(v.pop_back());
v.destroy_empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public fun remove<K: copy, V>(self: &mut VecMap<K, V>, key: &K): (K, V) {

/// Pop the most recently inserted entry from the map. Aborts if the map is empty.
public fun pop<K: copy, V>(self: &mut VecMap<K, V>): (K, V) {
assert!(!self.contents.is_empty(), EMapEmpty);
assert!(self.contents.length() != 0, EMapEmpty);
let Entry { key, value } = self.contents.pop_back();
(key, value)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public fun from_keys_values<K: copy, V>(mut keys: vector<K>, mut values: vector<
keys.reverse();
values.reverse();
let mut map = empty();
while (!keys.is_empty()) map.insert(keys.pop_back(), values.pop_back());
while (keys.length() != 0) map.insert(keys.pop_back(), values.pop_back());
keys.destroy_empty();
values.destroy_empty();
map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public fun into_keys<K: copy + drop>(self: VecSet<K>): vector<K> {
public fun from_keys<K: copy + drop>(mut keys: vector<K>): VecSet<K> {
keys.reverse();
let mut set = empty();
while (!keys.is_empty()) set.insert(keys.pop_back());
while (keys.length() != 0) set.insert(keys.pop_back());
set
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module sui_system::voting_power {

/// Update validators with the decided voting power.
fun update_voting_power(validators: &mut vector<Validator>, mut info_list: vector<VotingPowerInfoV2>) {
while (!info_list.is_empty()) {
while (info_list.length() != 0) {
let VotingPowerInfoV2 {
validator_index,
voting_power,
Expand Down
Binary file modified crates/sui-framework/packages_compiled/move-stdlib
Binary file not shown.
Binary file modified crates/sui-framework/packages_compiled/sui-framework
Binary file not shown.
Binary file modified crates/sui-framework/packages_compiled/sui-system
Binary file not shown.
1 change: 1 addition & 0 deletions crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const MAX_PROTOCOL_VERSION: u64 = 65;
// Version 62: Makes the event's sending module package upgrade-aware.
// Version 63: Enable gas based congestion control in consensus commit.
// Version 64: Switch to distributed vote scoring in consensus in mainnet
// Version 66: Update to Move stdlib.

#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProtocolVersion(u64);
Expand Down

0 comments on commit d691f79

Please sign in to comment.