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

Open
wants to merge 2 commits 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.

@owenhilyard owenhilyard force-pushed the linkedlist branch 2 times, most recently from 9743f28 to e46ebca Compare January 13, 2025 17:30
@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.

@lattner
Copy link
Collaborator

lattner commented Jan 20, 2025

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.

That sounds super weird. I'm not sure what is going on, but if you could reduce this down to a small testcase, I would be happy to take a look at some point.

Merges my earlier work with the one which appeared in the stdlib,
expanding the API and test coverage.

Also makes a few of the test_utils counters `Writable` to help them
conform to more APIs.

Signed-off-by: Owen Hilyard <[email protected]>
@owenhilyard owenhilyard marked this pull request as ready for review January 21, 2025 03:38
@owenhilyard
Copy link
Contributor Author

@lattner not sure if you want one without the attached data structure, but if I replace stdlib/test/collections/linked_list.mojo:464 with list[0][1].value = "Mojo", I get error: invalid call to '__setitem__': invalid use of mutating method on rvalue of type 'LinkedList[CopyCountedStruct]'. There isn't a ton of code under that that isn't normal linked list things, but if that's not minimal enough I can try to reduce it more.

@owenhilyard
Copy link
Contributor Author

cc @abduld as the original author.

@JoeLoser
Copy link
Collaborator

@lsh I can't seem to assign you to this PR after the GitHub organization move (think there's something up here, FYI @dlumma). In any case, do you mind taking a look this? Thanks!

Copy link
Collaborator

@JoeLoser JoeLoser left a comment

Choose a reason for hiding this comment

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

Just did a quick pass of this, looking good! Left some comments and @lsh will also take a look.

Thanks for working on improving this a ton from the basic implementation that was started prior to your work! Sorry that we already had a PR in-flight starting the linked list type.

stdlib/test/collections/test_linked_list.mojo Outdated Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Outdated Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Outdated Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Outdated Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Outdated Show resolved Hide resolved
stdlib/src/collections/linked_list.mojo Outdated Show resolved Hide resolved
stdlib/test/collections/test_linked_list.mojo Outdated Show resolved Hide resolved
stdlib/test/collections/test_linked_list.mojo Outdated Show resolved Hide resolved
stdlib/test/collections/test_linked_list.mojo Outdated Show resolved Hide resolved
@owenhilyard
Copy link
Contributor Author

@JoeLoser

Sorry that we already had a PR in-flight starting the linked list type.

That's fine, I think this shows a need for external stdlib contributors to be able to do some kind of work tracking and for there to be some visibility into non-sensitive work done internally before it hits nightly. Even on the level of a discord channel to toss "I'm working on a linked list" messages into.

Minor tweaks, mostly consisting of removing unnecessary things like some
leftover debug prints and explicit type parameters. Also adds time
complexity information to all public methods of `LinkedList`.

Signed-off-by: Owen Hilyard <[email protected]>
@owenhilyard owenhilyard changed the title [stdlib] Add Linked List [stdlib] Enhance Linked List Jan 23, 2025
@owenhilyard
Copy link
Contributor Author

I'm not going to have time to finish up the last few bits of work and deal with the merge conflict today, and potentially not tomorrow.

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.

6 participants