NSDictionary 的 NSMutableArray 中的二分搜索

发布于 2024-10-18 20:36:48 字数 1460 浏览 3 评论 0原文

大家好!这是第一个问题,所以请温柔一点:)。我对 Objective-C 有点熟悉,但在细节上有一些问题。简而言之,我想知道如何对 NSDictionaries 的 NSMutableArray 执行二分搜索,并在该搜索中查找部分字符串语法。我有一个 NSDictionaries 的 NSMutableArray,其中数组是一个联系人列表,每个 NSDictionary 是一个联系人(包含四条信息)。 对数组进行排序

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:type  ascending:YES];
[addressBook sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

我设法使用字典中用户提供的键 。该程序的部分功能是对各个联系人中的每条数据执行前缀搜索。

因此,虽然我发现 -indexOfObject:inSortedRange:options:usingComparator: (特别是 这篇文章,我不知道如何指示搜索搜索字符串的前缀以及如何实现搜索本身的块语法

我尝试了这个 - 但我只是得到了 。如此有用的“预期表达式”错误,因为我假设我无法用indexOfObjectPassingTest替换indexOfObject,

unsigned index2 = [addressBook 
            indexOfObjectPassingTest:<#^BOOL(id obj, NSUInteger idx, BOOL *stop)predicate#>:^(id obj, NSUInteger idx, BOOL *stop)
                       inSortedRange:NSMakeRange(0, [addressBook count]) 
                             options:NSBinarySearchingFirstEqual 
                     usingComparator:(NSComparator^(id obj1, id obj2)];

我将不胜感激任何帮助 -indexOfObject:inSortedRange:options:usingComparator: 工作,或者如果有人对二进制搜索有其他建议NSDictionaries 的 NSArray,我洗耳恭听

(是的,虽然这是一个算法作业,但它与编程语言无关。我试图最大限度地提高速度,所以每次搜索时我的想法都是数组。我还考虑过为每个字典键创建排序数组的副本,但由于我们正在处理大型 CSV 文件,这似乎有点过分了。因此,任何其他特定于 Mac 的渐近提示将不胜感激,因为在 Apple 的文档中通常很难找到这一点。)

Howdy all! First question here, so please be gentle :). I'm somewhat familiar with Objective-C but am having some problems with the particulars. In short, I want to know how I can performa a binary search on a NSMutableArray of NSDictionaries, and in that search, look for partial string syntax. I have an NSMutableArray of NSDictionaries, where the array is a contact list and each NSDictionary is one contact (with four pieces of information). I managed to sort the array using

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:type  ascending:YES];
[addressBook sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

where the key is the user-supplied key in the dictionary. Part of the functionality of the program is to perform prefix searches on each piece of data in the individual contacts.

So while I have found -indexOfObject:inSortedRange:options:usingComparator: (and in particular, this post, I can't figure out how to instruct the search to both search for the prefix of a string and how to implement the block syntax for the search itself.

I tried this - but I just get the ever so helpful "Expected expression" error, as I'm assuming I cannot substitute indexOfObjectPassingTest for indexOfObject.

unsigned index2 = [addressBook 
            indexOfObjectPassingTest:<#^BOOL(id obj, NSUInteger idx, BOOL *stop)predicate#>:^(id obj, NSUInteger idx, BOOL *stop)
                       inSortedRange:NSMakeRange(0, [addressBook count]) 
                             options:NSBinarySearchingFirstEqual 
                     usingComparator:(NSComparator^(id obj1, id obj2)];

I'd appreciate any assistance in getting -indexOfObject:inSortedRange:options:usingComparator: to work, or if someone has another suggestion for binary search on an NSArray of NSDictionaries, I'm all ears.

(And yes, while this is for an algorithms assignment, it's programming language agnostic. I am trying to maximize speed, so my thought had been to the array each time you are searching on it. I had also thought of making copies of the array sorted for each dictionary key, but because we are dealing with large CSV files, that seemed a bit excessive. So any other Mac-specific asymptotic hints would be MUCH appreciated, as it's often hard to find that in Apple's documentation.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2024-10-25 20:36:48

Foundation 框架不允许您在 NSArray 上显式指定二分搜索,除非您已经拥有该对象并正在查找索引值(显然数组并不总是排序的)。

您可以做的一件事是获取指定键的值数组,将其转换为 CFArray(免费)并使用 Core Foundation 的 CFArrayBSearchValue 函数。

我要做的另一件事是向 NSArray 添加一个类别,它采用块状比较器并实现二进制搜索算法。

Foundation framework does not let you explicitly specify binary search on NSArray unless you already have the object and looking for the index value (well obviously arrays are not always sorted).

One thing you could do is to get an array of values for the specified key, convert it to an CFArray (for free) and use Core Foundation's CFArrayBSearchValue function.

The other thing I would do is to add a category to NSArray which takes a block-formed comparator and implements binary search algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文