65 lines
1.1 KiB
D
65 lines
1.1 KiB
D
/**
|
|
* Linked list functionality.
|
|
*/
|
|
module hulk.list;
|
|
|
|
import hulk.hurl.a1;
|
|
|
|
/**
|
|
* Linked list structure.
|
|
*/
|
|
struct List(T)
|
|
{
|
|
/**
|
|
* Linked list item.
|
|
*/
|
|
T item;
|
|
|
|
/**
|
|
* Next list entry.
|
|
*/
|
|
List!T * next;
|
|
|
|
/**
|
|
* Add an item to this linked list.
|
|
*/
|
|
public void add(T item)
|
|
{
|
|
List!T * last_entry = last_entry();
|
|
List!T * new_entry = A1.allocate!(List!T)();
|
|
last_entry.item = item;
|
|
last_entry.next = new_entry;
|
|
}
|
|
|
|
/**
|
|
* Allow foreach iteration across a List.
|
|
*/
|
|
public int opApply(scope int delegate(ref T) dg)
|
|
{
|
|
List!T * entry = &this;
|
|
while (entry.next != null)
|
|
{
|
|
int result = dg(entry.item);
|
|
if (result != 0)
|
|
{
|
|
return result;
|
|
}
|
|
entry = entry.next;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Get the last linked list entry.
|
|
*/
|
|
private @property List!T * last_entry()
|
|
{
|
|
List!T * result = &this;
|
|
while (result.next != null)
|
|
{
|
|
result = result.next;
|
|
}
|
|
return result;
|
|
}
|
|
}
|