Sign in to follow this  
bilsa

iterators - creating

Recommended Posts

Hey all! In several collection libraries I have seen they create iterators like this: 1. iterate through the entire collection and copy each element into a new array. 2. return an iterator with the new array as internal So, I was wondering why you would want to do that? What I can figure is the following: 1. You can concurrently modify the collection without risking the iterator being broken. 2. You get the actual elements at the point of iterator creation But: 1. The performance should drop by half 2. If you would want to have concurrent modification, would it not be better create a separate serialized implementation with an iterator that locks() the collection when iterating? Am I just ignorantly clueless?? Thanks!

Share this post


Link to post
Share on other sites
No, you are not. In my opinion, an iterator should only have knowledge of the current element and how to go to the next element (or to any other element in the iterated container if random access is allowed). Having it to store the whole collection is just dumb, as any collection change outside the iterator should invalidate the iterator (if needed; in vectors, removing the last element should not change begin()) - that's the way the standard C++ library works, and it's quite an idiomatic use.

Concurrent modification should be implement by the client, not by the library - because you can't know if your library will be used in a concurrent environment. There is no point in building a thread-safe container library if you can't say how it will be used. A container library must be as fast as possible, and as safe as possible, but the additional safety provided by a thread-aware layer is just YAGNI in most cases.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by bilsa
Hey all!

In several collection libraries I have seen they create iterators like this:

1. iterate through the entire collection and copy each element into a new array.
2. return an iterator with the new array as internal

Are you sure that's what they do, or are you just assuming? I would really like to see an example of this, because the behavior makes absolutely no sense.

I believe foreach in C# is implemented in a [similar] fasion, but that's not an iterator, its an iteration, and its done entirely by the client.

CM

Share this post


Link to post
Share on other sites
Hi!

Thanks for your responses guys. I don't know if it makes any difference but I was looking at the JDSL - http://www.jdsl.org/doc.html.

The iterators are java style iterators. But I guess the concept should be similar to stl iterators.

I could agree that the foreach could be implemented in that sort of way, since it iterates "in place".

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this