-
Notifications
You must be signed in to change notification settings - Fork 28
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
Sorting methods for Slice variant #49
Comments
I agree that sorting is a worth adding to the generated code! However I would prefer doing something which sorts based on all fields by default, so that sorting We might be able to use the permutation crate here (although adding external dependencies in a proc-macro is tricky. The crate should be added as a dependency of I think we should be able generate code that looks like this, making sure fn sort(&mut self) {
use soa_derive::Permutation;
let mut permutation: Vec<usize> = (0..self.len()).collect();
permutation.sort_by_key(|i| self.index(i));
let permutation = Permutation::oneline(permutation);
permutation.apply_slice_in_place(&mut self.bar);
permutation.apply_slice_in_place(&mut self.baz);
} |
After some experimentation, I found that HRTB works perfectly for this (code includes minor corrections): impl<'a> FooSliceMut<'a>
where
for<'b> FooRef<'b>: Ord,
{
fn sort(&mut self) {
use soa_derive::Permutation;
let mut permutation: Vec<usize> = (0..self.len()).collect();
// the trait `SoAIndex<FooSlice<'_>>` is not implemented for `&usize`,
// but it is implemented for `usize`, so we dereference `i`
permutation.sort_by_key(|i| self.index(*i));
// We need to invert the permutation in order to sort the slices correctly
let mut permutation = Permutation::oneline(permutation).inverse();
permutation.apply_slice_in_place(&mut self.bar);
permutation.apply_slice_in_place(&mut self.baz);
}
// ... Other sorting methods ...
} Since I will try to convert it to macro code, and create a PR. |
Some useful methods I miss from Array Of Structs are the sorting methods, which are
sort()
,sort_by()
andsort_by_key()
for theslice
primitive type. From what I've seen, the Slice variant produced by the macro does not offer any sorting methods.I believe it is possible to implement sorting using the
permutation
crate, and adding a#[sort]
attribute. For example, thesort()
method would be like this (Example works if#[sort]
is removed/commented out):I don't have any experience with making macros, but I can try implementing it. I'm open to any other ideas or suggestions about the implementation.
The alternative to pulling in a whole crate for sorting is to zip the vectors from all fields, sort them, and then split them back, which is very costly due to multiple allocations. A middle-ground solution would be to provide sorting under a feature (eg.
sort
) to avoid increasing default dependencies.Limitations:
#[sort(...)]
syntax for naming)as_mut_slice()
method first, as described in the project's caveatsThe text was updated successfully, but these errors were encountered: