Iterating NSSet (was Re: My new framwork)
Iterating NSSet (was Re: My new framwork)
- Subject: Iterating NSSet (was Re: My new framwork)
- From: mmalcolm crawford <email@hidden>
- Date: Wed, 3 Mar 2004 15:48:11 -0800
On Mar 3, 2004, at 2:09 PM, Allan Odgaard wrote:
This seems straightforward enough:
Forwards:
unsigned currentIndex = [indexSet firstIndex];
while (currentIndex != NSNotFound)
{
//...
currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
}
I should have added efficiently. I doubt that indexGreaterThanIndex:
is O(1) and thus a bad idea for iterating large index sets, which is
also hinted by the documentation for
getIndexes:maxCount:inIndexRange:, which says: "Use this method to
quickly and efficiently traverse an index set".
NSIndexSet internally caches last-accesses, allowing such enumeration
(forwards or backwards) to happen very efficiently. The code snippet
above should be good enough for most uses. A reason to switch to the
get... variant is to avoid the message-sending overhead of the loop, in
case that becomes an issue.
mmalc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.