Is there a Queue/FIFO data structure for the iPhone?
data-structures, iphone, queue
Solution
Implementing a queue based on `NSMutableArray` is pretty easy, it's probably under 50 lines of code.
EDIT:
Found this with a quick google search:
@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)dealloc {
[objects release];
[super dealloc];
}
- (void)addObject:(id)object {
[objects addObject:object];
}
- (id)takeObject {
id object = nil;
if ([objects count] > 0) {
object = [[[objects objectAtIndex:0] retain] autorelease];
[objects removeObjectAtIndex:0];
}
return object;
}
@end
Problem
Before I roll my own Queue using `NSMutableArray`, I'd like to know if there is something more standard available. I don't see anything in the Apple docs, but I'll be surprised if there is not a Queue implementation from somewhere that folks are using. Java spoils me!