Add List class
This commit is contained in:
parent
1c6c922b45
commit
8303451929
64
src/hulk/list.d
Normal file
64
src/hulk/list.d
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user