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!
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).
Queues are a data structure in which you can insert new data from one end and delete data from the other end. That means it works like FIFO – First In First Out. Enqueue operation is used to insert an element at the beginning.
Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list.
The primary difference between Stack and Queue Data Structures is that Stack follows LIFO while Queue follows FIFO data structure type. LIFO refers to Last In First Out. It means that when we put data in a Stack, it processes the last entry first.
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
Cocoa itself doesn't have a Queue class, and there's not a standard per se, but there are several options, one of which may best fit your needs. See this question (and my answer).
Like you said, you can roll your own using NSMutableArray. If you just need a quick'n'dirty queue (and aren't worried about copying, encoding/decoding, enumeration, etc.) then the solution @Matt suggests is an easy approach. You should also consider adding queue methods to NSMutableArray
via a category, which is nice in that your "queue" is also an array (so you can pass it for NSArray parameters), and you get all the NS(Mutable)Array functionality for free.
If performance is important, I recommend using a structure more ideally suited for removing the first element. I wrote CHCircularBufferQueue for my own framework for this very reason. (Not trying to toot my own horn, just trying to save others some time.)
I have made a category containing just the deque method, based on Matt Bridges code.
@interface NSMutableArray (ShiftExtension)
// returns the first element of self and removes it
-(id)shift;
@end
@implementation NSMutableArray (ShiftExtension)
-(id)shift {
if([self count] < 1) return nil;
id obj = [[[self objectAtIndex:0] retain] autorelease];
[self removeObjectAtIndex:0];
return obj;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With