Skip to content

Commit

Permalink
Consistently use Indexer in LinkedList`
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Grenier <[email protected]>
  • Loading branch information
bgreni committed Feb 11, 2025
1 parent c92c135 commit a0e910a
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions stdlib/src/collections/linked_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,25 @@ struct LinkedList[
curr = curr[].next
return new^

fn insert(mut self, owned idx: Int, owned elem: ElementType) raises:
fn insert[
I: Indexer
](mut self, owned 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 +483,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 +615,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 +626,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 +650,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 +668,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 a0e910a

Please sign in to comment.