Created
September 27, 2017 16:10
-
-
Save henninglive/cdf236b4bf4cb2839557e96f3c3869a2 to your computer and use it in GitHub Desktop.
All possible ways to slice a slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn all_splits<T>(s: &[T]) -> Vec<Vec<&[T]>> { | |
| let len = s.len(); | |
| let mut vv = Vec::with_capacity(1<<(len - 1)); | |
| for mask in 0usize..(1<<(len - 1)) { | |
| let mut v = Vec::with_capacity(mask.count_ones() as usize + 1); | |
| let mut prev = 0; | |
| for i in 1..len { | |
| if (mask >> (i - 1)) & 0b1 != 0 { | |
| v.push(&s[prev..i]); | |
| prev = i; | |
| } | |
| } | |
| v.push(&s[prev..len]); | |
| vv.push(v); | |
| } | |
| vv | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment