implementation of `Iterator` for `Cursor` is wrong
hawkw opened this issue · 3 comments
I've recently started adding Cursor
s to intruder_alarm::doubly::List
(#9) in a branch. However, I've hit a fairly serious bug in the implementation of Iterator
for Cursor
. Cursor
s acting as Iterator
s will skip the first element of the data structure they cursor over.
The bug
Iterator::next
is currently implemented for Cursor
by calling self.next_item()
:
However, Cursor::next_item
will advance the cursor to the next element in the list and then call Cursor::get
, getting the current element:
In my branch, I'm creating my Cursor
s pointing at the head element of the list:
Since the Cursor
already points at the head element when Iterator::next
is called on it for the first time, Cursor::next_item
will advance the cursor and return the next item. The iterator never returns the head element.
How to fix it
We have a couple options here. We could special-case the Iterator
impl to check if it's pointing at the first element, but this seems kind of inelegant.
I think a better option is to rewrite Iterator::next
for Cursor
to call self.get()
, store the return value in a temporary variable, call self.move_forward()
, and then return the result of the call to get
. If we thought this functionality might be useful in contexts other than in the Iterator
implementation, we could also consider encapsulating it in a new function on Cursor
(maybe called get_and_advance
or something?), but this is probably unnecessary.
This should be a pretty easy fix if anyone's interested in picking it up!
@amanjeev I wouldn't worry about this right now --- fixing this bug shouldn't require any changes to the doubly-linked list code that you're basing your implementation on.
I'd focus on implementing the basic singly-linked list data structure, and hold off on adding iterators and/or cursors at this point. Whether or not a singly linked list should even provide a cursor (it doesn't really make sense, since a cursor would only be able to traverse the list in one direction) is an open question at this point.