Skip to content

std: introduce orderedRemoveMany #23920

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

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

std: introduce orderedRemoveMany #23920

wants to merge 1 commit into from

Conversation

andrewrk
Copy link
Member

This algorithm is non-trivial and makes sense for any data structure that acts as an array list, so I thought it would make sense as a method.

I have a real world case for this in a music player application (deleting queue items).

Adds the method to:

  • ArrayList
  • ArrayHashMap
  • MultiArrayList

@jacobly0
Copy link
Member

jacobly0 commented May 19, 2025

Idea for a better array list impl:

        pub fn orderedRemoveMany(self: *Self, sorted_indexes: []const usize) void {
            if (sorted_indexes.len == 0) return;
            var shift: usize = 1;
            for (sorted_indexes[0..sorted_indexes.len - 1], sorted_indexes[1..]) |removed, end| {
                if (removed == end) continue; // allows duplicates in `sorted_indexes`
                const start = removed + 1;
                const len = end - start; // safety checks `sorted_indexes` are sorted
                @memmove(self.items[start - shift ..][0..len], self.items[start ..][0..len]); // safety checks inital `sorted_indexes` are in range
                shift += 1;
            }
            const start = sorted_indexes[sorted_indexes.len - 1] + 1;
            const end = self.items.len;
            const len = end - start; // safety checks final `sorted_indexes` are in range
            @memmove(self.items[start - shift ..][0..len], self.items[start..][0..len]);
            self.items.len = end - shift;
        }

@andrewrk
Copy link
Member Author

andrewrk commented May 19, 2025

Nice! You didn't implement @memmove in the x86 backend when I wasn't looking, did you?

Ah, it's only not implemented for COFF.

Also noticed CI has no std lib test coverage for x86 backend on Windows, so I don't even need to disable the new tests here.

This algorithm is non-trivial and makes sense for any data structure
that acts as an array list, so I thought it would make sense as a
method.

I have a real world case for this in a music player application
(deleting queue items).

Adds the method to:
* ArrayList
* ArrayHashMap
* MultiArrayList
@andrewrk andrewrk force-pushed the orderedRemoveMany branch from 7d720f6 to ef29861 Compare May 19, 2025 02:38
@andrewrk
Copy link
Member Author

I'm not in any rush to merge this, I'll let it sit until wrangle-writer-buffering branch is complete, and include a zig1.wasm update there, and update to use @memmove there.

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