Re: Where is NSList?
Re: Where is NSList?
- Subject: Re: Where is NSList?
- From: "John C. Daub" <email@hidden>
- Date: Tue, 27 Jul 2004 19:49:56 -0500
on 7/27/04 5:35 PM, Tim Conkling at email@hidden wrote:
>
Sorry, I think I was pretty unclear in my original post.
>
>
I'm looking for a container that returns some information that uniquely
>
identifies each object added to it. This information is called an
>
iterator in the STL, but it's not used exclusively for iterating over a
>
container of objects, and I'm not actually looking to iterate over a
>
list of objects here. These iterators allow for bidirectional reference
>
between the list and the objects in the list -- i.e., the list knows
>
which objects it contains; and each object, if it chooses to retain the
>
iterator returned to it by the list it is added to, knows exactly where
>
it is contained in the list.
>
>
This is very useful, because it allows for operations like deletion
>
(which take linear time in a container like an NSArray) to be done in
>
constant time (in a list container), if you have retained the iterator
>
for the object to be deleted. Importantly, these iterators remain valid
>
when the container is modified.
>
>
An array is good for quick access via index, but object lookup is slow.
>
If I have an object that I know is contained in an NSArray, and I want
>
to delete it from that array, there is no quick method of doing so --
>
the operation is necessarily performed in linear time, AFAIK.
>
>
Again, I'm new to Cocoa, so I may be missing something, but I have
>
already looked at the documentation for NSArray and NSEnumerator, and
>
they don't seem to provide the functionality I'm looking for. But I
>
certainly apologize if I'm missing something obvious.
I don't think you're missing anything.
NSArray is more like a std::vector in terms of semantics. I understand what
you're wanting with a std::list, since when you modify the contents of the
std::list the std::iterator to some particular object in the std::list
doesn't become invalidated (like it would with a std::vector).
I don't think there's any container in stock Cocoa that has similar
semantics to a std::list. Maybe some 3rd party wrote something somewhere....
However, you may not need to do this. I think you can still use an NSArray
(or NSMutableArray), just when you have that reference to the object just
retain it. Then you could use something like indexOfObject: or removeObject:
or whatever it is that you need. I don't know if these methods function in
linear time or constant time... probably linear given the nature of the
container, but tho the performance characteristics are different than the
std::list, at least you ought to be able to accomplish what it is that
you're wanting.
Of course, if you find it truly lacking and truly necessary to have
std::list-like semantics, I guess you're always welcome to create your own
implementation and share it with the world for the betterment of Cocoa
programming worldwide. :-)
--
John C. Daub }:-)>=
<
mailto:email@hidden> <
http://www.hsoi.com/>
"We're only gonna die from our own arrogance." - Bad Religion
_______________________________________________
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.