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

[Feature Request] Add a deque struct to the stdlib #2659

Open
1 task done
gabrieldemarmiesse opened this issue May 15, 2024 · 5 comments
Open
1 task done

[Feature Request] Add a deque struct to the stdlib #2659

gabrieldemarmiesse opened this issue May 15, 2024 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed mojo-repo Tag all issues with this label

Comments

@gabrieldemarmiesse
Copy link
Contributor

gabrieldemarmiesse commented May 15, 2024

Review Mojo's priorities

What is your request?

Add a Deque struct to the stdlib, similar to python's deque: https://docs.python.org/3/library/collections.html#collections.deque

What is your motivation for this change?

While it's a very useful collection to have in general, in the context of the standard library it offers some new possibilities.
There are multiple places in the stdlib where we want to "consume" objects in a list in order. Doing so currently means either

  1. calling List.pop(0), while safe, this is very inneficient as all elements must be moved at every pop()
  2. Using a pointer and move_pointee. While this is efficient, this is quite unsafe.

Moving a list pointer into a deque would allow us to call deque.popleft() at very little cost. Thus consuming the elements in order.

Any other details?

The two places where we currently have this issue in the stdlib are List.extends() and Dict.fromkeys()

@gabrieldemarmiesse gabrieldemarmiesse added enhancement New feature or request mojo-repo Tag all issues with this label labels May 15, 2024
@bgreni
Copy link
Contributor

bgreni commented May 15, 2024

@gabrieldemarmiesse I was thinking about this a couple days ago so I'd like to take a crack at it if you haven't already started

@gabrieldemarmiesse
Copy link
Contributor Author

Let's wait for the go of the maintainers before working on a new struct.

@gryznar
Copy link
Contributor

gryznar commented May 16, 2024

To be aligned with current naming convention, it should be Deque, not deque ;)

@dimitrilw
Copy link

If you want a space to make a Deque struct before core Mojo maintainers give the green-light for std-lib, you are more than welcome to submit PRs to toybox. Honestly, I'm disappointed with myself for overlooking putting deque, ring-queue, and the like to my "hit list" of desired data structures.

@JoeLoser JoeLoser added the help wanted Extra attention is needed label May 31, 2024 — with Linear
Copy link
Collaborator

I'm +1 for a deque collection. Do you want to write up a one pager on an API design proposal for consideration and we can take a look to discuss before starting down an implementation? You may want to check out https://doc.rust-lang.org/std/collections/struct.VecDeque.html as something to consider too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

5 participants