Fast way to append to an SList

Steven Schveighoffer schveiguy at yahoo.com
Fri Jan 13 06:57:41 PST 2012


On Fri, 13 Jan 2012 06:34:39 -0500, Fernando Carvajal  
<fernandocarvajal at mailinator.com> wrote:

> I need a fast way to append to an SList. I was thinking of using  
> insertAfter()
> and keep track of a range that points to the end of the list but, after
> appending an item, how would you get the new range to the end?
>
> This is what I had planned:
>
> List!int list;
> Range end = list[];
>
> list.insertAfter(end, 42);
> end = // How to get the new end?

end.popFront();

But it's slightly more complex than  that.  Because end is actually a null  
pointer to begin with, it's not a valid range of the list.  This is due to  
the fact that SList is a struct.  the first insert will generate a valid  
range.  So it should be like this:

List!int list;
list.insertFront(42); // first insert
auto end = list[]; // note, you can't use Range here, it would be  
List!int.Range.

// from now on:
list.insertAfter(end, 21);
end.popFront();

-Steve


More information about the Digitalmars-d-learn mailing list