#
# one std list.
#
std.list.node(data, next, prev) type;
std.list.id(head, tail, count) type;
std.list()
{
init()
return new std.list.id(null, null, 0);
add(id, data) in
std.unit.istrue(id is std.list.id);
{
node = new std.list.node(data, null, id.tail);
if ( id.head == null )
id.head = node;
else
id.tail.next = node;
id.tail = node;
id.count++;
return node;
}
get(id, idx) in
std.unit.istrue(id is std.list.id);
{
count = 0;
current = id.head;
while ( current )
{
if ( count++ == idx )
return current.data;
current = current.next;
}
return null;
}
get_node(id, idx) in
std.unit.istrue(id is std.list.id);
{
count = 0;
current = id.head;
while ( current )
{
if ( count++ == idx )
return current;
current = current.next;
}
return null;
}
del(id, idx) in
std.unit.istrue(id is std.list.id);
{
node = get_node(id, idx);
if ( node == null )
return false;
if ( node == id.head )
id.head = node.next;
else
{
if ( node == id.tail )
id.tail = node.prev;
node.prev.next = node.next;
}
id.count--;
node.data = null;
node.next = null;
node.prev = null;
return true;
}
count(id) in
std.unit.istrue(id is std.list.id);
{
return id.count;
}
release(id) in
std.unit.istrue(id is std.list.id);
{
current = null;
next = null;
for ( current = id.head ;
current ;
current = next )
{
next = current.next;
current.data = null;
current.next = null;
}
id.count = 0;
id.head = null;
id.tail = null;
}
}
std.list.iter.id(list, current) type;
std.list.iter(id)
{
init(id) in
std.unit.istrue(id is std.list.id);
{
return new std.list.iter.id(id, id.head);
}
release(id) in
std.unit.istrue(id is std.list.iter.id);
{
id.list = null;
id.current = null;
}
rewind(id) in
std.unit.istrue(id is std.list.iter.id);
{
id.current = id.list.head;
}
rewind_end(id) in
std.unit.istrue(id is std.list.iter.id);
{
id.current = id.list.tail;
}
next(id) in
std.unit.istrue(id is std.list.iter.id);
{
if ( id.current )
id.current = id.current.next;
return id.current;
}
prev(id) in
std.unit.istrue(id is std.list.iter.id);
{
if ( id.current )
id.current = id.current.prev;
return id.current;
}
end(id) in
std.unit.istrue(id is std.list.iter.id);
{
if ( id.current )
return false;
return true;
}
get(id) in
std.unit.istrue(id is std.list.iter.id);
{
if ( id.current )
return id.current.data;
return null;
}
idx(id) in
std.unit.istrue(id is std.list.iter.id);
{
if ( id.current == null )
return -1;
count = 0;
current = id.list.head;
while ( current )
{
if ( current == id.current )
return count;
current = current.next;
count++;
}
return -1;
}
}
# vim:syntax=one