Skip to content
Browse files

Fix a bug in NSArray Binary Search

  • Loading branch information...
1 parent 3caecfc commit c22eebf301d1aab23269f4ed4b806483a7804a39 @Naituw committed Mar 18, 2012
View
2 WeiboSDK/Model/Stream/WeiboConcreteStatusesStream.m
@@ -128,7 +128,7 @@ - (void)_deleteStatus:(WeiboBaseStatus *)theStatus{
- (NSUInteger)statuseIndex:(WeiboBaseStatus *)theStatus{
NSInteger index = [statuses binarySearch:theStatus
usingBlock:^NSComparisonResult(id key, id object) {
- return [(WeiboBaseStatus *)object compare:key];
+ return [object compare:key];
}];
return index;
}
View
4 WeiboSDK/Utilities/Additions/NSArray+WeiboAdditions.m
@@ -21,7 +21,7 @@ -(NSInteger)binarySearch:(id)key usingBlock:(CompareObjects)comparator{
if(self.count == 0 || key == nil || comparator == NULL)
return -1;
NSInteger min = 0, max = [self count] - 1;
- while (min != max){
+ while (min <= max){
const NSInteger mid = min + (max - min) / 2;
switch (comparator(key, [self objectAtIndex:mid])){
case NSOrderedSame:
@@ -34,7 +34,7 @@ -(NSInteger)binarySearch:(id)key usingBlock:(CompareObjects)comparator{
break;
}
}
- return min; //-(min + 1)
+ return ~min; //-(min + 1) Key not found
}
@end

0 comments on commit c22eebf

Please sign in to comment.
Something went wrong with that request. Please try again.