在Objective-C中存储和检索数字对的快速方法
我正在实现排队泛洪填充算法,需要在在Objective-C中存储和检索数字对的快速方法,objective-c,ios,nsmutablearray,Objective C,Ios,Nsmutablearray,我正在实现排队泛洪填充算法,需要在NSMutableArray中存储和检索数字对 基本上,我正在创建一个数组 m_queue = [NSMutableArray array]; 然后在某个时候填充数组 [m_queue addObject:[NSValue valueWithCGPoint:CGPointMake(x + 1, y)]]; 然后检索下一次迭代的数据,并删除数组开头的值 NSValue* value = [m_queue objectAtIndex:0]; [m_queue r
NSMutableArray
中存储和检索数字对
基本上,我正在创建一个数组
m_queue = [NSMutableArray array];
然后在某个时候填充数组
[m_queue addObject:[NSValue valueWithCGPoint:CGPointMake(x + 1, y)]];
然后检索下一次迭代的数据,并删除数组开头的值
NSValue* value = [m_queue objectAtIndex:0];
[m_queue removeObjectAtIndex:0];
CGPoint nextPoint = [value CGPointValue];
[self queueFloodFill8:nextPoint.x y:nextPoint.y];
问题是:如何避免创建大量CGPoint
和NSValue
对象
我真的不需要点,算法使用整数值对,所以我认为可能有更好的方法来存储这样的对
更新:
我研究了如何实现@mattjgalloway和@CRD建议的C风格解决方案
我已经介绍了
typedef struct lookup_point_struct
{
int x;
int y;
struct lookup_point_struct* next;
} LookupPoint;
并重写了代码以使用此类结构的链表,而不是NSMutableArray
和CGPoint
/NSValue
所有这些使我的代码速度提高了大约3倍。内存消耗也显著下降。您希望您的
m_队列
数组能有多大
如果<代码> NSMutableArray < /COD>和<代码> NSvest对象(<代码> CGPoT < /代码>是一个结构,没有实际成本)影响您的算法,那么考虑使用C样式的结构数组作为循环缓冲区,以及队列的前/后两个索引。您可以将其抽象为队列类(或adt,如果需要,可以使用函数节省动态方法调用开销)
如果你需要处理一个无界队列,你可以<代码> > Mulalc < /C> >代码> ReLoCu按需要排队队列/ADT的数组(这基本上是<代码> NSMutableArray <代码>在幕后,但它的通用性有更多的开销)。除了创建自己的类之外,比如
NumberPair
或者将其放入数组中,而不是使用NSValue
和CGPoint
。这样做可能会稍微提高内存效率,您可以使NumberPair
包含两个整数,而不是像您所关心的那样包含浮点。比如:
@interface NumberPair : NSObject
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@end
@implementation NumberPair
@synthesize x, y;
@end
...
m_queue = [NSMutableArray array];
NumberPair *newPair = [[NumberPair alloc] init];
newPair.x = 1;
newPair.y = 2;
[m_queue addObject:newPair];
...
NumberPair *nextPoint = [m_queue objectAtIndex:0];
[m_queue removeObjectAtIndex:0];
[self queueFloodFill8:nextPoint.x y:nextPoint.y];
typedef struct {
int x;
int y;
} NumberPair;
NumberPair *m_queue = (NumberPair*)malloc(sizeof(NumberPair) * QUEUE_SIZE);
// ... etc
除此之外,您可以做一件更像C的事情,让一个包含两个整数的struct
,创建一个动态分配的数组来存储结构(您需要知道队列的最大大小或保持重新分配)。比如:
@interface NumberPair : NSObject
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@end
@implementation NumberPair
@synthesize x, y;
@end
...
m_queue = [NSMutableArray array];
NumberPair *newPair = [[NumberPair alloc] init];
newPair.x = 1;
newPair.y = 2;
[m_queue addObject:newPair];
...
NumberPair *nextPoint = [m_queue objectAtIndex:0];
[m_queue removeObjectAtIndex:0];
[self queueFloodFill8:nextPoint.x y:nextPoint.y];
typedef struct {
int x;
int y;
} NumberPair;
NumberPair *m_queue = (NumberPair*)malloc(sizeof(NumberPair) * QUEUE_SIZE);
// ... etc
另外,您可能想查看我的类,它包装了
NSMutableArray
,提供了一个类似堆栈的接口,您可以稍微调整它来做您想做的事情,而不是直接使用NSMutableArray
。尽管这并不重要。为什么不使用具有偶数个条目的可变数组?@Vince这将带来两倍的NSValue
分配、数组项分配和删除。关键是加快整个过程。检查了代码,对于我正在测试的图像,m_queue
获得了800-2000个条目。@Bobrovsky-您可能应该将@CRD和我指出的C风格方法与您的CGPoint
方法以及我在回答我的建议。@CRD谢谢,我已经实现了您建议的内容(请参阅更新)。我希望我能接受两个答案作为答案:-)谢谢你的提示。我用我所做的信息更新了问题。太棒了!很好的解决方案。这样的链表非常适合这样做。