Retrieving object from NSMutableArray based on key value of child NSMutableDictonary - objective-c

So I have an NSMutableArray in my app what is filled like so:
NSMutableArray
--Object 1 (NSMutableDictonary)
--Value (i) for key (key)
--Value (i) for key (key)
--Object 2 (NSMutableDictonary)
--Value (i) for key (key)
--Value (i) for key (key)
i need to be able to select an object from within the main NSMutableArray by looking for a key what matches a value of one of the NSMutableDictonary's keys...
I understand i could run a loop like so to achieve this:
for (NSMutableDictionary *object in arrayObject) {
if ([[object objectForKey:#"keyToSearch"] integerValue] == keyToCompare) {
return [object objectForKey:#"keyToReturn"];
}
}
However my concern is that if this array grows (what it can do) then this will take time to run a search on it..
So I was wondering is there any other way to retrieve the same results but more efficiently?
Thanks
Liam

The search is obviously going to take linear time in the number of dictionaries in the array. You're not going to get around that by using a different way to search. You could rewrite your search to use an NSPredicate, and it would be slightly shorter but it wouldn't be any faster.
If you want to make the search faster, you'll have to modify your data structure. Or, you could create a NSDictionary to serve as a lookup table, where the keys of the NSDictionary are values of keyToSearch, and the values are the NSMutableDictionaries in your array.
Still, unless your array gets really really, big the search time will be negligible, and you shouldn't worry about it.

Ultimately, any enumeration of any array will take time that grows as the size of the array grows. If you have any way to hint at the location of it, it may make it faster, but otherwise, Fast Enumeration is probably the fastest way to run through the data in the way that you want to.
Unless you run through hundreds of objects, the time taken should be negligible.

Related

Select nth value of NSArray

How would I go about selecting the nth values of an array and adding them to another array.
For example, if i have an NSArray which has 100 objects and I want to add every 5th object? I understand how to select the 5th object and how to add to a new array etc, but just looking for the best way to do this. This is for image manipulation, so will be dealing with arrays of up to 2m pixel values.
Is the best way to just use for loops?
You can using striding:
.stride(to: 100, by: 5)
So to create a new array:
Array(0.stride(to: 10, by: 2).map( { myArray[$0] }))
UPDATE: As Leo Dabus points out, the above will start at element 0 (and take every 2nd). If you want to start at the 5th and take every 5th, you would use:
Array(4.stride(to: 100, by: 5).map( { myArray[$0] }))
Using loops is pretty good: they are easy to read, and they are about as efficient as anything else that you may want to use for this purpose. The only optimization to the for loop approach is to reserve a specific number of elements upfront, because you know how many elements you are going to write.
If you are going to make the same selection from multiple arrays (e.g. processing an array of arrays), consider creating NSIndexSet, and applying it with objectsAtIndexes to perform the selection. This may give your code slightly better readability, because the for loop for creating indexes would be separate from the process of selection.
Finally, if you need to optimize for speed, and your arrays store wrapped primitives, consider using plain arrays instead of NSArray to avoid wrapping and unwrapping. This has a potential of giving you the most improvement, because by eliminating additional memory accesses for unwrapping it would also significantly improve locality of reference, which has crucial importance for cache use optimization.

Keeping an array sorted - at setting, getting or later?

As an aid to learning objective c/oop, I'm designing an iOS app to store and display periodic bodyweight measurements. I've got a singleton which returns a mutablearray of the shared store of measurement object. Each measurement will have at least a date and a body weight, and I want to be able to add historic measurements.
I'd like to display the measurements in date order. What's the best way to do this? As far as I can see the options are as follows: 1) when adding a measurement - I override addobject to sort the shared store every time after a measurement is added, 2) when retrieving the mutablearray I sort it, or 3) I retrieve the mutablearray in whatever order it happens to be in the shared store, then sort it when displaying the table/chart.
It's likely that the data will be retrieved more frequently than a new datum is added, so option 1 will reduce redundant sorting of the shared store - so this is the best way, yes?
You can use a modified version of (1). Instead of sorting the complete array each time a new object is inserted, you use the method described here: https://stackoverflow.com/a/8180369/1187415 to insert the new object into the array at the correct place.
Then for each insert you have only a binary search to find the correct index for the new object, and the array is always in correct order.
Since you said that the data is more frequently retrieved than new data is added, this seems to be more efficient.
If I forget your special case, this question is not so easy to answer. There are two basic solutions:
Keep array unsorted and when you try to access the element and array is not sorted, then sort it. Let's call it "lazy sorting".
Keep array sorted when inserting elements. Note this is not about appending new element at the end and then sort the whole array. This is about finding where the element should be (binary search) and place it there. Let's call it "sorted insert".
Both techniques are correct and useful and deciding which one is better depends on your use cases.
Example:
You want to insert hundreds of elements into the array, then access the elements, then again insert hundreds of elements, then access. In summary, you will be inserting values in big chunks. In this case, lazy sorting will be better.
You will often insert individual elements and you will access the elements often. Then sorted insert will have better performance.
Something in the middle (between inserting 1 and inserting tens of elements). You probably don't care which one of the methods will be used.
(Note that you can use also specialized structures to keep an array sorted, not based on NSArray, e.g. structures based on a balanced tree, while keeping number of elements in the subtree).

Is there any built in method for sorting in Objective-c?

I have two sorted NSMutableArrays (or I can use any other collection, not critical), I need to insert objects from the first array to the second and preserve sort order in the second array. What is the optimal (fastest) method to do that? I can implement all the known good algorithms, but my question is, if there is already some built-in method? If not, what is the best algorithm in my case?
The real answer would be: it depends, since you are asking: what is the fastest way of inserting objects from one array into another while preserving sort order.
There is no built in way of inserting in the right place of a sorted array. You can achieve the same effect by just adding the two arrays together but it won't be "the fastest way".
What is actually faster depends on many things like: how much data does the arrays contain, what is the ratio of data in array1 vs array2 (does one array contain much more data than the other)?, etc.
NOTE: You should probably begin with the simple solution and only optimize once you experience performance problems. Do measurements with a large data set though, to see that your solution works with whatever data your users may have.
Inserting items from one sorted array into another sorted array
If you want to merge the two arrays by inserting objects in the right place then normal algorithms apply. You should insert the smaller array into the bigger array and try to insert entire sorted sequences where possible instead of every item one by one.
For best performance you should try to make a batch insert using insertObjects:atIndexes: instead of inserting the object one by one.
You can use indexOfObject:inSortedRange:options:usingComparator: to find the index that each item should be inserted in the other array if you specify NSBinarySearchingInsertionIndex for the options. Also, the comparator you are using must be the same as the comparator that sorted the array, otherwise the result is "undefined".
With this in mind you would do something like this
Create mutable index
For every ITEM in SMALLER ARRAY
Find the index where to insert ITEM in LONGER ARRAY
Add (the insertion location + the location in the short array) as the index in the mutable set.
Next item.
Batch insert all items.
The documentation for insertObjects:atIndexes: tells you that "the corresponding location specified in indexes after earlier insertions have been made." Which in your case with two sorted array mean all items with a lower index will already have been added and thus you should add the index of the object in the short array to the value returned from indexOfObject:inSortedRange:options:usingComparator:.
Another (probably very premature optimization) you can do is decrease the sortedRange for every item in the loop so that you don't have to search through parts of the array that you know the item to be inserted is bigger than.
There are probably many other optimizations that can be made. You should still measure first! Hopefully this will get you started.
NSArray *newArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
newArray = [newArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I would start by simply adding all of the objects of the first array to the second and then resorting the second. Time how long it takes. If it is acceptable, stop there.
If not, you could try a binary search to find the insertion point in the second array for each item in the first array. Since both arrays are sorted, you might be able to optimise the search by using the last insertion point as the lower bound each time round. Something like this:
NSInteger insertionPoint = -1;
for (id object in array1)
{
insertionPoint = [self binarySearch: array2 for: object lowerBound: insertionPoint + 1];
[array2 insertObject: object atIndex: insertionPoint];
}
The Cocoa class NSSortDescriptor together with sortedArrayUsingDescriptors: from NSArray should do what you are after.
Since you are using mutable arrays, you might want to use sortUsingDescriptors: which sorts the mutable array without creating a new one.
Look at the documentation here to see if any of the NSArray sort methods work for you. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html. You can scroll down to the methods and there's 7 built-in ones for sorting. You could probably just combine the two arrays and run the sortedArrayUsingComparator: or one of the other methods.

Looking for a number in a group (array)

Is there a way to perform a group conditional statement?
For example, I have a process which returns random numbers from an array (x) which I want to compare to the first 100 prime numbers.
Is there a function or method available by which I could do:
if (x in first_100_prime_numbers)
...
As Almo already said in a comment, many collection classes in Cocoa have a containsObject: method that you can use for this purpose. Note that performing containsObject: on an array will probably be much slower (especially when the array is large) than on a set or hash table as the method has to iterate over every item in the array until it finds a match.

How to return objects in an NSArray at specified indexes?

Example:
I have an NSArray with 40 objects. What is the most efficient way to return ONLY those objects with index equal to a number specified in another NSArray (e.g {0, 27, 36} for example)?
Can a predicate be used here? Or is there a more simple and efficient approach?
Why don't you just iterate over the index array and look up each index in the data array replacing the index with the looked-up object.
In the end, the array that held the indices now holds the objects. If you don't want to wipe out your index array, then just create a new array of the same size as the index array and put the objects there.
You might be over-thinking the design of this and falling prey to micro-optimization. (Which is a bad thing.)
There is a method (objectsAtIndexes) for returning specified indexes from an original array, but it requires an NSIndexSet as its argument, and there isn't a built-in way to convert your array of indices to an index set. On the plus side, you can generate the index set with a short loop, as follows:
NSMutableIndexSet indexes = [NSMutableIndexSet indexSet];
for (NSNumber * number in indexArray)
{
[indexes addIndex:[number intValue]];
}
return [originalArray objectsAtIndexes:indexes];
Is this any more efficient than simply looping through the original array? I have no idea. You would have to profile your app to be sure.
I remember reading one time, and I cant say where but it stuck in my mind, that if this was something you need to do many times and the array is fairly large that there is a more efficient way. You could create a new dictionary from that array where each entry has the key of the index number and the value is the array item. You only create the dictionary once so the hard work is over in one shot. Then every time you need to access values at certain indexes you just ask for valueForKey:#"indexNumber". But I never had a need for this so I never tested if it was more efficient in the long run... it's just a thought.