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!
在我使用NSMutableArray滚动自己的Queue之前,我想知道是否有更多可用的标准。我没有在Apple文档中看到任何内容,但如果没有人们正在使用的某个地方的Queue实现,我会感到惊讶。 Java破坏了我!
6 个解决方案
#1
10
Implementing a queue based on NSMutableArray
is pretty easy, it's probably under 50 lines of code.
实现基于NSMutableArray的队列非常简单,可能不到50行代码。
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
#2
5
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).
Cocoa本身没有Queue类,并且本身没有标准,但有几个选项,其中一个可能最适合您的需求。看到这个问题(和我的回答)。
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.
就像你说的,你可以使用NSMutableArray自己动手。如果你只需要一个quick'n'dirty队列(并且不担心复制,编码/解码,枚举等),那么@Matt建议的解决方案是一种简单的方法。您还应该考虑通过类别向NSMutableArray添加队列方法,这很好,因为您的“队列”也是一个数组(因此您可以为NSArray参数传递它),并且您可以免费获得所有NS(Mutable)数组功能。
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.)
如果性能很重要,我建议使用更适合移除第一个元素的结构。出于这个原因,我为自己的框架编写了CHCircularBufferQueue。 (不要试图嘟嘟我自己的号角,只是试着节省一些时间。)
#3
1
I have made a category containing just the deque method, based on Matt Bridges code.
我基于Matt Bridges代码创建了一个仅包含deque方法的类别。
@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
#4
0
You can use the STL queue from the C++ Standard Library.
您可以使用C ++标准库中的STL队列。
#5
0
Check out the STL priority queue. It requires zero lines of code and it's portable! What more could you want?
检查STL优先级队列。它需要零行代码,并且可移植!你还能想要什么?
#6
0
You could use the :lastObject method of NSArray. Here is an untested example:
您可以使用NSArray的:lastObject方法。这是一个未经测试的例子:
Queue.h
#import <Foundation/Foundation.h>
@interface Queue : NSObject
-(void)enqueue:(id)object;
-(id)dequeue;
@end
Queue.m
#import "Queue.h"
@interface Queue()
@property(nonatomic, strong) NSMutableArray *backingArray;
@end
@implementation Queue
-(id)init {
self = [super init];
if (self) {
self.backingArray = [NSMutableArray array];
}
return self;
}
-(void)enqueue:(id<NSObject>)object {
[self.backingArray addObject:object];
}
-(id)dequeue {
id object = [self.backingArray lastObject];
[self.backingArray removeObject:object];
return object;
}
@end
#1
10
Implementing a queue based on NSMutableArray
is pretty easy, it's probably under 50 lines of code.
实现基于NSMutableArray的队列非常简单,可能不到50行代码。
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
#2
5
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).
Cocoa本身没有Queue类,并且本身没有标准,但有几个选项,其中一个可能最适合您的需求。看到这个问题(和我的回答)。
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.
就像你说的,你可以使用NSMutableArray自己动手。如果你只需要一个quick'n'dirty队列(并且不担心复制,编码/解码,枚举等),那么@Matt建议的解决方案是一种简单的方法。您还应该考虑通过类别向NSMutableArray添加队列方法,这很好,因为您的“队列”也是一个数组(因此您可以为NSArray参数传递它),并且您可以免费获得所有NS(Mutable)数组功能。
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.)
如果性能很重要,我建议使用更适合移除第一个元素的结构。出于这个原因,我为自己的框架编写了CHCircularBufferQueue。 (不要试图嘟嘟我自己的号角,只是试着节省一些时间。)
#3
1
I have made a category containing just the deque method, based on Matt Bridges code.
我基于Matt Bridges代码创建了一个仅包含deque方法的类别。
@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
#4
0
You can use the STL queue from the C++ Standard Library.
您可以使用C ++标准库中的STL队列。
#5
0
Check out the STL priority queue. It requires zero lines of code and it's portable! What more could you want?
检查STL优先级队列。它需要零行代码,并且可移植!你还能想要什么?
#6
0
You could use the :lastObject method of NSArray. Here is an untested example:
您可以使用NSArray的:lastObject方法。这是一个未经测试的例子:
Queue.h
#import <Foundation/Foundation.h>
@interface Queue : NSObject
-(void)enqueue:(id)object;
-(id)dequeue;
@end
Queue.m
#import "Queue.h"
@interface Queue()
@property(nonatomic, strong) NSMutableArray *backingArray;
@end
@implementation Queue
-(id)init {
self = [super init];
if (self) {
self.backingArray = [NSMutableArray array];
}
return self;
}
-(void)enqueue:(id<NSObject>)object {
[self.backingArray addObject:object];
}
-(id)dequeue {
id object = [self.backingArray lastObject];
[self.backingArray removeObject:object];
return object;
}
@end