Merged [18171]: An exception like [http://www.visualdistortion.org/crash/view.jsp...
[adiumx.git] / Other / XtrasCreator / NSMutableArrayAdditions.m
bloba98667ca83dc70552c58bf8c4b596371a7ee38fb
1 //
2 //      NSMutableArrayAdditions.m
3 //      Growl
4 //
5 //      Created by Mac-arena the Bored Zo on 2005-09-12.
6 //  Copyright 2005 The Growl Project. All rights reserved.
7 //
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];
19         if (!count) {
20                 //bail now so we can assume a non-empty array later
21                 return 0U;
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;
25         }
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
31                  *      right here.
32                  */
33                 return i;
34         }
35         signed movementDirection = initialComparison;
36         i += movementDirection;
38         while ((i  > 0U)
39         &&     (i <  count)
40         &&     compareObjectsWithSelector(obj, [self objectAtIndex:i], compareCmd) == initialComparison
41         ) {
42                 i += movementDirection;
43         }
45         return i;
48 @end
50 static inline NSComparisonResult compareObjectsWithSelector(id a, id b, SEL cmd) {
51         return (NSComparisonResult)objc_msgSend(a, cmd, b);