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

linked list iterators

Moderators: larsivi kris

Posted: 05/25/07 07:31:19 Modified: 05/25/07 07:31:51

I'm trying to figure out how to take a linked list like LinkSeq? or CircularSeq? and do something like this:

  • Iterate forward to find an element
  • Save the current iterator
  • Iterate forward some more till I find another element
  • then I want to remove the elements between those two iterators

I've got two problems: There doesn't seem to be a dup/clone/duplicate type method on iterators, and there isn't a removeRange type method that takes iterators.

Wouldn't it make more sense for iterators to be structs with value semantics? An iterator is a glorified pointer. Why should it have reference semantics? It differs from what I'm used to with C++ anyway. Maybe it makes sense in Java or somewhere where there are no structs.

Author Message

Posted: 05/25/07 20:31:43

Iterators are 'fast fail' enabled, meaning they test to see if the container is modified during the traversal. If the iterator was allowed to mutate the container, it would effectively kill itself :)

I'd like to see structs used for iterators also, though there are difficulties in doing so. However, opApply is available directly in the container itself so Iterators are actually optional in many cases

Posted: 05/26/07 06:13:27

Not sure I understand your response, since I didn't say I wanted to use any of the iterators after modifying the list. Either way, it seems an odd comment since one of the main reasons to use linked lists is precisely that their iterators aren't invalidated by mutations elsewhere in the list. (If they are in Tango then that's quite odd.) Sure, if I've got an iterator pointing to element X, and X gets removed then my iterator is no longer valid, but if I'm pointing to node X and Y is removed, my iterator should still be good. Are you saying Tango's linked list iterators don't work that way?

Posted: 05/28/07 20:38:14

'fast fail' is related to multiple threads