Friday, May 31, 2013

NSArray indexOfObject inSortedRange options usingComparator example in Objective C (iOS).


NSArray indexOfObject inSortedRange options usingComparator

Returns the index, within a specified range, of an object compared with elements in the array using a given NSComparator block.

- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp

Parameters of [NSArray indexOfObject inSortedRange options usingComparator]
obj
An object for which to search in the array.
If this value is nil, throws an NSInvalidArgumentException.
r
The range within the array to search for obj.
If r exceeds the bounds of the array (if the location plus length of the range is greater than the count of the array), throws an NSRangeException.
opts
Options for the search. For possible values, see “NSBinarySearchingOptions.”
If you specify both NSBinarySearchingFirstEqual and NSBinarySearchingLastEqual, throws an NSInvalidArgumentException.
cmp
A comparator block used to compare the object obj with elements in the array.
If this value is NULL, throws an NSInvalidArgumentException.

Return Value of [NSArray indexOfObject inSortedRange options usingComparator]
If the NSBinarySearchingInsertionIndex option is not specified:

If the obj is found and neither NSBinarySearchingFirstEqual nor NSBinarySearchingLastEqual is specified, returns an arbitrary matching object's index.
If the NSBinarySearchingFirstEqual option is also specified, returns the lowest index of equal objects.
If the NSBinarySearchingLastEqual option is also specified, returns the highest index of equal objects.
If the object is not found, returns NSNotFound.
If the NSBinarySearchingInsertionIndex option is specified, returns the index at which you should insert obj in order to maintain a sorted array:
[NSArray indexOfObject inSortedRange options usingComparator]
If the obj is found and neither NSBinarySearchingFirstEqual nor NSBinarySearchingLastEqual is specified, returns any equal or one larger index than any matching object’s index.
If the NSBinarySearchingFirstEqual option is also specified, returns the lowest index of equal objects.
If the NSBinarySearchingLastEqual option is also specified, returns the highest index of equal objects.
If the object is not found, returns the index of the least greater object, or the index at the end of the array if the object is larger than all other elements.
Special Considerations
The elements in the array must have already been sorted using the comparator cmp. If the array is not sorted, the result is undefined.

NSArray indexOfObject inSortedRange options usingComparator example.
NSMutableArray *array = …;
id newObject = …;
NSComparator comparator = …;

NSUInteger newIndex = [array indexOfObject:newObject
                             inSortedRange:(NSRange){0, [array count]}
                                   options:NSBinarySearchingInsertionIndex
                           usingComparator:comparator];

[array insertObject:newObject atIndex:newIndex];

Example of [NSArray indexOfObject inSortedRange options usingComparator].
int index1 = [array indexOfObject:number
                    inSortedRange:NSMakeRange(0, [array count])
                          options:NSBinarySearchingFirstEqual
                  usingComparator:globalBlock]; 

NSArray indexOfObject inSortedRange options usingComparator example.
// Run binary search.
int index1 = [array indexOfObject:number
                    inSortedRange:NSMakeRange(0, [array count])
                          options:NSBinarySearchingFirstEqual
                  usingComparator:[self compareNSNumber]];
NSTimeInterval stop1 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Binary: Found index position: %d in %f seconds.", index1, stop1 - start);

End of NSArray indexOfObject inSortedRange options usingComparator example article.