What about something like:

@implementation NSSet (Random)

- (id) randomObject {
  NSArray * allObjects = [self allObjects];
  if ([allObjects count] == 0) { @throw ...; }
  return [allObjects objectAtIndex:(arc4random() % [allObjects count])];
}

@end

On May 25, 2010, at 10:49 AM, Michael A. Crawford wrote:

> I'd like to extend Apple implementation of NSMutableSet to include a 
> randomObject method.  The purpose of this method is self-explanatory and its 
> potential use is I'm sure obvious to most of you.
> 
> For my first idea I thought about simply adding a category to NSMutableSet 
> but any obvious implementation of -randomObject would require accessing 
> existing instance variables or adding my own in order to implement the random 
> access I would need to for the set.  I also don't know how objects are stored 
> internally.  Is it an array?  Is it a key/value pair? I don't know.  How am I 
> going to correlate the random numbers I generate with the objects in the set?
> 
> Next, I thought about using inheritance with NSMutableArray.  I even wrote 
> some code:
> 
> + (void)initialize
> {
>    if ( self == [MCRandomlyAccessedArray class] )
>    {
>        srandom(time(NULL));
>    }
> }
> 
> - (id)randomObject
> {
>    // If the array is empty, throw an exception.  The caller should know 
> better.
>    // If there is only one, return object zero every time.
>    // If there are only two, alternate with every access.
>    // If there are three or more, randomize but never provide the same object
>    // twice in a row.
>    if ( [self count] )
>    {
>        NSUInteger index = 0;
> 
>        if ( [self count] > 1 )
>        {
>            if ( 2 == [self count] )
>            {
>                index = (++lastObjectIndex % 2);
>            }
>            else
>            {
>                do
>                {
>                    index = (random() % [self count]);
>                } while (index == lastObjectIndex);
>            }
>            lastObjectIndex = index;
>        }
>        return [self objectAtIndex:index];
>    }
>    else
>    {
>        [[NSException exceptionWithName:NSRangeException
>                                 reason:@"Array is empty"
>                               userInfo:nil] raise];
>    }
>    return nil; // unreachable
> }
> 
> Come to find out at runtime that this won't work either because 
> NSMutableArray is abstract!
> 
> *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
> reason: '*** -[NSMutableArray addObject:]: method only defined for abstract 
> class.  Define -[MCRandomlyAccessedArray addObject:]!'
> 
> This would seem to indicate that I need to write a lot more code in order to 
> have a complete implementation.  I'm not in the mood to re-invent the wheel.  
> I'm not lazy, I just have a lot of other code that needs to be written, which 
> pertains to the big picture.
> 
> Next, I thought maybe I could simply wrap-up an NSMutableArray in my own 
> class and let it do most of the heavy lifting when it comes to all of those 
> existing methods.  The only problem is that I'm no expert on how to duplicate 
> all of the class and instance init methods and memory management code.
> 
> So, that brings me to you lovely people.  What do you recommend?  Do I bite 
> the bullet and re-implement NSMutableArray?  If so, where can I find guidance 
> on this?  I love the containers provided by Apple.  They simply work and I 
> don't have to think about them.  So much so, that I don't really know how 
> they work.  My expertise is/was in C/C++ and STL.
> 
> Is there fourth alternative or something I missed in my brief examination as 
> described above?
> 
> Thanks.
> 
> -Michael
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/davedelong%40me.com
> 
> This email sent to davedel...@me.com

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to