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

stdlib Vec #1118

Merged
merged 39 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
364fbe1
Add partial implementation.
adlerjohn Mar 30, 2022
6721558
Fix offset calculation.
adlerjohn Mar 31, 2022
ce6759c
memcopy when growing
adlerjohn Mar 31, 2022
03e2948
Add more methods.
adlerjohn Mar 31, 2022
d62982a
Fix copy bytes calculation.
adlerjohn Mar 31, 2022
a10f4af
Update sway-lib-std/src/vec.sw
adlerjohn Mar 31, 2022
29e7053
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Mar 31, 2022
d5290f0
Add empty test.
adlerjohn Mar 31, 2022
b1b4d9b
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Apr 2, 2022
5d45f01
Add push functionality.
adlerjohn Apr 2, 2022
e9d0875
Add get operation.
adlerjohn Apr 2, 2022
a597f3d
Add test.
adlerjohn Apr 2, 2022
eee9b25
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Apr 2, 2022
c0d68ef
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Apr 4, 2022
eba59f7
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Apr 9, 2022
be37f06
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn May 13, 2022
79a0770
Fix path to stdlib.
adlerjohn May 13, 2022
d1ee993
Fix revert name.
adlerjohn May 13, 2022
906903a
Merge branch 'master' into adlerjohn/stdlib-vec
sezna May 18, 2022
cc7ed13
Merge branch 'master' into adlerjohn/stdlib-vec
mohammadfawaz Jun 16, 2022
dd30329
Minor fixes to vec + new test
mohammadfawaz Jun 16, 2022
114f942
fmt
mohammadfawaz Jun 16, 2022
27b5ddb
Remove all asm blocks
mohammadfawaz Jun 16, 2022
a1d27c5
Additional clean up
mohammadfawaz Jun 16, 2022
0c68d25
* Format the test and add JSON oracle
mohammadfawaz Jun 16, 2022
51b979d
remove unnecessary import
mohammadfawaz Jun 16, 2022
6ea4ab8
remove unnecessary file
mohammadfawaz Jun 16, 2022
c5daa7a
Update sway-lib-std/src/vec.sw
mohammadfawaz Jun 16, 2022
a9bbfa1
Update sway-lib-std/src/vec.sw
mohammadfawaz Jun 16, 2022
0ba4ae4
first wave of comments
mohammadfawaz Jun 16, 2022
a345ee1
Update test/src/e2e_vm_tests/test_programs/should_pass/stdlib/vec/src…
mohammadfawaz Jun 16, 2022
1c4971d
Update test/src/e2e_vm_tests/test_programs/should_pass/stdlib/vec/src…
mohammadfawaz Jun 16, 2022
0b58093
wave 2 of comments
mohammadfawaz Jun 16, 2022
57758b5
revert the alphbeization of ptr and cap
mohammadfawaz Jun 16, 2022
369da8d
Merge branch 'master' into adlerjohn/stdlib-vec
mohammadfawaz Jun 16, 2022
009f27d
Address more comments
mohammadfawaz Jun 16, 2022
61a9aa6
Merge branch 'master' into adlerjohn/stdlib-vec
emilyaherbert Jun 16, 2022
98fa9f6
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Jun 16, 2022
8b8279e
Merge branch 'master' into adlerjohn/stdlib-vec
adlerjohn Jun 17, 2022
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
1 change: 1 addition & 0 deletions sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ dep reentrancy;
dep vm/mod;
dep flags;
dep u128;
dep vec;

use core::*;
142 changes: 142 additions & 0 deletions sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
library vec;

use ::alloc::{alloc, realloc};
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
use ::intrinsics::size_of;
use ::mem::{read, write};
use ::option::Option;

struct RawVec<T> {
ptr: u64,
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
cap: u64,
}
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved

impl<T> RawVec<T> {
/// Create a new `RawVec` with zero capacity.
fn new() -> Self {
RawVec {
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
ptr: alloc(0),
cap: 0,
}
}

/// Creates a `RawVec` (on the heap) with exactly the capacity for a
/// `[T; capacity]`. This is equivalent to calling `RawVec::new` when
/// `capacity` is `0`.
fn with_capacity(capacity: u64) -> Self {
RawVec {
ptr: alloc(capacity * size_of::<T>()),
cap: capacity,
}
}

/// Gets the pointer of the allocation.
fn ptr(self) -> u64 {
self.ptr
}

/// Gets the capacity of the allocation.
fn capacity(self) -> u64 {
self.cap
}

/// Grow the capacity of the vector by doubling its current capacity. The
/// `realloc` function / allocates memory on the heap and copies the data
/// from the old allocation to the new allocation
fn grow(mut self) {
let new_cap = match self.cap {
0 => 1, _ => 2 * self.cap,
};

self.ptr = realloc(self.ptr, self.cap * size_of::<T>(), new_cap * size_of::<T>());
self.cap = new_cap;
}
}

/// A contiguous growable array type, written as `Vec<T>`, short for 'vector'.
pub struct Vec<T> {
buf: RawVec<T>,
len: u64,
}

impl<T> Vec<T> {
/// Constructs a new, empty `Vec<T>`.
///
/// The vector will not allocate until elements are pushed onto it.
pub fn new() -> Self {
Vec {
buf: ~RawVec::new(),
len: 0,
}
}

/// Constructs a new, empty `Vec<T>` with the specified capacity.
///
/// The vector will be able to hold exactly `capacity` elements without
/// reallocating. If `capacity` is 0, the vector will not allocate.
///
/// It is important to note that although the returned vector has the
/// *capacity* specified, the vector will have a zero *length*.
pub fn with_capacity(capacity: u64) -> Self {
Vec {
buf: ~RawVec::with_capacity(capacity),
len: 0,
}
}

/// Appends an element to the back of a collection.
pub fn push(mut self, value: T) {
// If there is insufficient capacity, grow the buffer.
if self.len == self.buf.capacity() {
self.buf.grow();
};

// Get a pointer to the end of the buffer, where the new element will
// be inserted.
let end = self.buf.ptr() + self.len * size_of::<T>();

// Write `value` at pointer `end`
write(end, value);

// Increment length.
self.len = self.len + 1;
}

/// Gets the capacity of the allocation.
pub fn capacity(self) -> u64 {
self.buf.cap
}

/// Clears the vector, removing all values.
///
/// Note that this method has no effect on the allocated capacity
/// of the vector.
pub fn clear(mut self) {
self.len = 0;
}

/// Returns a vector element at `index`, or None if `index` is out of
/// bounds.
pub fn get(self, index: u64) -> Option<T> {
// First check that index is within bounds.
if self.len <= index {
return Option::None::<T>();
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
};

// Get a pointer to the desired element using `index`
let ptr = self.buf.ptr() + index * size_of::<T>();

// Read from `ptr`
Option::Some(read(ptr))
}

/// Returns the number of elements in the vector, also referred to
/// as its 'length'.
pub fn len(self) -> u64 {
self.len
}

/// Returns `true` if the vector contains no elements.
pub fn is_empty(self) -> bool {
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
self.len == 0
}
}
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub fn run(locked: bool, filter_regex: Option<regex::Regex>) {
),
("should_pass/stdlib/b512_test", ProgramState::Return(1)), // true
("should_pass/stdlib/block_height", ProgramState::Return(1)), // true
("should_pass/stdlib/vec", ProgramState::Return(1)), // true
(
"should_pass/language/trait_override_bug",
ProgramState::Return(7),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-E8B33641BFB485A7'
dependencies = []

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

[[package]]
name = 'vec'
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"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
Loading