digitalmars.D.learn - Fast way to append to an SList
- Fernando Carvajal (12/12) Jan 13 2012 I need a fast way to append to an SList. I was thinking of using insertA...
- Steven Schveighoffer (15/24) Jan 13 2012 end.popFront();
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? list.insertAfter(end, 21); ... So, how would you get the end of a SList without traversing it from the start? Thanks
Jan 13 2012
On Fri, 13 Jan 2012 06:34:39 -0500, Fernando Carvajal <fernandocarvajal 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
Jan 13 2012