January 13, 2012 Fast way to append to an SList | ||||
---|---|---|---|---|
| ||||
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 |
January 13, 2012 Re: Fast way to append to an SList | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fernando Carvajal | 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 |
Copyright © 1999-2021 by the D Language Foundation