Skip to content

Commit

Permalink
inline unnecessary helper functions for List
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 10, 2024
1 parent 2ace785 commit 1401fe4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
40 changes: 11 additions & 29 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,23 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
# Methods
# ===-------------------------------------------------------------------===#

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.size,
)
@parameter
if hint_trivial_type:
memcpy(new_data, self.data, self.byte_length())
else:
for i in range(size):
(src + i).move_pointee_into(dest + i)

if self.data:
self.data.free()
Expand Down Expand Up @@ -737,8 +738,8 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
if stop_normalized < 0:
stop_normalized += len(self)

start_normalized = _clip(start_normalized, 0, len(self))
stop_normalized = _clip(stop_normalized, 0, len(self))
start_normalized = max(start_normalized, min(0, len(self)))
stop_normalized = max(stop_normalized, min(0, len(self)))

for i in range(start_normalized, stop_normalized):
if self[i] == value:
Expand Down Expand Up @@ -932,22 +933,3 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
The UnsafePointer to the underlying memory.
"""
return self.data


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)
2 changes: 1 addition & 1 deletion stdlib/test/collections/test_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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
3 changes: 2 additions & 1 deletion stdlib/test/python/my_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __init__(self, bar):

class AbstractPerson(ABC):
@abstractmethod
def method(self): ...
def method(self):
...


def my_function(name):
Expand Down

0 comments on commit 1401fe4

Please sign in to comment.