Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Iterator copy method?

Moderators: larsivi kris

Posted: 02/12/08 17:28:05

What happened to the Iterator copy() method?

I was trying out compiling tangobos/tests/writetest.d and it uses a copy() method.

Is there some other way to do that now? Here's the snipped where copy() is used:

    auto iter = intlist.elements();
    while(iter.more()) {
        auto iter0 = iter.copy();
        writefln("Iter iter: ", iter.get());
        writefln("Iter iter: ", iter.get());
        writefln("Iter iter0: ", iter0.get());
        
    }

And the error it gives is

writetest.d(36): Error: no property 'copy' for type 'tango.util.collection.model.GuardIterator.GuardIterator!(int).GuardIterator'
Author Message

Posted: 02/12/08 17:59:34

Curious. According to svn, there's never been such a copy method.

Posted: 02/13/08 00:41:28

Oh, silly me. It appears that I created tangobos/tests/writetest.d myself back in May. Upon revisiting Tangobos yesterday I just assumed that it was part of the distribution.

The copy() method must have been something that I had added to my local copy of Tango back then.

Still, a copy/dup method on tango.util.collection.model.Iterator would be a good thing to have, don't you think? Otherwise there's no way to save your traversal state.

Posted: 02/13/08 07:51:42

It sounds like a good idea from the onset, but how would you ensure that the iterator is still valid at a later point? If you think this is easy to solve, please create a ticket so we can consider it closer.

Posted: 02/14/08 01:12:51

larsivi wrote:

It sounds like a good idea from the onset, but how would you ensure that the iterator is still valid at a later point? If you think this is easy to solve, please create a ticket so we can consider it closer.

You don't have to. Caveat user. Similar caveats apply if you add or remove elements from some kinds of containers in the middle of iterating over them. For instance removing element zero from an array invalidates all current iterators into that array container. But removing an element from a linked list only invalidates iterators that point to the deleted node.

I'll try making a patch. My thought would be to add a .dup method to the base Iterator interface.

Posted: 02/14/08 02:33:54

On second thought, maybe I won't try making a patch.

On second thought a better solution overall might be to create a new "Dupable" interface. Conflating the Iterator interface with copy-ability doesn't seem quite right and will also break any code out there that implements the current Iterator interface, but lacks a dup() method.

So probably this needs more discussion.

Also it's not something I'm actually in need of right now. Though I've definitely written algorithms before using STL that required the ability to copy iterators. I think it was probably something using std::list.