Skip to content

Instantly share code, notes, and snippets.

@CodaFi
Created May 25, 2013 22:03
Show Gist options
  • Save CodaFi/5650930 to your computer and use it in GitHub Desktop.
Save CodaFi/5650930 to your computer and use it in GitHub Desktop.
void DMIndexSetDivide(NSArray **slices, NSIndexSet *inIndexSet, NSUInteger parts) {
if (parts == 0 || parts == 1) {
*slices = [NSArray arrayWithObject:inIndexSet];
return;
}
NSMutableArray *indexSets = [NSMutableArray arrayWithCapacity:parts];
for (int i = 0; i < parts; i++) {
indexSets[i] = [NSMutableIndexSet indexSet];
}
NSUInteger divisor = roundtol(inIndexSet.count / parts);
__block NSUInteger idx = 0;
__block NSUInteger indexSetIdx = 0;
__block NSMutableIndexSet *currentIndexSet = nil;
[inIndexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
if (idx % divisor == 0) {
currentIndexSet = indexSets[indexSetIdx];
if (indexSetIdx + 1 < indexSets.count)
indexSetIdx++;
}
[currentIndexSet addIndex:index];
idx++;
}];
*slices = indexSets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment