xxxxxxxxxx
import std.range;
struct List(T)
{
struct Link
{
Link *prev;
Link *next;
T data = T.init;
}
Link base;
this(Stuff)(Stuff stuff)
{
insert(stuff);
}
void insert(T[] stuff...)
{
if(stuff.length > 1)
{
size_t nitems;
auto r = buildList(stuff, nitems);
r.end.next = base.next;
if(base.next)
base.next.prev = r.end;
base.next = r.start;
base.next.prev = &base;
}
}
static Link *allocate(T item, Link *prev = null, Link *next = null)
{
auto lnk = new Link(prev, next, item);
return lnk;
}
struct Range
{
Link *start;
Link *end;
}
Range buildList(Stuff)(ref scope Stuff stuff, ref size_t numItems)
{
Link *result = allocate(stuff.front);
Link *tail = result;
for(stuff.popFront; !stuff.empty; stuff.popFront)
{
auto newItem = allocate(stuff.front, tail);
tail.next = newItem;
tail = newItem;
}
return Range(result, tail);
}
}
void main()
{
import std.stdio;
auto lnk = new List!string(["hello", "world"]);
auto ptr = lnk.base.next;
while(ptr !is null)
{
writef("%s, ", ptr.data);
ptr = ptr.next;
}
writeln();
}