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

Investigate more efficient length function for UnorderedCartPowerRange in mach.range.cartpower #16

Open
pineapplemachine opened this issue Mar 3, 2017 · 0 comments

Comments

@pineapplemachine
Copy link
Owner

The current implementation relies on this recursive function; however it seems likely that it should be possible to compute without recursion, or at least with less of it:

static private size_t unorderedlength(size_t size)(in size_t ilen){
    static if(size == 0){
        return ilen > 0 ? 1 : 0;
    }else static if(size == 1){
        return ilen;
    }else static if(size == 2){
        // Same as `ilen + unorderedlength!size(ilen - 1)`
        // And as `ilen * (ilen + 1) / 2`, but without overflow
        immutable x = ilen + 1;
        return (ilen / 2) * x + ((ilen & 1) * x) / 2;
    }else{
        if(ilen <= 1){
            return ilen;
        }else{
            return unorderedlength!(size)(ilen - 1) + unorderedlength!(size - 1)(ilen);
        }
    }
}
@pineapplemachine pineapplemachine changed the title Investigate more efficient length function for UnorderedCartPowerRange in mach.range.cartpower Investigate more efficient length function for UnorderedCartPowerRange in mach.range.cartpower Mar 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant