从NSArray中获取的NSIndexSet [英] Get NSIndexSet from NSArray

查看:189
本文介绍了从NSArray中获取的NSIndexSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSArray的有有用的方法来找到指定索引的对象。

NSArray has useful methods to find objects for specified indexes

// To find objects by indexes
- (id)objectAtIndex:(NSUInteger)index
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes

// To find index by object
- (NSUInteger)indexOfObject:(id)anObject

不过,我想获得的NSIndexSet (多个索引)给定的对象。是这样的:

However, I want to get NSIndexSet (multiple indexes) for given objects. Something like:

- (NSIndexSet *)indexesOfObjects:(NSArray *)objects

此方法不存在为的NSArray 。我缺少的东西吗?是否有人知道另一种标准方法?否则,我必须写这是一个类中的方法。

This method does not exist for NSArray. Am I missing something? Does someone know another standard method? Otherwise I have to write this as a category method.

推荐答案

它可能使用一组指定的对象找到,如来实现它是有用的

It might be useful to implement it using a set to specify the objects to find, such as:

- (NSIndexSet *) indicesOfObjectsInSet: (NSSet *) set
{
    if ( [set count] == 0 )
        return ( [NSIndexSet indexSet] );

    NSMutableIndexSet * indices = [NSMutableIndexSet indexSet];

    NSUInteger index = 0;
    for ( id obj in self )
    {
        if ( [set containsObject: obj] )
            [indices addIndex: index];

        index++;
    }

    return ( [[indices copy] autorelease] );
}

这需要访问每一个对象数组中,但至少只有这样做一次,并利用快速枚举,而这样做。使用的NSSet和反对该组数组中测试的每个对象也比阵列测试纳入快得多。

This requires visiting every object in the array, but at least only does so once and makes use of fast enumeration while doing so. Using an NSSet and testing each object in the array against that set is also much faster than testing for inclusion in an array.

有一个潜在的优化在这里,但它会在一个单一的对象被存储在接收阵列多次的情况下断裂

There's a potential optimization here, but it would break in the case where a single object is stored in the receiving array multiple times:

if ( [set containsObject: obj] )
{
    [indices addIndex: index];
    if ( [indices count] == [set count] )
        break;
}

如果您扫描这样一个20'000项的Array两个对象和他们都在前十里面,你将能够避免阵列中的扫描其他19'990对象。正如我虽然说,没有帮助如果数组包含重复,因为它会尽快它的发现2指数(即使它们都指向同一个对象)停止。

That way if you're scanning a 20'000-item array for two objects and they're both inside the first ten, you'll be able to avoid scanning the other 19'990 objects in the array. As I said though, that doesn't help if the array contains duplicates, because it'll stop as soon as it's found 2 indices (even if they both point to the same object).

话说回来,我跟Mike的评论表示赞同以上。机会是你在和自己的一些痛苦来优化时间。这可能是值得我们思考不同的数据类型;例如,虽然看起来NSArray的一个简单的平面容器中的最合理的选择,如果你实际上并不需要的订购信息,最好使用一个替代的NSSet;这有它不会存储在同一对象的附加优势(计算使用 -isEqual:)的两倍。如果你想跟踪重复的,但不需要排序,你可以使用NSCountedSet,这表现为的NSSet除了它跟踪每个对象有多少次被添加/删除,而不实际存储重复。

Having said that, I agree with Mike's comment above. Chances are you're setting yourself up for some pain come optimization-time. It may be worth thinking about different data types; for instance, while NSArray seems the most logical choice for a simple flat container, if you don't actually need the ordering information it's better to use an NSSet instead; this has the added advantage that it won't store the same object (calculated using -isEqual:) twice. If you do want to keep track of duplicates, but don't need ordering, you can use NSCountedSet, which behaves as NSSet except it keeps track of how many times each objects has been added/removed without actually storing duplicates.

这篇关于从NSArray中获取的NSIndexSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆