What's the difference between NSIndexSet and NSSet?
Asked Answered
C

1

7

I'm a bit new to Cocoa and I was reading about NSIndexSet. Why is it necessary? It seems to me that NSIndexSet is nothing but a NSSet of integers, is that right? What's the purpose of creating a separate collection alltogether?

Cf answered 19/12, 2011 at 0:12 Comment(0)
A
12

There are a couple reasons:

  1. NSIndexSet stores unsigned integer primitive types, whereas NSSet stores objects.
  2. NSIndexSet is optimized for storing unsigned integers, specifically a set of integers into another data structure like an NSArray.
Affective answered 19/12, 2011 at 0:26 Comment(3)
To elaborate on #2, NSIndexSet lets you iterate through the indexes in order or in reverse order. An NSSet is unordered, and an NSOrderedSet is manually ordered (meaning you can break the order by inserting an object at the wrong position).Jedidiah
Got it. Another thing I just noticed. NSIndexSet actually uses NSRange to store consecutive indexes, indeed very optimized for storing index value into other collections.Cf
Operations over NSSet are O(1) (hashing), while most of the NSIndexSet operations are O(N) (looping).Changteh

© 2022 - 2024 — McMap. All rights reserved.