Skip to content

Commit

Permalink
Add swap method to Vec<T> (FuelLabs#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-sitko authored Jul 6, 2022
1 parent 9ebdf6c commit 223afb2
Show file tree
Hide file tree
Showing 13 changed files with 855 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,32 @@ impl<T> Vec<T> {
self.len -= 1;
Option::Some(read(self.buf.ptr() + self.len * size_of::<T>()))
}

/// Swaps two elements.
///
/// # Arguments
///
/// * element1_index - The index of the first element
/// * element2_index - The index of the second element
///
/// # Reverts
///
/// Reverts if `element1_index` or `element2_index` is greater than or equal to the length of vector.
pub fn swap(mut self, element1_index: u64, element2_index: u64) {
assert(element1_index < self.len);
assert(element2_index < self.len);

if element1_index == element2_index {
return;
}

let val_size = size_of::<T>();

let element1_ptr = self.buf.ptr() + element1_index * val_size;
let element2_ptr = self.buf.ptr() + element2_index * val_size;

let element1_val = read(element1_ptr);
copy(element2_ptr, element1_ptr, val_size);
write(element2_ptr, element1_val);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-F443649679DC6856'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-F443649679DC6856'
dependencies = ['core']

[[package]]
name = 'vec_swap_param1_out_of_bounds'
source = 'root'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "vec_swap_param1_out_of_bounds"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
script;

use std::{assert::assert, vec::Vec};

fn main() {
let mut vector = ~Vec::new();

let number0 = 0u8;
let number1 = 1u8;
let number2 = 2u8;

vector.push(number0);
vector.push(number1);
vector.push(number2);

assert(vector.len() == 3);
assert(vector.capacity() == 4);
assert(vector.is_empty() == false);

vector.swap(3, 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
validate_abi = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-626C5D02EBFA3B6A'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-626C5D02EBFA3B6A'
dependencies = ['core']

[[package]]
name = 'vec_swap_param2_out_of_bounds'
source = 'root'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "vec_swap_param2_out_of_bounds"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
script;

use std::{assert::assert, vec::Vec};

fn main() {
let mut vector = ~Vec::new();

let number0 = 0u8;
let number1 = 1u8;
let number2 = 2u8;

vector.push(number0);
vector.push(number1);
vector.push(number2);

assert(vector.len() == 3);
assert(vector.capacity() == 4);
assert(vector.is_empty() == false);

vector.swap(0, 3);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
validate_abi = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-E670B7A81773E7E0'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-E670B7A81773E7E0'
dependencies = ['core']

[[package]]
name = 'vec_swap'
source = 'root'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "vec_swap"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Loading

0 comments on commit 223afb2

Please sign in to comment.