Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:40:21+00:00 2026-05-29T09:40:21+00:00

I am implementing queued flood fill algorithm and need to store and retrieve pairs

  • 0

I am implementing queued flood fill algorithm and need to store and retrieve pairs of numbers in NSMutableArray.

Basically, I am creating an array

m_queue = [NSMutableArray array];

then at some time I populate the array

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

then I retrieve data for the next iteration and remove 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.x y:nextPoint.y];

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

I don’t really need points, the algorithm uses pairs of integer values, so I think there might be a better way to store such pairs.

UPDATE:
I looked into implementing C-style solution like @mattjgalloway and @CRD suggested.

I’ve introduced

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

and have rewritten code to use linked list of such structs instead of NSMutableArray and CGPoint/NSValue.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:40:23+00:00Added an answer on May 29, 2026 at 9:40 am

    There wouldn’t really be a better Objective-C / Foundation way of doing it, apart from maybe creating your own class such as NumberPair or something which you put into the array rather than using NSValue and CGPoint. It might be slightly more memory efficient to do that and you could make NumberPair contain two integers rather than floats like you are concerned about. Something like:

    @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];
    

    Other than that you could do a more C-like thing of having a struct containing two integers, create a dynamically allocated array to store the structs (you’d need to know the max size of the queue or keep reallocating). Something like:

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

    Also, you might want to check out my MJGStack class which wraps NSMutableArray to provide a stack like interface which you might be able to adjust slightly to do what you want rather than using NSMutableArray directly. Although that’s not essential by any means.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a interpreter-like project for which I need a strange little scheduling queue.
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I am implementing a C++ message queue based on a std::queue. As I need
I need help with implementing IBM WebSphere MQ for a system where the queue
I've been implementing an enhanced Shunting-Yard algorithm for parsing an arithmetic expression. One aspect
I'm implementing a kind of write/store buffer in a Redis-backed library to squash multiple
so I am having trouble implementing priority queue. For a priority queue, I need
I'm implementing Dijkstra's on a board of tiles. I want to store all the
When implementing a graph search algorithm, I needed a priority queue which allowed for
I'm implementing a A* search algorithm but I keep running into problems with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.