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?
What's the difference between NSIndexSet and NSSet?
Asked Answered
There are a couple reasons:
NSIndexSet
stores unsigned integer primitive types, whereasNSSet
stores objects.NSIndexSet
is optimized for storing unsigned integers, specifically a set of integers into another data structure like anNSArray
.
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.