简体   繁体   English

NSArray class 的“sortedArrayHint”方法,这个方法的目的是什么,它是如何使用的?

[英]“sortedArrayHint” method of NSArray class, what is the purpose of this method and how is it used?

Question is the same as the title.问题与标题相同。 ("sortedArrayHint" method of NSArray class, what is the purpose of this method and how is it used) (NSArray class的“sortedArrayHint”方法,这个方法的目的是什么,怎么用)

I read documentation but the explanation is not clear.我阅读了文档,但解释不清楚。

Please explain the purpose of this method and its usage.请说明此方法的目的及其用法。

The idea is simple.这个想法很简单。 Assume you have a large array that should always be sorted.假设您有一个应该始终排序的大数组。 Changing or inserting even one element means you have to resort the array.甚至更改或插入一个元素都意味着您必须使用数组。 Sorting is costly.分拣成本很高。

The method -[NSArray sortedArrayHint] can be called on an already sorted array in order to get private internal data that can be used to speed up a sort of the same array given that only a small change has been made.可以在已经排序的数组上调用-[NSArray sortedArrayHint]方法,以获取私有内部数据,如果只进行了很小的更改,这些数据可用于加速同一数组的排序。

Usage is simple:用法很简单:

  1. Get and store the hint from the original sorted array using -[NSArray sortedArrayHint] .使用-[NSArray sortedArrayHint]从原始排序数组中获取并存储提示。
  2. After a small change;稍作改动后; resort using -[NSArray sortedArrayUsingFunction:context:hint:] with the stored hint.使用-[NSArray sortedArrayUsingFunction:context:hint:]和存储的提示。
  3. After a large change;经过大的改变; resort using -[NSArray sortedArrayUsingFunction:context:] , and get a new hint.使用-[NSArray sortedArrayUsingFunction:context:] ,并获得新的提示。

What is a small, or large, change is something you must measure with Instruments.什么是小的或大的变化是你必须用 Instruments 来衡量的。

I never use this myself, since I have found it more effective to use my own categories on NSArray and NSMutabelArray for sorted inserts, that uses a binary search, on sorted array.我自己从不使用它,因为我发现在NSArrayNSMutabelArray上使用我自己的类别来进行排序插入更有效,它在排序数组上使用二进制搜索。 My code is available as open source here: https://github.com/Jayway/CWFoundation我的代码在此处作为开源代码提供: https://github.com/Jayway/CWFoundation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM