Skip to content

Commit

Permalink
fix details
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 4, 2024
1 parent 6c178fb commit 8e88d34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
55 changes: 20 additions & 35 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -468,31 +468,32 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
# Methods
# ===-------------------------------------------------------------------===#

# FIXME: this needs to be consistent with {String, StringSlice}.byte_length()
fn bytecount(self) -> Int:
"""Gets the bytecount of the List.
fn byte_length(self) -> Int:
"""Gets the byte length of the List.
Returns:
The bytecount of the List.
The byte length of the List.
"""
return len(self) * sizeof[T]()

fn _realloc(mut self, new_capacity: Int):
var new_data = UnsafePointer[T].alloc(new_capacity)

_move_pointee_into_many_elements[hint_trivial_type](
dest=new_data,
src=self.data,
size=self._len,
)
@parameter
if hint_trivial_type:
memcpy(new_data, self.data, len(self))
else:
for i in range(len(self)):
(self.data + i).move_pointee_into(new_data + i)

if self.data:
self.data.free()
self.data = new_data
self.capacity = new_capacity

fn append(mut self, owned value: T):
"""Appends a value to this list.
"""Appends a value to this list. If there is no capacity left, resizes
to twice the current capacity. Except for 0 capacity where it sets 1.
Args:
value: The value to append.
Expand All @@ -505,23 +506,24 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: SIMD[D, _]):
"""Appends a vector to this list.
"""Appends a vector to this list. If there is no capacity left, resizes
to `len(self) + value.size`.
Parameters:
D: The DType.
Args:
value: The value to append.
"""
if len(self) + value.size > self.capacity:
self._realloc(self.capacity + value.size)
self.reserve(len(self) + value.size)
(self.data + len(self)).store(value)
self._len += value.size

fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: SIMD[D, _], count: Int):
"""Appends count items from a vector to this list.
"""Appends a vector to this list. If there is no capacity left, resizes
to `len(self) + count`.
Parameters:
D: The DType.
Expand All @@ -530,26 +532,24 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
value: The value to append.
count: The ammount of items to append.
"""

if len(self) + count > self.capacity:
self._realloc(self.capacity + count)
self.reserve(len(self) + count)
var v_ptr = UnsafePointer.address_of(value).bitcast[Scalar[D]]()
memcpy(self.data + len(self), v_ptr, count)
self._len += count

fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: Span[Scalar[D]]):
"""Appends a Span to this list.
"""Appends a Span to this list. If there is no capacity left, resizes
to `len(self) + len(value)`.
Parameters:
D: The DType.
Args:
value: The value to append.
"""
if len(self) + len(value) > self.capacity:
self._realloc(self.capacity + len(value))
self.reserve(len(self) + len(value))
memcpy(self.data + len(self), value.unsafe_ptr(), len(value))
self._len += len(value)

Expand Down Expand Up @@ -986,18 +986,3 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](

fn _clip(value: Int, start: Int, end: Int) -> Int:
return max(start, min(value, end))


fn _move_pointee_into_many_elements[
T: CollectionElement, //, hint_trivial_type: Bool
](dest: UnsafePointer[T], src: UnsafePointer[T], size: Int):
@parameter
if hint_trivial_type:
memcpy(
dest=dest.bitcast[Int8](),
src=src.bitcast[Int8](),
count=size * sizeof[T](),
)
else:
for i in range(size):
(src + i).move_pointee_into(dest + i)
3 changes: 1 addition & 2 deletions stdlib/src/memory/memory.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,10 @@ fn memcpy[
src: The source pointer.
count: The number of elements to copy.
"""
var n = count * sizeof[dest.type]()
_memcpy_impl(
dest.bitcast[Byte, origin=MutableAnyOrigin](),
src.bitcast[Byte, origin=MutableAnyOrigin](),
n,
count * sizeof[T](),
)


Expand Down
2 changes: 1 addition & 1 deletion stdlib/test/collections/test_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_list():
list.append(i)

assert_equal(5, len(list))
assert_equal(5 * sizeof[Int](), list.bytecount())
assert_equal(5 * sizeof[Int](), list.byte_length())
assert_equal(0, list[0])
assert_equal(1, list[1])
assert_equal(2, list[2])
Expand Down

0 comments on commit 8e88d34

Please sign in to comment.