Last active
December 15, 2015 04:19
-
-
Save CodaFi/5201011 to your computer and use it in GitHub Desktop.
Divide an array of Obj-C objects into an arbitrary amount of smaller arrays.
This file contains 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
void DMArrayDivide(NSArray **slices, NSArray *inArray, NSUInteger parts) { | |
if (parts == 0 || parts == 1) { | |
*slices = inArray; | |
return; | |
} | |
NSMutableArray *arrays = [NSMutableArray arrayWithCapacity:parts]; | |
for (int i = 0; i < parts; i++) { | |
arrays[i] = @[].mutableCopy; | |
} | |
long int idx = 0; | |
for (id object in inArray) { | |
[arrays[idx] addObject:object]; | |
idx = (idx + 1) % parts; | |
} | |
*slices = arrays; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment