Skip to content

Instantly share code, notes, and snippets.

@reportbase
Created March 22, 2015 21:28
Show Gist options
  • Save reportbase/e5266ac5d5d3dc8e65b6 to your computer and use it in GitHub Desktop.
Save reportbase/e5266ac5d5d3dc8e65b6 to your computer and use it in GitHub Desktop.
binary split
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