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);
// Find people to remove and add to a remove list.
Person.ListHead removedPeople;
people.filter(
delegate bool(Person per) // Delegate literal for callback.
{
if(per.age < 40)
{
people.remove(per); // Remove from people list.
removedPeople ~= per; // Add to removed list.
}
return true; // Continue.
});
// Now delete items from removedPeople.
if(!removedPeople.isEmpty)
{
Person cur, next;
next = removedPeople.head;
do
{
cur = next; // Moving to next item (or first).
next = next.next; // Keep track of next one since cur.next won't exist next time around.
delete cur; // Kill it!
}
while(next !is removedPeople.head);
}
// Now there should only be people in their 40s or older, and other people are deleted without use of GC.
foreach(Person p; people.each)
{
writefln("Person %s is %d years old", p.name, p.age);
}
return 0;
}