Dividing an array into subgroups?
Dividing an array into subgroups?
- Subject: Dividing an array into subgroups?
- From: Andrew Merenbach <email@hidden>
- Date: Mon, 16 Jun 2003 20:09:25 -0700
I would like to divide an array into subarrays of a specified size, and
to assign the remainder to their own array. I have produced the
following code (which could quite probably use some major optimisation
or rewriting):
@implementation NSArray (TestCategory)
- (NSArray *)partition:(unsigned)groupSize {
NSMutableArray *output = [NSMutableArray array];
unsigned start, length, ct = [self count];
for (start = 0; start < ct; start += groupSize) {
length = (start + groupSize < ct) ? groupSize : ct - start;
[output addObject:[self subarrayWithRange:NSMakeRange(start,
length)]];
}
return (NSArray *) output;
}
I'm uncertain as to whether it would actually be a good idea, but might
there exist a tail-recursive partitioning algorithm that would work
better than the above code? Could there be any advantage to it, such
as speed or code size? What might I put into it?
Take care,
Andrew
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.