list

Linked list; list.d 2.1.2. This circular linked list adds the next and previous pointers directly into your data structure (struct or class) using a mixin so that no extra memory allocations are needed to add items to the list. It is even possible to have more than one linked list per data structure by making use of mixin identifiers.

The documentation file can be compiled to test the below example.

Example:
module people;
private import std.stdio;
private import list;

class Person
{
	mixin List; // Turn the Person class into a linked list.

	char[] name;
	ubyte age;

	this(char[] name, ubyte age)
	{
		this.name = name;
		this.age = age;
	}
}

int main()
{
	Person.ListHead people;
	people ~= new Person("John", 19);
	people ~= new Person("Paul", 31);
	people ~= new Person("George", 44);
	people ~= new Person("Ringo", 41);

	foreach(Person p; people.each)
	{
		writefln("Person %s is %d years old", p.name, p.age);
	}

	return 0;
}
template List()
Mixin for creating circular linked lists.

PType prev();
Property:
get previous item in the list. null if not belonging to a list.
PType next();
Property:
get next item in the list. null if not belonging to a list.
void beginList();
Begin a list with this item. Must not already belong to a list. It is not necessary to keep this item as the list head.

bool listIsEmpty();
Property:
get whether or not the list is empty.
void listAdd(PType item);
Add an item after this one. item must not already belong to a list.

void listAddSafe(PType item);
Same as listAdd but will beginList() automatically if needed.

void listAddTail(PType item);
Add an item before this one. item must not already belong to a list.

void listAddTailSafe(PType item);
Same as listAddTail but will beginList() automatically if needed.

void opCatAssign(PType item);
Shortcut for listAddTailSafe(item).

void listRemove();
Remove this item from the list. Must belong to a list.

void unlist();
Remove this item from the list. It is safe to call this even if not belonging to a list.

PType filter(bool delegate(PType) callback);
Calls back callback for each item in the list, allowing the item to be removed. Only the item being called back may be removed during filter. Removed items must not be deleted until the end of filter. callback can return false to stop filter early.

Returns:
the item started on, one of its next items if removed during filter, or null if all items removed.

ListEach each();
Property:
get an enumerator for foreach to loop over all items, starting at this one and stopping when all have been encountered.

each.backwards can be used to foreach over all items in reverse order, starting with the previous one and stopping on this as the last one.

List items must not be added or removed during foreach - use filter for such modifications.
ListEndless endless();
Property:
get an enumerator for foreach to loop over all items, starting at this one and continuously looping until foreach breaks.

endless.backwards can be used to foreach over all items in reverse order, starting with the previous one and continuously looping until foreach breaks.

List items must not be added or removed during foreach - use filter for such modifications.
PType listSkip(int n);
Skips n items and returns that item. n may be negative. Skipping wraps around if not enough items.

PType listFind(PType item);
Returns the item if item is part of this list, otherwise returns null.

PType listFind(bool delegate(PType item) compare);
Calls back compare for each item in the list; when compare returns true, listFind returns that item; or listFind returns null if compare never returns true.

size_t listLength();
Property:
get the count of items in the list. This causes the list to be traversed.
ListHead listHead();
Property:
get a ListHead for this item.
struct ListHead;
Designating an item to be the list's head.

PType head();
Property:
get first item, or null if none.
PType tail();
Property:
get last item, or null if none.
bool isEmpty();
Property:
get whether or not the list is empty.
void addHead(PType item);
Adds a new item to the list, making a new head.

void addTail(PType item);
Adds a new item to the list, making a new tail.

void opCatAssign(PType item);
Shortcut for addTail(item).

void remove(PType item);
Properly adjusts the head if the head item is removed.

void skipHead(int n);
Skip n items and change the head item.

size_t length();
PType filter(bool delegate(PType) callback);
ListEach each();
ListEndless endless();
PType skip(int n);
PType find(PType item);
PType find(bool delegate(PType item) compare);
Aliases for operating on the head item.

template ListNames()
Mixin for friendlier List names. Can also take a mixin identifier as an argument.