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 three __delitem__ method overloads for List #2704

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

Conversation

rd4com
Copy link

@rd4com rd4com commented May 17, 2024

This PR includes three __delitem__ method overloads for List:

  • fn __delitem__(inout self, position: Int)
  • fn __delitem__(inout self, *positions: Int)
  • fn __delitem__(inout self, owned positions: List[Int])

The implementation ensure that an index will not be deleted twice,
even if provided multiple times.
The order of removal is the reverse of sorted indexes.

 

Example:

  x = List[Int](0,10,20,30,40)
  x.__delitem__(0,4)
  for i in x: print(i[]) #10,20,30

 

@rparolin proposed using __delitem__ directly instead of del_batch_of_indexes

Suggestion for the future:

del my_list[0,1,4,5]

@rd4com rd4com requested review from a team as code owners May 17, 2024 11:34

```mojo
x = List[Int](0,10,20,30,40)
x.delete_batch_of_indexes(0,4)
Copy link
Collaborator

@rparolin rparolin May 27, 2024

Choose a reason for hiding this comment

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

Since we lack the language feature to remove subranges via the del keyword, I propose we implement the __delitem__ member function and call it directly until we land the language feature enabling the use of del. @JoeLoser Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

Hello,

new commit changed the method name to __delitem__ and includes an overload that takes only one position as an integer.

That way, it won't create a set and sort an array if there is only one index. ( _ = self.pop(position))

  • Should there be any debug_asserts that asserts that len(self) > index and index >= 0 ?

  • Should the methods just don't do self.pop(position) if position is out of bound ?

@rd4com rd4com force-pushed the list_del_batch_of_indexes branch from 2fc905f to ff9a5a9 Compare May 29, 2024 22:06
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
@rd4com rd4com force-pushed the list_del_batch_of_indexes branch from ff9a5a9 to c2e36e7 Compare May 30, 2024 10:01
@rd4com rd4com changed the title [stdlib] List.delete_batch_of_indexes [stdlib] Add three __delitem__ method overloads for List May 30, 2024
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.

None yet

2 participants