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] Consistently use Indexer in LinkedList #3990

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
28 changes: 21 additions & 7 deletions stdlib/src/collections/linked_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,23 @@ struct LinkedList[
curr = curr[].next
return new^

fn insert(mut self, owned idx: Int, owned elem: ElementType) raises:
fn insert[I: Indexer](mut self, idx: I, owned elem: ElementType) raises:
"""
Insert an element `elem` into the list at index `idx`.

Time Complexity: O(1)

Parameters:
I: The type of index to use.

Raises:
When given an out of bounds index.

Args:
idx: The index to insert `elem` at. `-len(self) <= idx <= len(self)`.
elem: The item to insert into the list.
"""
var i = max(0, index(idx) if idx >= 0 else index(idx) + len(self))
var i = max(0, index(idx) if Int(idx) >= 0 else index(idx) + len(self))
bgreni marked this conversation as resolved.
Show resolved Hide resolved

if i == 0:
var node = Self._NodePointer.alloc(1)
Expand Down Expand Up @@ -478,7 +481,7 @@ struct LinkedList[
self._head = node
self._size += 1
else:
raise String("Index {} out of bounds").format(idx)
raise String("Index {} out of bounds").format(i)

fn extend(mut self, owned other: Self):
"""
Expand Down Expand Up @@ -610,7 +613,9 @@ struct LinkedList[
"""
return not (self == other)

fn _get_node_ptr(ref self, index: Int) -> UnsafePointer[Node[ElementType]]:
fn _get_node_ptr[
I: Indexer
](ref self, index: I) -> UnsafePointer[Node[ElementType]]:
"""
Get a pointer to the node at the specified index.

Expand All @@ -619,14 +624,17 @@ struct LinkedList[

Time Complexity: O(n) in len(self)

Parameters:
I: The type of index to use.

Args:
index: The index of the node to get.

Returns:
A pointer to the node at the specified index.
"""
var l = len(self)
var i = normalize_index[container_name="LinkedList"](index, self)
var i = normalize_index[container_name="LinkedList"](Int(index), self)
debug_assert(0 <= i < l, "index out of bounds")
var mid = l // 2
if i <= mid:
Expand All @@ -640,12 +648,15 @@ struct LinkedList[
curr = curr[].prev
return curr

fn __getitem__(ref self, index: Int) -> ref [self] ElementType:
fn __getitem__[I: Indexer](ref self, index: I) -> ref [self] ElementType:
"""
Get the element at the specified index.

Time Complexity: O(n) in len(self)

Parameters:
I: The type of index to use.

Args:
index: The index of the element to get.

Expand All @@ -655,12 +666,15 @@ struct LinkedList[
debug_assert(len(self) > 0, "unable to get item from empty list")
return self._get_node_ptr(index)[].value

fn __setitem__(mut self, index: Int, owned value: ElementType):
fn __setitem__[I: Indexer](mut self, index: I, owned value: ElementType):
"""
Set the element at the specified index.

Time Complexity: O(n) in len(self)

Parameters:
I: The type of index to use.

Args:
index: The index of the element to set.
value: The new value to set.
Expand Down