Skip to content

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.

@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.

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 enabled auto-merge (rebase) August 11, 2025 20:25
@andrewrk
Copy link
Member Author

tested locally with stage3/bin/zig build test -Dskip-release -Dskip-non-native

@andrewrk andrewrk disabled auto-merge August 11, 2025 20:32
@andrewrk andrewrk merged commit 59de7e3 into master Aug 11, 2025
4 of 14 checks passed
@andrewrk andrewrk deleted the orderedRemoveMany branch August 11, 2025 20:32
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