8 Replies - 12308 Views - Last Post: 18 March 2013 - 11:57 AM

looping algorithm suckyness

Posted 16 March 2013 - 08:18 AM

Background:

I've got 1 object (we'll call this MainObj ) 3 arrays of objects. Two of them have data that is unrelated. The third is and association array that contains objects with properties for determining how the two otherwise unrelated arrays can be related to the MainObj.

The end goal is to have a subarray of the related as and bs.
I realize that this could be solved with maps in the MainObject unfortunately I'm dealing with an api that queries a database for these values and three individual queries returning three individual arrays is the only starting point. The length of these arrays as, bs, associations can and will be different.

Potential solutions:

So the obvious approach is to loop through all three arrays to determine this.

Replies To: looping algorithm suckyness

Re: looping algorithm suckyness

Posted 16 March 2013 - 02:59 PM

Encapsulate the problem in a class. Then you don't have to care if the class materialises the view or scans your arrays every time it is queried. I'm not entirely clear on how your association array works but something suitable can probably be adapted from the following:

Re: looping algorithm suckyness

Posted 17 March 2013 - 10:23 AM

stackoverflow you're correct that this would at least limit doing this multiple times; however, in this case it's going to be used as a utility function where the cache would not reflect updates unless it was refreshed. The api in this case doesn't support page queries. So it's an all or nothing query. Meaning I couldn't just query for the updates an merge them in.

Re: looping algorithm suckyness

Posted 17 March 2013 - 10:39 AM

cfoley, on 16 March 2013 - 10:59 PM, said:

Encapsulate the problem in a class. Then you don't have to care if the class materialises the view or scans your arrays every time it is queried. I'm not entirely clear on how your association array works but something suitable can probably be adapted from the following:

cfoley I don't understand how this solves the looping issue. I'm reading your answer to imply creating a utility class that builds the two association arrays that are needed via the Relateion method. So the Relateion method would still do the exact same looping algorithm, but you get the accessor methods associatedAs and associatedBs. Maybe I wasn't clear in the initial post, or I misunderstand you're solution. Creating a wrapper or utility object to hold the finished products is really a non-issue. I only interested in trying to find a better solution to the initial looping algorithm.

To be fair these are completely disparate objects so there just might not be a better solution.

Re: looping algorithm suckyness

Posted 17 March 2013 - 02:06 PM

I think my point about the looping issue is that it might not be an issue. If you put it behind an interface then you can swap in a different implementation if you have performance problems. Other than that, it's a nice simple piece of code that communicates your intent.

If you have a large set of data, it's an n^2 algorithm so you might find that it's slow. If and only if you find it's slow, you can switch to maps which will give you a linear algorithm. Don't worry about having to construct the maps from the arrays. That will still be fast compared to the original algorithm.

Re: looping algorithm suckyness

Posted 18 March 2013 - 09:19 AM

cfoley I wholly agree with you and in fact my implementation is similar to your encapsulation idea. O(n^2) is probably not going to be an issue in my case. I was really just curious if someone smarter than me might have a solution that I wasn't seeing. Thanks for you input.