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] Add Linked List #3942

Draft
wants to merge 1 commit into
base: nightly
Choose a base branch
from

Conversation

owenhilyard
Copy link
Contributor

Adds a LinkedList type which implements most of the API surface of List (also grabbing the tests). This should help with filling in basic data structures in the stdlib.

No iterator because I ran into some nasty issues with origins and I'm not sure how far I can push casting them before I get to UB.

There's also an instance of list.__getitem__(0).__getitem__(1).value = "Mojo" in test_linked_list.mojo since I couldn't find another way to avoid the copy. If someone has a solution please let me know.

@owenhilyard owenhilyard requested a review from a team as a code owner January 13, 2025 04:20
# c = c[].next
return current

fn __getitem__[I: Indexer](ref self, read idx: I) raises -> ref [self] T:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was decided a while ago to just use Int here and let the implicit conversion do the work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following what List does. If we want to establish that as a convention, I think that it probably should go in the stdlib docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see now it was recently changed back to this way. That's fine I preferred it anyways

Returns:
Whether the list is empty or not.
"""
return self._head == Self.PointerT()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use return not self._head instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had "check head for null" in my head when writing this. I'll remove the comparison.

Adds a `LinkedList` type which implements most of the API surface of
`List` (also grabbing the tests). This should help with filling in basic
data structures in the stdlib.

Signed-off-by: Owen Hilyard <[email protected]>
@owenhilyard owenhilyard marked this pull request as draft January 16, 2025 21:26
@owenhilyard
Copy link
Contributor Author

Move to draft due to existing linked list. I'll circle around after I've merged the two implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants