Created
March 22, 2015 21:28
-
-
Save reportbase/e5266ac5d5d3dc8e65b6 to your computer and use it in GitHub Desktop.
binary split
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
inline void binary_split(int* arr, int size, int split) | |
{ | |
if(size <= split) | |
return; | |
arr[size - 1] = true; | |
int n = (int)ceil(size / 2.0); | |
binary_split(arr, n, split); | |
binary_split(arr + n, size - n, split); | |
} | |
inline int* get_binary_split(int size, int split, int* out = NULL) | |
{ | |
int* arr = (int*) calloc(sizeof(int), size); | |
arr[0] = 1; | |
binary_split(arr, size, split); | |
int* c = (int*) malloc(sizeof(int) * size); | |
int* pc = c; | |
for(int n = 0; n < size; ++n) | |
if(arr[n]) | |
*pc++ = n; | |
if(out) | |
*out = pc - c; | |
free(arr); | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment