Skip to content

Commit

Permalink
[External] [stdlib] Consistently use Indexer in LinkedList (#55875)
Browse files Browse the repository at this point in the history
[External] [stdlib] Consistently use `Indexer` in `LinkedList`

Use `Indexer` in all appropriate locations in `LinkedList` for
consistency.

Co-authored-by: bgreni <42788181+bgreni@users.noreply.github.com>
Closes #3990
MODULAR_ORIG_COMMIT_REV_ID: 414230e1163eab1e961bece26ab395bef8b3904c
  • Loading branch information
bgreni authored and modularbot committed Feb 15, 2025
1 parent 3c949d8 commit ddddf01
Showing 1 changed file with 21 additions and 7 deletions.
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))

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

0 comments on commit ddddf01

Please sign in to comment.