GithubHelp home page GithubHelp logo

Comments (2)

Luthaf avatar Luthaf commented on August 31, 2024

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 XXXStructVec produces the same output as sorting a Vec<XXXStruct>. Then sorting based on a single field can be implemented through sort_by and sort_by_key methods.

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 soa-derive, and soa-derive-internal should generate calls to soa_derive::permutations).

I think we should be able generate code that looks like this, making sure StructRef implements PartialOrd

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);
}

from soa-derive.

Christopher-S-25 avatar Christopher-S-25 commented on August 31, 2024

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 sort_by_key() requires FooRef to implement Ord, it should be easy enough to obtain the sorting methods by using this attribute: #[soa_attr(Ref, derive(Ord, PartialOrd, Eq, PartialEq))], or by manually implementing (some of) the traits for more complex cases.

I will try to convert it to macro code, and create a PR.

from soa-derive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.