2 // NSMutableArrayAdditions.m
5 // Created by Mac-arena the Bored Zo on 2005-09-12.
6 // Copyright 2005 The Growl Project. All rights reserved.
8 // This file is under the BSD License, refer to License.txt for details
10 #import "NSMutableArrayAdditions.h"
11 #include <objc/objc-runtime.h>
13 static inline NSComparisonResult compareObjectsWithSelector(id a, id b, SEL cmd);
15 @implementation NSMutableArray (NSMutableArrayAdditions)
17 - (unsigned) indexForInsortingObject:(id)obj usingSelector:(SEL)compareCmd {
18 unsigned count = [self count];
20 //bail now so we can assume a non-empty array later
22 } else if (count == 1U) {
23 //bail now so we can assume an array with more than one object later
24 return compareObjectsWithSelector(obj, [self objectAtIndex:0U], compareCmd) == NSOrderedDescending;
27 unsigned i = count / 2U;
28 NSComparisonResult initialComparison = compareObjectsWithSelector(obj, [self objectAtIndex:i], compareCmd);
29 if (initialComparison == NSOrderedSame) {
30 /*the object to be inserted is equal to the pivot, so we can just insert it
35 signed movementDirection = initialComparison;
36 i += movementDirection;
40 && compareObjectsWithSelector(obj, [self objectAtIndex:i], compareCmd) == initialComparison
42 i += movementDirection;
50 static inline NSComparisonResult compareObjectsWithSelector(id a, id b, SEL cmd) {
51 return (NSComparisonResult)objc_msgSend(a, cmd, b);