• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Dividing an array into subgroups?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Dividing an array into subgroups?
      • From: Michael Tsai <email@hidden>
  • Prev by Date: Re: array performance
  • Next by Date: Re: How to warn users in the installer that 10.2 is required?
  • Previous by thread: Re: Cocoa Java Delegate Name
  • Next by thread: Re: Dividing an array into subgroups?
  • Index(es):
    • Date
    • Thread