[Intrusive] Obtaining end iterator from value reference

Hi all, is it possible with boost::intrusive::slist to obtain the end() iterator from a value object present in a list? Using s_iterator_to() I can obtain a valid iterator from a value reference but i can't forward iterate from it without knowing the end(). Reading the slist code I found the method slist_iterator::get_container but since is a detail implementation, I prefer not to use it. I also found that a default construct iterator is almost equivalent to end() for linear slists and should work just fine. For circular slists? // snipped struct dummy : public slist_base_hook<> {}; typedef slist<dummy> MyList; void fn(dummy const& o) { MyList::const_iterator it = MyList::s_iterator_to(o); MyList::const_iterator e = ???; for (; it != e; ++it) { ... } } Regards, Andrea

El 29/04/2013 12:20, Andrea Cuneo escribió:
Hi all,
is it possible with boost::intrusive::slist to obtain the end() iterator from a value object present in a list? Using s_iterator_to() I can obtain a valid iterator from a value reference but i can't forward iterate from it without knowing the end().
Reading the slist code I found the method slist_iterator::get_container but since is a detail implementation, I prefer not to use it.
In most cases it returns null (a pointer to the container is stored only when dereferencing needs to access some container-stored value-traits), s_iterator_to can't be used when value_traits is stateful, so that function is not going to help you.
I also found that a default construct iterator is almost equivalent to end() for linear slists and should work just fine. For circular slists?
There is no way to obtain the end node from a node in a circular list, as there is no runtime information stored in the ring that marks the end node. Best, Ion

On Apr 29, 2013, at 12:34 PM, Ion Gaztañaga
There is no way to obtain the end node from a node in a circular list, as there is no runtime information stored in the ring that marks the end node.
But with a (non-empty) circular list, can't you just iterate until you return to your starting point?

El 30/04/2013 3:00, Nat Goodspeed escribió:
On Apr 29, 2013, at 12:34 PM, Ion Gaztañaga
wrote: There is no way to obtain the end node from a node in a circular list, as there is no runtime information stored in the ring that marks the end node.
But with a (non-empty) circular list, can't you just iterate until you return to your starting point?
The iterator does not know which node is the starting point (only the container stores a pointer to the first node). The iterator only stores a pointer to the next node, it does not know if the next node is the last or the first node as it's a circular slist. Ion

On Wed, May 1, 2013 at 7:09 AM, Ion Gaztañaga
El 30/04/2013 3:00, Nat Goodspeed escribió:
On Apr 29, 2013, at 12:34 PM, Ion Gaztañaga
wrote:
There is no way to obtain the end node from a node in a circular list, as there is no runtime information stored in the ring that marks the end node.
But with a (non-empty) circular list, can't you just iterate until you return to your starting point?
The iterator does not know which node is the starting point (only the container stores a pointer to the first node). The iterator only stores a pointer to the next node, it does not know if the next node is the last or the first node as it's a circular slist.
I think the OP was talking about a function which is passed a single node without knowing its location in the circular list. I was suggesting that if the function can be sure it's a circular list, it should still be possible to iterate over all items in the list, if the function doesn't care whether it starts at the container's official "first" node.

El 01/05/2013 16:01, Nat Linden escribió:
I was suggesting that if the function can be sure it's a circular list, it should still be possible to iterate over all items in the list, if the function doesn't care whether it starts at the container's official "first" node.
Yes, but the ring isn't formed only with user provided nodes. One item in the ring will be the "end" node that is stored in the container (one past the last user element). No one except the container knows which node is this. Trying to use the end node will provoke UB.
participants (4)
-
Andrea Cuneo
-
Ion Gaztañaga
-
Nat Goodspeed
-
Nat Linden