A quick way to store and retrieve pairs of numbers in Objective-C

I implement an algorithm for populating filled queues and must store and retrieve pairs of numbers in NSMutableArray .

Basically, I create an array

 m_queue = [NSMutableArray array]; 

then at some point i fill the array

 [m_queue addObject:[NSValue valueWithCGPoint:CGPointMake(x + 1, y)]]; 

then I get the data for the next iteration and delete the value at the beginning of the array

 NSValue* value = [m_queue objectAtIndex:0]; [m_queue removeObjectAtIndex:0]; CGPoint nextPoint = [value CGPointValue]; [self queueFloodFill8:nextPoint.xy:nextPoint.y]; 

The question is: what can I do to avoid creating a large number of CGPoint and NSValue ?

I do not need points, the algorithm uses pairs of integer values, so I think that there may be a better way to store such pairs.

UPDATE: I studied the implementation of a C-style solution like @mattjgalloway and @CRD.

I imagined

 typedef struct lookup_point_struct { int x; int y; struct lookup_point_struct* next; } LookupPoint; 

and rewritten code to use a linked list of such structures instead of NSMutableArray and CGPoint / NSValue .

All this made my code about 3 times faster. And memory consumption has also dropped significantly.

+4
source share
2 answers

In fact, there would be no better way to Objective-C / Foundation, except to create your own class, such as NumberPair or what you put in an array, rather than using NSValue and CGPoint . This may be a bit more memory efficient, and you can make NumberPair contain two integers rather than float, as you are concerned. Sort of:

 @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.xy:nextPoint.y]; 

In addition, you could do a more C-like thing with a struct containing two integers, create a dynamically allocated array for storing structures (you need to know the maximum size of the queue or reallocate). Sort of:

 typedef struct { int x; int y; } NumberPair; NumberPair *m_queue = (NumberPair*)malloc(sizeof(NumberPair) * QUEUE_SIZE); // ... etc 

Alternatively, you might want to check out the MJGStack class that wraps NSMutableArray to provide an interface like you might be able to tweak a bit to do what you want, rather than using NSMutableArray directly. Although this is not important in any case.

+1
source

How big do you expect to get an m_queue array?

If the value of NSMutableArray and NSValue ( CGPoint is a structure, there is no real value there) affects your algorithm, then consider using an array of C structures as a circular buffer along with two indexes for the front / back of the queue. You can abstract this into the queue class (or adt, using functions to save the overhead of the dynamic method if you need to).

If you need to deal with an unlimited queue, you can malloc and realloc array with your class / adt queue as needed (which is essentially what NSMutableArray does behind the scenes, but with a lot of overhead for its generality).

+1
source

Source: https://habr.com/ru/post/1394361/


All Articles