问题描述
我正在尝试使用objective-c 块实现二进制搜索.我正在使用函数 indexOfObject:inSortedRange:options:usingComparator:
.这是一个例子.
I am trying to implement a binary search using objective-c blocks. I am using the function indexOfObject:inSortedRange:options:usingComparator:
. Here is an example.
// A pile of data.
NSUInteger amount = 900000;
// A number to search for.
NSNumber* number = [NSNumber numberWithInt:724242];
// Create some array.
NSMutableArray* array = [NSMutableArray arrayWithCapacity:amount];
for (NSUInteger i = 0; i < amount; ++i) {;
[array addObject:[NSNumber numberWithUnsignedInteger:i]];
}
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
// Run binary search.
int index1 = [array indexOfObject:number
inSortedRange:NSMakeRange(0, [array count])
options:NSBinarySearchingFirstEqual
usingComparator:^(id lhs, id rhs) {
if ([lhs intValue] < [rhs intValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if([lhs intValue] > [rhs intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSTimeInterval stop1 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Binary: Found index position: %d in %f seconds.", index1, stop1 - start);
// Run normal search.
int index2 = [array indexOfObject:number];
NSTimeInterval stop2 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Normal: Found index position: %d in %f seconds.", index2, stop2 - start);
我想知道如何将外部定义的 Objective-c 块与上述功能一起使用.这里有两个比较函数.
I wonder how I can use an externally defined objective-c block with the aforementioned function. Here are two compare functions.
NSComparisonResult compareNSNumber(id lhs, id rhs) {
return [lhs intValue] < [rhs intValue] ? NSOrderedAscending : [lhs intValue] > [rhs intValue] ? NSOrderedDescending : NSOrderedSame;
}
NSComparisonResult compareInt(int lhs, int rhs) {
return lhs < rhs ? NSOrderedAscending : lhs > rhs ? NSOrderedDescending : NSOrderedSame;
}
这些是参考 NSObjCRuntime.h
中的以下声明编写的.
Those are written in reference to the following declarations which can be found in NSObjCRuntime.h
.
enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
推荐答案
可以将块定义为全局变量,获得类似函数的效果.
You can define a block as a global variable to get an effect similar to functions.
NSComparisonResult (^globalBlock)(id,id) = ^(id lhs, id rhs) {
if([lhs intValue] < [rhs intValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if([lhs intValue] > [rhs intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
};
然后,在进行比较的方法中:
Then, in the method doing the comparison:
int index1 = [array indexOfObject:number
inSortedRange:NSMakeRange(0, [array count])
options:NSBinarySearchingFirstEqual
usingComparator:globalBlock];
将块放在标题中,供外部使用:
To put the block in a header, for external use:
NSComparisonResult (^globalBlock)(id,id);
这篇关于如何内联编写 Objective-C 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!