Re: NSQueue equivalent?
Re: NSQueue equivalent?
- Subject: Re: NSQueue equivalent?
- From: John Hörnkvist <email@hidden>
- Date: Tue, 19 Feb 2002 20:53:36 +0100
On Tuesday, February 19, 2002, at 07:33 PM, Steve Mykytyn wrote:
It would be helpful to have a class like "NSQueue" that allowed one to
put in objects and take them out FIFO or LIFO. A search of the
documentation didn't reveal any obvious ways to accomplish this. Any
suggestions? Won't be that hard to write one, but would rather not
reinvent the wheel...
Something like this:
@interface Queue:NSObject
{
NSMutableArray* objects;
}
- (void)addObject:(id)object;
- (id)takeObject;
@end
@implementation Queue
- (id)init
{
if (self=[super init])
{
objects=[[NSMutableArray alloc] init];
}
return self;
}
- (void)addObject:(id)object
{
[objects addObject:object];
}
- (id)takeObject
{
id object=nil;
if ([objects count])
{
object=[objects objectAtIndex:0];
[object retain];
[objects removeObjectAtIndex:0];
}
return [object autorelease];
}
@end
Another option is to simply make it a category on NSMutableArray:
@interface NSMutableArray(Queue)
- (id)takeObject;
@end
@implementation NSMutableArray(Queue)
- (id)takeObject
{
id object=nil;
if ([self count])
{
object=[self objectAtIndex:0];
[object retain];
[self removeObjectAtIndex:0];
}
return [object autorelease];
}
@end
The implementation of NSMutableArray should make this access pattern
quite efficient.
Regards,
John Hornkvist
--
ToastedMarshmallow, the perfect Cocoa companion
http://www.toastedmarshmallow.com
_______________________________________________
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.